I am new to Hibernate and trying to get a OneToOne annotation to work. I've looked at lots of examples, other threads, and the documentation and can not figure out what I am doing wrong here. As far as I can tell mine is identical to some of the example's I've seen.
Table: loan (Owner)
Code:
id int primary key
(Other columns ommitted)
Table: loanPaymentInfo
Code:
loanId int primary key references loan(id)
Code:
@Entity
public class Loan{
@Id
private Long id;
@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="loan")
@JoinColumn(name="id")
private LoanPaymentInfo paymentInfo;
//paymentInfo getter (no setter)
}
@Entity LoanPaymentInfo{
@Id
@GeneratedValue(generator="foreign")
@GenericGenerator(name="foreign", strategy = "foreign", parameters={
@Parameter(name="property", value="loan")
})
private Long loanId;
@OneToOne(fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
private Loan loan;
//loan getter (no setter)
}
When I create a loan and set some properties on loan and loanPaymentInfo, I get the following exception:
Code:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.mlikesit.trading.p2p.db.entity.loan.LoanPaymentInfo#1]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1950)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2594)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2494)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2821)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.mlikesit.trading.p2p.db.dao.HibernateUtil.commitTransaction(HibernateUtil.java:200)
at com.mlikesit.trading.p2p.db.dao.HibernateUtil.inTransaction(HibernateUtil.java:228)
at com.mlikesit.trading.p2p.db.dao.HibernateUtil.save(HibernateUtil.java:140)
at com.mlikesit.trading.p2p.db.entity.loan.LoanTest.test(LoanTest.java:31)
more...
Thank you in advance for any help.