From: <leg...@at...> - 2003-08-02 07:41:14
|
The following comment has been added to this issue: Author: Paul Rivers Created: Sat, 2 Aug 2003 2:39 AM Body: My apologies - in creating the simple test case, I found that I had I had this code: public void setParent(Parent parent) { this.parent = parent; parent.getBagChildren().add(this); } Which evidentally caused the newly loaded object to be added to it's parent's collection twice. Sorry! --------------------------------------------------------------------- View the issue: http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-223 Here is an overview of the issue: --------------------------------------------------------------------- Key: HB-223 Summary: Parent with Bag children returns duplicate children Type: Bug Status: Unassigned Priority: Major Project: Hibernate2 Components: core Versions: 2.0.1 Assignee: Reporter: Paul Rivers Created: Fri, 1 Aug 2003 4:03 PM Updated: Fri, 1 Aug 2003 4:03 PM Environment: WindowsXP, 1.4.1_03, MySQL 4.0.13 Description: I have a simple case - a parent, with children in a bag. It's a one-to-many mapping, the bag is lazy. I create a parent, add a child, and save them. At this point, there's only 1 child in the bag. I start a new session, and load the parent - and now there's two children in the bag. They are both identical copies of the original child. When I check the database, there is only 1 child in the database. If I'm doing something wrong, please let me know. I can't figure out what it would be - it's such a simple case. Below is the relevant code, if there's anything else I can do to help let me know: ------------------------------------------------------------ Parent mapping of the child: <bag name="bagChildren" inverse="true" cascade="all" lazy="true"> <key column="parent_id"/> <one-to-many class="hibernateexamples.BagChild"/> </bag> ------------------------------------------------------------ Mapping of the child: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="hibernateexamples.BagChild" table="BagChild"> <id name="id" unsaved-value="-1"> <generator class="native" /> </id> <property name="name" type="string" length="2147483647"/> <many-to-one name="parent" column="parent_id" /> </class> </hibernate-mapping> ------------------------------------------------------------ Test code: public static void main(String[] args) throws Throwable { //Recreate the database tables CreateTablesMain.main(null); Session session = DatabaseHelper.startSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); Parent parent = new Parent("Parent Name", "The parent for testing Bag"); BagChild bagChild = new BagChild(parent, "Bag Child 1"); System.out.println("Size of bag before saved: " + parent.getBagChildren().size()); session.save(parent); transaction.commit(); } finally { // If the transaction wasn't already commited or rolled back, assume an error occured and roll it back if(transaction != null && !transaction.wasCommitted() && !transaction.wasRolledBack()) transaction.rollback(); session.close(); } session = DatabaseHelper.startSession(); transaction = null; try { transaction = session.beginTransaction(); // Print bag children Parent parent = (Parent) session.load(Parent.class, new Long(1)); Collection bagChildren = parent.getBagChildren(); System.out.println("Bag Size: " + bagChildren.size()); Iterator it = bagChildren.iterator(); while(it.hasNext()) { BagChild child = (BagChild) it.next(); System.out.println(child.getId()); } transaction.commit(); } finally { // If the transaction wasn't already commited or rolled back, assume an error occured and roll it back if(transaction != null && !transaction.wasCommitted() && !transaction.wasRolledBack()) transaction.rollback(); session.close(); } } ------------------------------------------------------------ Test code prints out: Size of bag before saved: 1 Hibernate: insert into parent (name, description) values (?, ?) Hibernate: SELECT LAST_INSERT_ID() Hibernate: insert into BagChild (name, parent_id) values (?, ?) Hibernate: SELECT LAST_INSERT_ID() Hibernate: select parent0_.id as id, parent0_.name as name, parent0_.description as descript3_ from parent parent0_ where parent0_.id=? Hibernate: select bagchild0_.id as id__, bagchild0_.id as id, bagchild0_.name as name, bagchild0_.parent_id as parent_id from BagChild bagchild0_ where bagchild0_.parent_id=? Bag Size: 2 1 1 --------------------------------------------------------------------- JIRA INFORMATION: This message is automatically generated by JIRA. If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa If you want more information on JIRA, or have a bug to report see: http://www.atlassian.com/software/jira |