-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate - Having trouble updating a Collection
PostPosted: Wed Sep 21, 2005 9:16 am 
Newbie

Joined: Wed Sep 21, 2005 9:07 am
Posts: 3
Hi

I'm having trouble, please help. I have a document object that contains a Set of Note Objects (a one to many relationship). Each object refers to the relevant table. However, when I edit the document object and add a new Note to the collection and try to update my changes, I get the following exception:

Hibernate Exceptionobject references an unsaved transient instance - save the transient instance before flushing: beans.Note

I need the new Note object in the collection to be saved along with any changes to the Document Bean object. Can anyone help me?


Cheers
Faisal
-----
My Mappings are:

1) DocumentBean

<hibernate-mapping>
<class name="beans.DocumentBean" table="document">
<id name="docId" unsaved-value="0" type="long">
<generator class="native" />
</id>
<property name="status" type="string" />
<property name="docType" type="string" />
<property name="fileName" type="string" />
<property name="lastModified" type="string" />
<property name="userId" column="memno" type="string" />
<property name="mimeType" type="string" />
<property name="fileContents" type="binary" />
<set name="notes" table="note" lazy="true" cascade="none">
<key>
<column name="docId" not-null="false" />
</key>
<one-to-many class="beans.Note" />
</set>
</class>


</hibernate-mapping>

2) Note


<hibernate-mapping>
<class name="beans.Note" table="note">
<id name="noteId" unsaved-value="0">
<generator class="native" />
</id>
<property name="text" />
<property name="username" column="username" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Hibernate - Having trouble updating a Collection
PostPosted: Wed Sep 21, 2005 9:53 am 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
change cascade="none" to cascade="save-update" or cascade="all"

zargarf wrote:
Hi

I'm having trouble, please help. I have a document object that contains a Set of Note Objects (a one to many relationship). Each object refers to the relevant table. However, when I edit the document object and add a new Note to the collection and try to update my changes, I get the following exception:

Hibernate Exceptionobject references an unsaved transient instance - save the transient instance before flushing: beans.Note

I need the new Note object in the collection to be saved along with any changes to the Document Bean object. Can anyone help me?


Cheers
Faisal
-----
My Mappings are:

1) DocumentBean

<hibernate-mapping>
<class name="beans.DocumentBean" table="document">
<id name="docId" unsaved-value="0" type="long">
<generator class="native" />
</id>
<property name="status" type="string" />
<property name="docType" type="string" />
<property name="fileName" type="string" />
<property name="lastModified" type="string" />
<property name="userId" column="memno" type="string" />
<property name="mimeType" type="string" />
<property name="fileContents" type="binary" />
<set name="notes" table="note" lazy="true" cascade="none">
<key>
<column name="docId" not-null="false" />
</key>
<one-to-many class="beans.Note" />
</set>
</class>


</hibernate-mapping>

2) Note


<hibernate-mapping>
<class name="beans.Note" table="note">
<id name="noteId" unsaved-value="0">
<generator class="native" />
</id>
<property name="text" />
<property name="username" column="username" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 9:55 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Quote:
<set name="notes" table="note" lazy="true" cascade="none">


Use cascade="all-delete-orphan"
[/quote]


Top
 Profile  
 
 Post subject: Still having problems
PostPosted: Thu Sep 29, 2005 11:32 am 
Newbie

Joined: Wed Sep 21, 2005 9:07 am
Posts: 3
Hi, I'm still having problems with the mappings shown. The problem is because the foreign key in the Note table is not allowed to be null (as it makes no sense to have a note without a valid parent). However this is causing constraint issues with the cascading suggested.

If I instead make the foreign key nullable and allow the note object to have an instance of the DocumentBean parent, the foreign key (docId) in the note table is blank when update is called on the DocumentBean parent.

I need to be able to save changes to the DocumentBean, including the addition of a Note Child in the Set correctly and it's driving me crazy. Please help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 12:35 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
post the code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:32 am 
Newbie

Joined: Wed Sep 21, 2005 9:07 am
Posts: 3
The DB Tables

1) DOCUMENT

CREATE TABLE "DB2ADMIN"."DOCUMENT" (
"DOCID" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (
START WITH +1
INCREMENT BY +1
MINVALUE +1
MAXVALUE +9223372036854775807
NO CYCLE
NO CACHE
NO ORDER ) ,
"DOCTYPE" VARCHAR(256) NOT NULL ,
"STATUS" VARCHAR(256) NOT NULL ,
"FILENAME" VARCHAR(256) NOT NULL ,
"FILECONTENTS" BLOB(5242880) LOGGED NOT COMPACT ,
"MIMETYPE" VARCHAR(256) ,
"LASTMODIFIED" VARCHAR(256) ,
"MEMNO" VARCHAR(50) NOT NULL )
IN "USERSPACE1" ;

-- DDL Statements for primary key on Table "DB2ADMIN"."DOCUMENT"

ALTER TABLE "DB2ADMIN"."DOCUMENT"
ADD CONSTRAINT "CC1125661572210" PRIMARY KEY
("DOCID");

2) NOTE

CREATE TABLE "DB2ADMIN"."NOTE" (
"NOTEID" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (
START WITH +1
INCREMENT BY +1
MINVALUE +1
MAXVALUE +9223372036854775807
NO CYCLE
NO CACHE
NO ORDER ) ,
"DOCID" BIGINT ,
"TEXT" VARCHAR(256) NOT NULL ,
"USERNAME" VARCHAR(10) )
IN "USERSPACE1" ;

-- DDL Statements for primary key on Table "DB2ADMIN"."NOTE"

ALTER TABLE "DB2ADMIN"."NOTE"
ADD CONSTRAINT "CC1125661313632" PRIMARY KEY
("NOTEID");

-- DDL Statements for foreign keys on Table "DB2ADMIN"."NOTE"

ALTER TABLE "DB2ADMIN"."NOTE"
ADD CONSTRAINT "CC1127941822406" FOREIGN KEY
("DOCID")
REFERENCES "DB2ADMIN"."NOTE"
("NOTEID")


The Beans


1) DOCUMENT


public class DocumentBean implements BeanCastInterface
{
private long docId;
private String docType;
private String lastModified;
private String status;
private String fileName;
private String userId;
private byte[] fileContents;
private Set notes;
private String mimeType;

...

2) NOTE

public class Note implements BeanCastInterface
{
private long noteId;
private String text;
private String username;
private DocumentBean document;

....

The Mappings


1) DOCUMENT


<hibernate-mapping>
<class name="beans.DocumentBean" table="document">
<id name="docId" unsaved-value="0" type="long">
<generator class="native" />
</id>
<property name="status" type="string"/>
<property name="docType" type="string"/>
<property name="fileName" type="string" />
<property name="lastModified" type="string"/>
<property name="userId" column="memno" type="string"/>
<property name="mimeType" type="string"/>
<property name="fileContents" type="binary"/>
<set name="notes" table="note" inverse="true" lazy="true" cascade="all-delete-orphan">
<key>
<column name="docId" not-null="true"/>
</key>
<one-to-many class="beans.Note" />
</set>



</class>


</hibernate-mapping>

2) NOTE


<hibernate-mapping>
<class name="beans.Note" table="note">
<id name="noteId" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="document" class="beans.DocumentBean" column="docid" not-null="true"/>
<property name="text"/>
<property name="username" column="username"/>
</class>
</hibernate-mapping>


The code for the Update

1) Retrieve DocumentBean from Hibernate Session
DocumentBean d = HibernateService.getInstance().getDocument(new Long(sessionBean.getDocument().getDocId()));
2) Change a couple of properties on the bean according to form submission
3) Retrieve notes Set from the DocumentBean and add the new Note (based on a form param)


Note n = new Note();
n.setText(newNote);
n.setUsername(username);
n.setNoteId(0);
n.setDocument(d);
d.getNotes().add(n);
HibernateService.getInstance().updateDocument(d);

The problems

Hi, I'm still having problems with the mappings shown. The problem is because the foreign key in the Note table is not allowed to be null (as it makes no sense to have a note without a valid parent). However this is causing constraint issues with the cascading suggested.

If I instead make the foreign key nullable and allow the note object to have an instance of the DocumentBean parent, the foreign key (docId) in the note table is blank when update is called on the DocumentBean parent.

I need to be able to save changes to the DocumentBean, including the addition of a Note Child in the Set correctly and it's driving me crazy. Please help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 10:00 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
try after removing unsaved-value="0"


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