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