-->
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: not-null property references a null or transient value
PostPosted: Wed Oct 20, 2010 5:19 am 
Newbie

Joined: Sat Dec 01, 2007 7:33 am
Posts: 8
Hi,
I have many-to-one association betwen BOOK and PUBLISHER entities.
when i try to save the association a hibrnate exception appear :

org.hibernate.PropertyValueException: not-null property references a null or transient value: tn.com.smartsoft.hibernate.beans.Book.publisher



PUBLISHER mapping file :

<!-- Publisher mapping -->
<hibernate-mapping>
<class name="tn.com.smartsoft.hibernate.beans.Publisher" table="publisher" catalog="bookshopdb">
<id name="publisherId" type="java.lang.Long">
<column name="PUBLISHER__ID" default="100" />
<generator class="increment" />
</id>
<property name="code" type="java.lang.String">
<column name="CODE" length="25" not-null="true" unique="true" />
</property>
<property name="publisherName" type="java.lang.String">
<column name="PUBLISHER_NAME" length="25" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="75" />
</property>

<set name="books" inverse="true" cascade="all">
<key>
<column name="PUBLICHER_ID" />
</key>
<one-to-many class="tn.com.smartsoft.hibernate.beans.Book" />
</set>


</class>


BOOK mapping file :


<hibernate-mapping>
<class name="tn.com.smartsoft.hibernate.beans.Book" table="book" catalog="bookshopdb">
<id name="bookId" type="java.lang.Long">
<column name="BOOK_ID" />
<generator class="native" />
</id>
<property name="isbn" type="java.lang.String">
<column name="ISBN" length="20" not-null="true" unique="true" />
</property>
<property name="bookName" type="java.lang.String">
<column name="BOOK_NAME" length="225" />
</property>
<property name="publishDate" type="java.util.Date">
<column name="PUBLISH_DATE" length="10" />
</property>
<property name="price" type="java.lang.Long">
<column name="PRICE" precision="10" scale="0" />
</property>
<many-to-one name="publisher" class="tn.com.smartsoft.hibernate.beans.Publisher" lazy="proxy" cascade="save-update" fetch="join">
<column name="PUBLICHER_ID" not-null="true" />
</many-to-one>

<set name="chapters" inverse="true">
<key>
<column name="BOOK_ID" not-null="true" />
</key>
<one-to-many class="tn.com.smartsoft.hibernate.beans.Chapter" />
</set>
</class>
</hibernate-mapping>



i have tried to execute the following code but a hibernate exception was thrown:

Book book = new Book();
book.setBookName("My BOOK1");
book.setIsbn("123456");
book.setPrice(Long.valueOf(10));

Publisher publisher = new Publisher();
publisher.setCode("18");
publisher.setPublisherName("Publisher1");
book.setPublisher(publisher);

Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(book);
tx.commit();
session.close();

the Hibernate Exception

org.hibernate.PropertyValueException: not-null property references a null or transient value: tn.com.smartsoft.hibernate.beans.Book.publisher


regards,


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Wed Oct 20, 2010 5:28 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Try calling "saveOrUpdate" on publisher instead of book.


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Wed Oct 20, 2010 5:37 am 
Newbie

Joined: Sat Dec 01, 2007 7:33 am
Posts: 8
thank you NEWBIE for response
i have tried to saveOrUpdate(publisher) instate of book
a new row is added in the publisher table but book entity was not inserted in database.

s you can see hibernate generate the following queries :

Hibernate: select max(PUBLISHER__ID) from publisher
Hibernate: insert into bookshopdb.publisher (CODE, PUBLISHER_NAME, ADDRESS, PUBLISHER__ID) values (?, ?, ?, ?)


regards,


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Wed Oct 20, 2010 5:42 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Also add the following:

Code:
if (publisher.getBooks() == null) {
    publisher.setBooks(new HashSet());
}
publisher.getBooks().add(book);


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Wed Oct 20, 2010 5:56 am 
Newbie

Joined: Sat Dec 01, 2007 7:33 am
Posts: 8
thank you Newbie it work corectly now!


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Wed Oct 20, 2010 6:58 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
zaghdoudi wrote:
thank you Newbie it work corectly now!


By the way, I am r, not newbie :)


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Wed Oct 20, 2010 8:04 am 
Newbie

Joined: Sat Dec 01, 2007 7:33 am
Posts: 8
so sorry thank agin you r.
it's clear you aren't a Newbie because you have an advance knowledge
regards,


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.