我有两个实体和这些实体之间的oneToOne关系。 第一个:
@Entity
public class B{
@OneToOne(targetEntity = A.class, cascade = CascadeType.ALL)
@JoinColumn(name = "aId", columnDefinition = "VARCHAR(50)", referencedColumnName = "aId", insertable = true, updatable = true, nullable = false)
private A a;
.....
}
另一个:
@Entity
public class A{
@OneToOne(mappedBy = "a", targetEntity = B.class, fetch = FetchType.LAZY)
private B b;
}
我尝试用JAX-RS读取json'文件并获得异常:
{
"a": {
"name": "my name"
"b": {
"attribut": "my attribut...."
}
}
我的控制器
@SuppressWarnings({ "resource" })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/send")
public Response post(MyRoot myRoot) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(D.class);
AutowireCapableBeanFactory acbFactory = applicationContext
.getAutowireCapableBeanFactory();
acbFactory.autowireBean(this);
a = myRoot.getA();
em.persist(a);
b = a.getB();
em.persist(b);
return Response.status(200).entity(myRoot).build();
}
MyRoot返回A-Entity。 我得到了这个例外:
Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : B.a -> A
at org.hibernate.engine.spi.CascadingActions$8.noCascade(CascadingActions.java:398)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:129)
at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:167)
at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:158)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:91)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
... 52 more
任何想法?