-->
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.  [ 4 posts ] 
Author Message
 Post subject: Persist problem on parent relationship entity
PostPosted: Fri Aug 29, 2008 6:22 am 
Newbie

Joined: Tue Oct 02, 2007 3:49 am
Posts: 17
Dear All,

I get stuck on this persist problem for a few day. Hope some one can help me here. I'd a class "BorrowOut" to keep tracking which books have been borrowed Out. Since it's a unidirection relationship, I'm not necesary to use the book to get the borrowOut information. I design my models as follow.

In entity BorrowOut

Code:
@Entity
@Table(name = "borrow")
public class BorrowOut extends ModelBase implements Serializable {
   @Id
   @Column(name = "id", nullable = false)
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @Temporal(TemporalType.DATE)
   @Column(name = "date", nullable = false)
   private Date date = new Date();

   @ManyToOne(cascade = CascadeType.PERSIST)
   @JoinColumn(name = "book_id")
   private Book book;


In entity Book
Code:
@Entity
@Table(name = "book")
public class Book implements Serializable {
   @Id
   @Column(name = "book_id", length = 10, unique = true)
   private String bookId;

   @Column(name = "name", length = 50)
                private String name;


User will choose a book by its name which they want to borrow out. Once they submit, my program will get the book object using the name by createQuery and then I'll will assign it to the new created borrowOut object. Lastly, I'll persist the borrowOut object. My problem is it will insert the chosen book into the table "Book" again. Thus it violate the unique constraint. I'm using extended persistent context. Suppose it will check my book object which is transient or not. I also tried to take out the CascadeType.PERSIST as blank type. However, it state that my BorrowOut object references an unsaved transient instance - save the transient instance before flushing and that instance is my book object. That's drive me mad. I just simply fetch the book object by its name and assign it to BorrowOut record. It should not generate insert or update sql syntax on the table book. I'm frustrated on this. Hope someone can help me out.

Best Rdgs
Ellis


Top
 Profile  
 
 Post subject: Re: Persist problem on parent relationship entity
PostPosted: Fri Aug 29, 2008 11:59 am 
Newbie

Joined: Thu Aug 28, 2008 3:31 am
Posts: 5
I think you can use session.merge() instead persist. I'm not sure of this, but you can have a try.


Top
 Profile  
 
 Post subject: re: RSS Feed Hibernate Forums Index -> Hibernate Users
PostPosted: Fri Aug 29, 2008 12:52 pm 
Newbie

Joined: Fri Aug 29, 2008 9:07 am
Posts: 16
Location: edinburgh/zielona gora
so there words this is not working??:

Code:

// assuming your select will always return one book
Book b =  (Book)session.createQuery("from  Book where name=:name").setString("name","your name").uniqueResult() ;

BorrowOut bo = new BorrowOut();
bo.setDate(date);
bo.setBool(b);

session.save(bo);

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 10:00 pm 
Newbie

Joined: Tue Oct 02, 2007 3:49 am
Posts: 17
I'm using JPA in my program. I'm doing same thing as you mentioned. Only difference is I'm using entitymanager instead of session.

Code:
Book b =  (Book)em.createQuery("from  Book where name=:name").setString("name","your name").uniqueResult() ;

BorrowOut bo = new BorrowOut();
bo.setDate(date);
bo.setBool(b);

em.persist(bo);


When the program run in the line em.persist, I can see from my console that the insert statement generated on both of my tables BorrowOut and Book also. Then I get the uniqueContraint error in that case. If I also tried to assign the CascadeType to blank or "Merge" and "Persist" together. PropertyField error occur and state that my BorrowOut record is reference to null or transient value. That's really make me headache by this error. :(


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