-->
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.  [ 11 posts ] 
Author Message
 Post subject: Hibernate one-to-many association
PostPosted: Thu Aug 18, 2005 11:59 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
Hi,
Can anyone help me on this simple example that I found in web? When I try to insert a House object after having set Students collection, it hangs for the release of lock and stops.

Mapping files
===========

<hibernate-mapping package="example">
<class name="House" table="amber_one2many_houses">
<id name="id" column="id"
type="int"
>
<generator class="assigned" />
</id>

<property name="name"/>

<set name="students" lazy="true">
<key column="house"/>
<one-to-many class="Student" />
</set>
</class>
</hibernate-mapping>


<hibernate-mapping package="example">
<class name="Student" table="amber_one2many_students">
<id name="id" column="id"
type="int"
>
<generator class="assigned" />
</id>
<property name="name"/>
<many-to-one name="house" /> <!-- class="House" not-null="true" column="house" unique="true" /> -->
</class>
</hibernate-mapping>

Code that inserts House
===============

Student st=new Student();
java.util.HashSet students=new java.util.HashSet();
st.setId(10);
st.setName("Arumugam");

Student st1=new Student();
st1.setId(11);
st1.setName("Ramamani");
students.add(st1);
students.add(st);

House house=new House();
house.setName("White House");
house.setId(10);
house.setStudents(students);

hsession.save(house);
trans.commit();



Exception :
========

Hibernate: insert into amber_one2many_houses (name, id) values (?, ?)
Hibernate: update amber_one2many_students set house=? where id=?
Hibernate: update amber_one2many_students set house=? where id=?
Exception :org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1

Exception thrown is :org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:88)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:74)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at dao.user.HibernateUserDAO.insert(HibernateUserDAO.java:71)
at service.user.HibernateUserService.insertUser(HibernateUserService.java:25)
at controller.RegisterUserController.onSubmit(RegisterUserController.java:58)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:248)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:243)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:355)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Response from Hibernate :-1



Any idea to solve this problem. I have been stuck at Association problems with hibernate since last week, yet not able to figure out why Hibernate is not as simple as CMR in EJB.

Thanks in advance.

Aru K


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 12:12 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
try

<set name="students" lazy="true" inverse="true">
<key column="house"/>
<one-to-many class="Student" />
</set>

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 12:17 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
Thanks Li!

It does solve one half of association. ie, it is now inserting House, but not Students?

What do I need to do to insert Students too when I insert its parent House?

Plz advise!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 12:24 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
I am sorry, I should have added the cascading style:

Try

<set name="students" lazy="true" inverse="true" cascade="all">
<key column="house"/>
<one-to-many class="Student" />
</set>

Hope this helps!

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 12:36 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
Hi Li,

Now I get exactly what I got previously.

One insert for Parent and two Updates for child table.


Exception
=======
Hibernate: insert into amber_one2many_houses (name, id) values (?, ?)
Hibernate: update amber_one2many_students set name=?, house=? where id=?
Hibernate: update amber_one2many_students set name=?, house=? where id=?
Exception :org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
Our luck deserted us. Exception thrown is :org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:88)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:74)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at dao.user.HibernateUserDAO.insert(HibernateUserDAO.java:71)
at service.user.HibernateUserService.insertUser(HibernateUserService.java:25)
at controller.RegisterUserController.onSubmit(RegisterUserController.java:58)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:248)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:243)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:355)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Response from Hibernate :-1


This is what I get even if I change cascade value to different options.

Plz advise!!!


Top
 Profile  
 
 Post subject: Re: Hibernate one-to-many association
PostPosted: Fri Aug 19, 2005 12:58 am 
Beginner
Beginner

Joined: Wed Jul 13, 2005 2:18 pm
Posts: 44
arumugam_kasi wrote:
Hi,
Mapping files
===========

<hibernate-mapping package="example">
<class name="House" table="amber_one2many_houses">
<id name="id" column="id"
type="int"
>
<generator class="assigned" />
</id>

<property name="name"/>

<set name="students" lazy="true">
<key column="house"/>
<one-to-many class="Student" />
</set>
</class>
</hibernate-mapping>


<hibernate-mapping package="example">
<class name="Student" table="amber_one2many_students">
<id name="id" column="id"
type="int"
>
<generator class="assigned" />
</id>
<property name="name"/>
<many-to-one name="house" /> <!-- class="House" not-null="true" column="house" unique="true" /> -->
</class>
</hibernate-mapping>


As suggested by previous poster, inverse=true and cascade=all should be set on the <set> tag. But as you saw, that was only part of the problem...

arumugam_kasi wrote:
Code that inserts House
===============

Student st=new Student();
java.util.HashSet students=new java.util.HashSet();
st.setId(10);
st.setName("Arumugam");

Student st1=new Student();
st1.setId(11);
st1.setName("Ramamani");
students.add(st1);
students.add(st);

House house=new House();
house.setName("White House");
house.setId(10);
house.setStudents(students);

hsession.save(house);
trans.commit();


The way you've coded this confuses hibernate into thinking more things happened than actually did. As suggested in the hibernate manual, I would do something like this:

Code:
House house = new House();
house.setName("White House");
house.setId(10);

Student st1 = new Student();
st1.setId(11);
st1.setName("Ramamani");
house.addStudent(st1);

Student st2 = new Student();
st2.setId(10);
st2.setName("Arumugam");
house.addStudent(st2);

session.save(house);
trans.commit();


Notice the use of an 'addStudent' method, which would look something like this:
Code:
public void addStudent(Student student) {
    students.add(student);
    student.setHouse(this);
}


The reason you're getting multiple updates is because you've told hibernate to cascade the update operation on House to its associated Students. So when you add the students to the house and save the house, the students will get saved automatically. Calling save again on the students is redundant.

_________________
- Matt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 1:19 am 
Beginner
Beginner

Joined: Wed Jul 13, 2005 2:18 pm
Posts: 44
Should have added this in the previous post - for a good explanation of biderectional one-to-many mappings, check out this portion of the manual:

http://www.hibernate.org/hib_docs/refer ... child.html

_________________
- Matt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 1:27 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
Again I get exception, hanging for lock release, as it issues two updates.

I think I miss something in my class.

House.java
=========

public class House {
private int _id;

private String _name;
private Collection _students=new java.util.HashSet();


/**
* Returns the id of the house, the primary key.
*/
public int getId()
{
return _id;
}

/**
* Sets the primary key of the house, only called from a create method.
*
* @param id the houseId of the new house
*/
public void setId(int id)
{
_id = id;
}

/**
* Returns the name of the house.
*/
public String getName()
{
return _name;
}

/**
* Sets the name of the house.
*/
public void setName(String name)
{
_name = name;
}

/**
* Returns house students
*/
public Collection getStudents()
{
return _students;
}

/**
* Sets the students of the house.
*/
public void setStudents(Collection students)
{
_students = students;
}

public void addStudent(Student student) {
_students.add(student);
student.setHouse(this);
}

}



Student.java
=========

package example;

/**
* Implementation class for the Student bean.
*
* This bean use the following schema:
*
* <code><pre>
* CREATE TABLE amber_many2one_students (
* id INTEGER NOT NULL,
*
* name VARCHAR(250),
*
* house INTEGER,
*
* PRIMARY KEY(id)
* );
* </pre></code>
*/
public class Student {
private int _id;

private String _name;

private House _house;

/**
* Returns the id of the student, the primary key.
*/
public int getId()
{
return _id;
}

/**
* Sets the primary key of the student, only called from a create method.
*
* @param id the studentId of the new student
*/
public void setId(int id)
{
_id = id;
}

/**
* Returns the name of the instructor for this student.
*
* Amber will automatically cache the value so most
* calls to getInstructor can avoid database calls.
*/
public String getName()
{
return _name;
}

/**
* Sets the name of the instructor for this student.
*/
public void setName(String name)
{
_name = name;
}

/**
* Returns the student's house.
*
* Amber will cache the value so most calls to getHouse can
* avoid database calls.
*/
public House getHouse()
{
return _house;
}

/**
* Sets the student's house.
*/
public void setHouse(House house)
{
_house = house;
}
}



Appln Code
========

House house = new House();
house.setName("White House");
house.setId(12);

Student st1 = new Student();
st1.setId(11);
st1.setName("Ramamani");
house.addStudent(st1);

Student st2 = new Student();
st2.setId(10);
st2.setName("Arumugam");
house.addStudent(st2);

hsession.save(house);
trans.commit();


Exception:
=======
Exception thrown is :org.hibernate.exception.LockAcquisitionException: Could not execute JDBC batch update
org.hibernate.exception.LockAcquisitionException: Could not execute JDBC batch update

.
.
.


Please help me to solve this irritating issue.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 1:35 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
Correction.

It was not issueing two updates, but single insert into house table caused the given exception.

Sorry for the mistake.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 10:57 am 
Newbie

Joined: Tue Aug 02, 2005 1:40 pm
Posts: 16
Location: US
<many-to-one name="house" />

in your many to one give the column="id" and set insert="false" and update="false" and then try running it and let me know weather it is inserting or updating .. i am also getting the same dam problem atleast doing this will get rid of the batch update problem


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 20, 2005 4:45 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:58 am
Posts: 40
Location: Singapore
HURRAHHHHHHHHHHHHH.....


I got what went wrong with my code in one-to-many association.

Its because of data type I used in my Java for IDs. Use of Long solved all the problems.

Wishes all the best to all Friends!!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.