-->
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.  [ 2 posts ] 
Author Message
 Post subject: Simple cloning with a simple collection.
PostPosted: Mon Feb 28, 2005 11:41 am 
Newbie

Joined: Mon Feb 28, 2005 11:07 am
Posts: 1
I am using Hibernate 2.0 with Eclipse 3.1 RC5 and MySQL 4.1 and I'm trying to do a simple thing: take a persistent object that has a collection (a Barrell full of Monkeys) and clone it. The collection is just a simple set collection, and I'm using SerializationHelper to do the actual cloning. What I want is to keep both Barrel objects (the original and the clone) with their respective collections. However, Hibernate is intent on removing the collection of the first barrel after with second Barrel (the clone) is put into the database. I can get around all this by writing custom clone code for each class that creates a new object,populates its data, and returns the new object, but this seems kinda silly in that Hibernate has to have some sort of facility to do this sort of simple thing. Hibernate In Action doesn't seem to have the info I need sitting on a platter for me to pick out either. A smack in the right direction would be grateful.

<?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="src.Barrel" table="barrels">
<id name="id" column="barrel_id">
<generator class="increment"/>
</id>
<property name="label" column="label"/>
<set name="contents" cascade="save-update" lazy="false">
<key column="barrel_id"/>
<one-to-many class="src.Monkey"/>
</set>
</class>
</hibernate-mapping>

<?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="src.Monkey" table="monkeys">
<id name="id" column="monkey_id">
<generator class="increment"/>
</id>
<property name="name" column="name"/>
<property name="age" column="age"/>

</class>
</hibernate-mapping>

Some source:

// Create the initial barrel

Barrel theBarrel = new Barrel("Barrel # 10");

Monkey monk;

HashSet names = new HashSet();
names.add("Jimmy");
names.add("Biff");
names.add("Skip");

Iterator nameIter = names.iterator();

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Set monkeyGroup = new HashSet();

while (nameIter.hasNext()) {
monk = new Monkey((String)nameIter.next(), 12);
session.save(monk);
monkeyGroup.add(monk);
}


theBarrel.setContents(monkeyGroup);

session.save(theBarrel);

tx.commit();
session.close();

// Get barrel from database

session = sessionFactory.openSession();
tx = session.beginTransaction();

List result = session.find("from barrels in class src.Barrel");
Iterator resultItr = result.iterator();

Barrel storBarrel = null;
Iterator monkeyGroupItr;

// Only 1 Barrel so this is silly...

while (resultItr.hasNext()) {
Barrel storedBarrel = (Barrel)resultItr.next();
storBarrel = storedBarrel;
}
tx.commit();
session.close();



Barrel copyBarrel = (Barrel)SerializationHelper.clone(storBarrel);
session = sessionFactory.openSession();
tx = session.beginTransaction();
copyBarrel.setLabel("Barrel # 20");

session.saveOrUpdateCopy(copyBarrel);

tx.commit();
session.close();


A little debug output:
DEBUG - Starting clone through serialization
DEBUG - Starting serialization of object [src.Barrel@1a62c31]
DEBUG - Starting deserialization of object
DEBUG - Attempting to locate class [src.Barrel]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [net.sf.hibernate.collection.Set]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [net.sf.hibernate.collection.ODMGCollection]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [net.sf.hibernate.collection.PersistentCollection]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [net.sf.hibernate.impl.SessionImpl$CollectionEntry]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [java.lang.Integer]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [java.lang.Number]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [java.util.HashMap]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [src.Monkey]
DEBUG - Class resolved through context class loader
DEBUG - Attempting to locate class [java.util.HashSet]
DEBUG - Class resolved through context class loader
DEBUG - opened session
DEBUG - begin
DEBUG - total checked-out connections: 0
DEBUG - using pooled JDBC connection, pool size: 0
DEBUG - current autocommit status:false
DEBUG - id unsaved-value strategy NULL
DEBUG - loading [src.Barrel#0]
DEBUG - attempting to resolve [src.Barrel#0]
DEBUG - object not resolved in any cache [src.Barrel#0]
DEBUG - Materializing entity: [src.Barrel#0]
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - select barrel0_.barrel_id as barrel_id0_, barrel0_.label as label0_ from barrels barrel0_ where barrel0_.barrel_id=?
Hibernate: select barrel0_.barrel_id as barrel_id0_, barrel0_.label as label0_ from barrels barrel0_ where barrel0_.barrel_id=?
DEBUG - preparing statement
DEBUG - binding '0' to parameter: 1
DEBUG - processing result set
DEBUG - done processing result set (0 rows)
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - total objects hydrated: 0
DEBUG - initializing non-lazy collections
DEBUG - generated identifier: 2
DEBUG - saving [src.Barrel#2]
DEBUG - processing cascades for: src.Barrel
DEBUG - done processing cascades for: src.Barrel
DEBUG - processing cascades for: src.Barrel
DEBUG - cascading to collection: src.Barrel.contents
DEBUG - cascading to copy()
DEBUG - id unsaved-value strategy NULL
DEBUG - loading [src.Monkey#0]
DEBUG - attempting to resolve [src.Monkey#0]
DEBUG - object not resolved in any cache [src.Monkey#0]
DEBUG - Materializing entity: [src.Monkey#0]
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - select monkey0_.monkey_id as monkey_id0_, monkey0_.name as name0_, monkey0_.age as age0_ from monkeys monkey0_ where monkey0_.monkey_id=?
Hibernate: select monkey0_.monkey_id as monkey_id0_, monkey0_.name as name0_, monkey0_.age as age0_ from monkeys monkey0_ where monkey0_.monkey_id=?
DEBUG - preparing statement
DEBUG - binding '0' to parameter: 1
DEBUG - processing result set
DEBUG - done processing result set (0 rows)
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - total objects hydrated: 0
DEBUG - initializing non-lazy collections
DEBUG - generated identifier: 4
DEBUG - saving [src.Monkey#4]
DEBUG - cascading to copy()
DEBUG - id unsaved-value strategy NULL
DEBUG - loading [src.Monkey#0]
DEBUG - attempting to resolve [src.Monkey#0]
DEBUG - entity does not exist
DEBUG - generated identifier: 5
DEBUG - saving [src.Monkey#5]
DEBUG - cascading to copy()
DEBUG - id unsaved-value strategy NULL
DEBUG - loading [src.Monkey#0]
DEBUG - attempting to resolve [src.Monkey#0]
DEBUG - entity does not exist
DEBUG - generated identifier: 6
DEBUG - saving [src.Monkey#6]
DEBUG - done processing cascades for: src.Barrel
DEBUG - commit
DEBUG - flushing session
DEBUG - processing cascades for: src.Barrel
DEBUG - cascading to collection: src.Barrel.contents
DEBUG - cascading to saveOrUpdate()
DEBUG - saveOrUpdate() persistent instance
DEBUG - cascading to saveOrUpdate()
DEBUG - saveOrUpdate() persistent instance
DEBUG - cascading to saveOrUpdate()
DEBUG - saveOrUpdate() persistent instance
DEBUG - done processing cascades for: src.Barrel
DEBUG - Collection dirty: [src.Barrel.contents#1]
DEBUG - Flushing entities and processing referenced collections
DEBUG - Collection found: [src.Barrel.contents#2], was: [src.Barrel.contents#1]
DEBUG - Forcing collection initialization
DEBUG - Processing unreferenced collections
DEBUG - Scheduling collection removes/(re)creates/updates
DEBUG - Flushed: 4 insertions, 0 updates, 0 deletions to 4 objects
DEBUG - Flushed: 1 (re)creations, 0 updates, 1 removals to 1 collections
DEBUG - listing entities:
DEBUG - src.Monkey{age=12, name=Skip, id=5}
DEBUG - src.Monkey{age=12, name=Jimmy, id=6}
DEBUG - src.Barrel{contents=[Monkey#4, Monkey#5, Monkey#6], label=Barrel # 20, id=2}
DEBUG - src.Monkey{age=12, name=Biff, id=4}
DEBUG - executing flush
DEBUG - Inserting entity: [src.Barrel#2]
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - insert into barrels (label, barrel_id) values (?, ?)
Hibernate: insert into barrels (label, barrel_id) values (?, ?)
DEBUG - preparing statement
DEBUG - Dehydrating entity: [src.Barrel#2]
DEBUG - binding 'Barrel # 20' to parameter: 1
DEBUG - binding '2' to parameter: 2
DEBUG - Adding to batch
DEBUG - Inserting entity: [src.Monkey#4]
DEBUG - Executing batch size: 1
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - insert into monkeys (name, age, monkey_id) values (?, ?, ?)
Hibernate: insert into monkeys (name, age, monkey_id) values (?, ?, ?)
DEBUG - preparing statement
DEBUG - Dehydrating entity: [src.Monkey#4]
DEBUG - binding 'Biff' to parameter: 1
DEBUG - binding '12' to parameter: 2
DEBUG - binding '4' to parameter: 3
DEBUG - Adding to batch
DEBUG - Inserting entity: [src.Monkey#5]
DEBUG - reusing prepared statement
DEBUG - insert into monkeys (name, age, monkey_id) values (?, ?, ?)
Hibernate: insert into monkeys (name, age, monkey_id) values (?, ?, ?)
DEBUG - Dehydrating entity: [src.Monkey#5]
DEBUG - binding 'Skip' to parameter: 1
DEBUG - binding '12' to parameter: 2
DEBUG - binding '5' to parameter: 3
DEBUG - Adding to batch
DEBUG - Inserting entity: [src.Monkey#6]
DEBUG - reusing prepared statement
DEBUG - insert into monkeys (name, age, monkey_id) values (?, ?, ?)
Hibernate: insert into monkeys (name, age, monkey_id) values (?, ?, ?)
DEBUG - Dehydrating entity: [src.Monkey#6]
DEBUG - binding 'Jimmy' to parameter: 1
DEBUG - binding '12' to parameter: 2
DEBUG - binding '6' to parameter: 3
DEBUG - Adding to batch
DEBUG - Executing batch size: 3
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - Deleting collection: [src.Barrel.contents#1]
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - update monkeys set barrel_id=null where barrel_id=?
Hibernate: update monkeys set barrel_id=null where barrel_id=?
DEBUG - preparing statement
DEBUG - binding '1' to parameter: 1
DEBUG - Adding to batch
DEBUG - done deleting collection
DEBUG - Executing batch size: 1
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - Inserting collection: [src.Barrel.contents#2]
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - update monkeys set barrel_id=? where monkey_id=?
Hibernate: update monkeys set barrel_id=? where monkey_id=?
DEBUG - preparing statement
DEBUG - binding '2' to parameter: 1
DEBUG - binding '4' to parameter: 2
DEBUG - Adding to batch
DEBUG - reusing prepared statement
DEBUG - update monkeys set barrel_id=? where monkey_id=?
Hibernate: update monkeys set barrel_id=? where monkey_id=?
DEBUG - binding '2' to parameter: 1
DEBUG - binding '5' to parameter: 2
DEBUG - Adding to batch
DEBUG - reusing prepared statement
DEBUG - update monkeys set barrel_id=? where monkey_id=?
Hibernate: update monkeys set barrel_id=? where monkey_id=?
DEBUG - binding '2' to parameter: 1
DEBUG - binding '6' to parameter: 2
DEBUG - Adding to batch
DEBUG - done inserting collection: 3 rows inserted
DEBUG - Executing batch size: 3
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - post flush
DEBUG - transaction completion
DEBUG - closing session
DEBUG - disconnecting session
DEBUG - returning connection to pool, pool size: 1
DEBUG - transaction completion
Finished!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 8:35 am 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
"beanlib" should solve this cloning problem for you:
http://hansonchar.blogspot.com/2005/06/ ... rnate.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.