-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problem using serialized/deserialized objects
PostPosted: Fri Feb 27, 2004 1:12 pm 
Newbie

Joined: Fri Feb 27, 2004 12:53 pm
Posts: 4
Hi,

I'm using Hibernate 2.1.1 and HSQL DB.
I have 2 objects related one-many relationship.
I serialize these objects in a file and after that try to deserialize from that file and save them in DB. I also use my own class generating IDs for that objects. If I use simple object (not related) it works fine, but when I use relation objects it fails... here is descripiton of the exception:

2004-02-27 18:44:29,669 ERROR [net.sf.hibernate.impl.SessionImpl] Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)


Can anyone help me

Thx,
Niki


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 6:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Gonna need a little more information. Just take a look on the post message screen for a list of things that would help diagnose your problem


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 4:34 am 
Newbie

Joined: Fri Feb 27, 2004 12:53 pm
Posts: 4
First I want to briefely describe why I use serialization:

I want to copy some data from one database to other(something like replication). I have query which returns me Vector of objects (related objects - ClassBean and StudentBean see mapping doc. below). I serialize that Vector using ObjectOutputStream and ZipOutputStream (to ZIP the file) into binary file. In the other side(other database) I read the binaty file get the Vector objest, iterate it and save all objects in DB. The problem I think comes from that first I save Parent related object(ClassBean) and after that child related objects again(StudentBean)....
If anyone can offer me how to make this better please tell me...

here is info that I colud give you:

Hibernate version 2.1.1
mapping document:
<?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="com.cosmos.apps.nwr.beans.objects.DataObjectIdentifier" table="t_Objects" dynamic-update="true">
<id name="objectID" column="Object_ID" type="string" length="56">
<generator class="assigned"/>
</id>
<property name="createdDate" column="Create_date" type="timestamp" not-null="true"/>
<property name="ownerName" type="string" length="64" column="owner_name">
<column name="Owner_Name" not-null="true" sql-type="NVARCHAR(64)"/>
</property>
<property name="isLink" column="Is_Link" type="boolean" not-null="true"/>
<property name="isDeleted" column="Is_Deleted" type="boolean" not-null="true"/>
<property name="linkedID" column="Linked_ID" type="string" not-null="false"/>
<many-to-one name="objectType" class="com.cosmos.apps.nwr.beans.objects.ObjectTypesBean" column="Object_Type_ID"/>
</class>

<class name="com.cosmos.apps.nwr.beans.objects.ObjectTypesBean" table="t_Object_Types">
<id name="id" column="Object_Type_Id" type="int" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="typeName" type="string" length="64" column="Type_Name"/>
<property name="className" type="string" length="64" column="Class_Name"/>
<property name="counterValue" type="long" column="Counter_Value"/>
<property name="description" type="string" length="1024" column="Description"/>
</class>

<class name="test.ClassBean" table="t_Classes">
<id name="id" column="Class_ID" type="string" length="56">
<generator class="com.cosmos.apps.nwr.hibernate.DOIGenerator"/>
</id>
<property name="name" column="Name" type="string" not-null="true"/>
<set name="students" cascade="all" inverse="true">
<key column="Class_ID"/>
<one-to-many class="test.StudentBean"/>
</set>
</class>

<class name="test.StudentBean" table="t_Students">
<id name="id" column="Student_ID" type="string" length="56">
<generator class="com.cosmos.apps.nwr.hibernate.DOIGenerator"/>
</id>
<property name="name" column="Name" type="string" not-null="true"/>
<many-to-one name="classbean" class="test.ClassBean" column="Class_ID"/>
</class>

</hibernate-mapping>


this is the code for reading objects from binary file and save them:

ZipInputStream zin = new ZipInputStream(new FileInputStream("transfer.zip"));
// Get the first entry
ZipEntry entry = zin.getNextEntry();
System.out.println("entry.getName() " + entry.getName());
ObjectInputStream in = new ObjectInputStream(zin);
Collection all = (Collection)in.readObject();

Iterator iter = all.iterator();
while (iter.hasNext())
{
Object item = (Object)iter.next();

ses.save(item);
}

ses.flush();
ses.connection().commit();
}


this is full stack trace:


2004-03-01 10:27:56,840 ERROR [net.sf.hibernate.impl.SessionImpl] Could not synchronize database state with session

net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
...........
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)

at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)

at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:672)

at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java: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 TestSerialisation.extractFromZip(TestSerialisation.java:93)

at TestSerialisation.main(TestSerialisation.java:134)


database name - hsqldb.original_version=1.7.1


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 4:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This is surely because of your use of cascade with an exotic combination of "assigned", "identity" and a custom generator. Cascade has to determine somehow if the object has to be saved or updated. Search the forum for the various discussions about "unsaved-value" for your options.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 5:14 am 
Newbie

Joined: Fri Feb 27, 2004 12:53 pm
Posts: 4
hi michael,

as reply of your message I must add that in my ID Generator I check if the object have already ID(in case of trasferring data between databases)
and then just save it in tha database without generating ID!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 6:14 am 
Newbie

Joined: Fri Feb 27, 2004 12:53 pm
Posts: 4
Thx Michael It works,

I add unsaved-value="any" attribute to ClassBean and StudentBean and now objects are saved to the database!
I hope I wont have any problems in more complex relations!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 6:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Note that with unsaved-value=any, the objects will always get saved, never updated during cascades.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 1:42 am 
Newbie

Joined: Tue Mar 23, 2004 1:04 am
Posts: 3
Hi,

I have little problem with serialization/deserialization PersistenCollections.
I have same functionality as described in posts above in this thread. I mean import/export db objects from/into binary file. But I hane another problem:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.enterra.surv.po.Survey" table="evote_survey" lazy="false">
<cache usage="read-write"/>

<id name="id" column="f_survey_id" type="int" unsaved-value="0">
<generator class="com.enterra.surv.system.IdGenerator"/>
</id>

<many-to-one name="creator" class="com.enterra.surv.po.User" column="f_owner" not-null="true"/>

<many-to-one name="style" class="com.enterra.surv.po.Style" column="f_style_id" not-null="false"/>

<property name="name" type="java.lang.String" column="f_name" not-null="true"/>

...
<bag name="pages" cascade="all" inverse="true" lazy="true" table="evote_page" order-by="f_order" where="f_archived=0">
<cache usage="read-write"/>
<key column="f_survey_id"/>
<one-to-many class="com.enterra.surv.po.Page"/>
</bag>

...

</class>
</hibernate-mapping>

As you can see, I use lazy collection initialization. When I try to serialize the Survey, this collection dont serialized. And after deserialization I have:

Failed to lazily initialize a collection - no session or session was closed
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:214)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Bag.size(Bag.java:232)
at com.enterra.surv.test.ImportExportTest.main(ImportExportTest.java:31)

But if lazy=false everything works perfect. Survey's serialization and deserialization works.

Is where any way to avoid this problem and leave lazy=true?
Thank you for your perfect Hibernate framework?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 1:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You need to lock() the entity in a new session. please search the forum for LOTS of similar problems.


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