I found code for this example (see
Item.java and
Bid.java) and have isolated the strange behaviour further.
To-0/1 works but To-1 does notThe following mapping in class Item,
Code:
@OneToMany(mappedBy = "item")
private final Collection<Bid> bids = new ArrayList<Bid>();
@ManyToOne
@JoinColumn(nullable = true)
private Bid successfulBid;
makes the code work. However, I have a slightly different use case in which there always is and must be a successfulBid. So I set @JoinColumn(nullable = false) in the above. With this, I get an exception
not-null property references a null or transient value: org.hbf.basics.hibernate.HibernateTest$Item.successfulBid
Does anybody know why?
I also get this exception when I set @ManyToOne(optional=false).
Cascading does not workSecondly, I want to use cascading and enable it on both (class Item):
Code:
@OneToMany(mappedBy = "item", cascade = {
CascadeType.ALL
})
private final Collection<Bid> bids = new ArrayList<Bid>();
@ManyToOne(cascade = {
CascadeType.ALL
})
@JoinColumn(nullable = true)
private Bid successfulBid;
And in bidsTest() I disable the save of the child:
Code:
session.save(item);
//session.save(bid);
With this, I get
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.hbf.basics.hibernate.HibernateTest$Bid
Any hints much appreciated,
Hbf