Hibernate with relation one to many

P

Pavel L.Yatsuk

Hello!
I'm trying to use hibernate for objects wich have relation one to many
all request with selection work fine but with save or update i receive
exception.
Can anybody say whats wrong?

-------------------- exception -------------
27/7/2004 9:44:32 net.sf.hibernate.impl.SessionFactoryObjectFactory
addInstance
INFO: no JNDI name configured
Hibernate: update intcandidate set first_name=?, last_name=?, email=?,
password=? where id=?
27/7/2004 9:44:32 net.sf.hibernate.impl.SessionImpl execute
SEVERE: Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL update or deletion failed (row
not foun
d)
at
net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatche
r.java:25)
at
net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:672)
at
net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:625)
at
net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52
)
at
net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2262)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
at
com.evalica.hudson.candidates.web.CandidateServlet.addemployment(Cand
idateServlet.java:164)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
com.evalica.hudson.web.ActionServlet.doGet(ActionServlet.java:77)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl



My config files and beans
--------- Candidate ----------
<hibernate-mapping>

<class name="com.evalica.hudson.candidates.model.Candidate"
table="intcandidate">

<id name="id" column="id" type="int" unsaved-value="0" >
<generator class="increment"/>
</id>

<property name="firstName" column="first_name" not-null="true"/>
<property name="lastName" column="last_name" not-null="true"/>
<property name="email" not-null="true"/>
<property name="password" not-null="true"/>

<set name="employments" cascade="all" >
<key column="c_id" />
<one-to-many
class="com.evalica.hudson.candidates.model.Employment"/>
</set>

</class>

</hibernate-mapping>


public class Candidate {

private int id;
private String firstName;
private String lastName;
private String email;
private String password;
private Set employments=new HashSet();

for all present get and set method

-------------- Employment.hbm.xml ------------
<hibernate-mapping>

<class name="com.evalica.hudson.candidates.model.Employment"
table="intemployment">

<id name="id" column="id" type="int" unsaved-value="0" >
<generator class="increment"/>
</id>

<property name="startDate" column="start_date" type="date"/>
<property name="endDate" column="end_date" type="date"/>
<property name="company" type="string"/>

</class>

</hibernate-mapping>

public class Employment {

private int id;
private Date startDate;
private Date endDate;
private String company;

for all present get and set method
 
O

Oscar kind

Pavel L.Yatsuk said:
I'm trying to use hibernate for objects wich have relation one to many
all request with selection work fine but with save or update i receive
exception.
Can anybody say whats wrong?

-------------------- exception ------------- [...]
net.sf.hibernate.HibernateException: SQL update or deletion failed (row
not foun
d)

Apparently, Hibernate tries to reference a row that doesn't exist (yet).


[...]
--------- Candidate ---------- [...]
<set name="employments" cascade="all" >
<key column="c_id" />
<one-to-many
class="com.evalica.hudson.candidates.model.Employment"/>
</set>
[...]
-------------- Employment.hbm.xml ------------
[mapping and class without a reference to Candidate]


If a Candidate has a Set of Employment's with a one-to-many relationship,
the Employment's must reference the Candidate. Also, to prevent clashes,
one of the releations (the reference or the Set) must be "inverse". This
means that that relation isn't updated, but the other is.

For more information on this, see:
http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-onetomany
http://www.hibernate.org/hib_docs/reference/en/html/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top