-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: one-to-many bidirectional insert problem
PostPosted: Mon Nov 14, 2005 8:06 pm 
Newbie

Joined: Sun Nov 13, 2005 5:06 pm
Posts: 3
I have a problem with one-to-many bidirectional relationship. I can see the insert statements being echo'd to the system console but the records are not actually getting inserted into the 'many' table.

I am committing and flushing the session and no exceptions are thrown. Any suggestions are appreciated.

Database is MySQL.

Code snippet and mapping are as follows:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

Transaction tx = session.beginTransaction();
Employee employee = (Employee)session.load(Employee.class, "1");
Set jobs = employee.getJobs();
if (jobs == null){
jobs = new HashSet();
employee.setJobs(jobs);
}
Job job = new Job();
job.Name = "Work";
jobs.add(job);

session.save(employee);
tx.commit();
session.flush();

snippet from the hibernate file on the Employee end:

<class name="Employee" table="Employee" >
<id name="employeeId" column="employeeId" type="java.lang.String" unsaved-value="undefined">
<generator class="assigned"/>
</id>
<set name="jobs" inverse="true" cascade="all">
<key column="jobId" />
<one-to-many class="Job" />
</set>
</class>

snippet from the hibernate file on the Job end:

<id name="jobId" column="jobId" type="java.lang.Integer">
<generator class="identity"/>
</id>
<many-to-one name="employee"
column="employeeId"
not-null="true" />


Top
 Profile  
 
 Post subject: show the generted sql by hibernate.
PostPosted: Tue Nov 15, 2005 4:22 am 
Newbie

Joined: Thu Jun 23, 2005 9:23 pm
Posts: 13
could you show the generted sql by hibernate.


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional insert problem
PostPosted: Tue Nov 15, 2005 4:32 am 
Newbie

Joined: Thu Jun 23, 2005 9:23 pm
Posts: 13
steven512 wrote:
snippet from the hibernate file on the Job end:

<id name="jobId" column="jobId" type="java.lang.Integer">
<generator class="identity"/>
</id>
<many-to-one name="employee"
column="employeeId"
not-null="true" />


maybe you miss the the class attribute at hte <many-to-one> tag.
add the class = "Employee" into the many-to-one tag.
best wishes~!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 10:30 am 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
It looks like you are flushing after the commit. (I don't know if that has anything to do with it, though.)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.