JPA/Hibernate:传递给持久化的分离实体
@Entitypublic class Transaction { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @ManyToOne(cascade = {CascadeType.ALL},fetch= FetchType.EAGER) private Account fromAccount;....@Entitypublic class Account { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @OneToMany(cascade = {CascadeType.ALL},fetch= FetchType.EAGER, mappedBy = "fromAccount") private Set<Transaction> transactions;
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.paulsanwald.Account at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
if (account.getId()!=null) { account = entityManager.merge(account);}Transaction transaction = new Transaction(account,"other stuff"); // the below fails with a "detached entity" message. why?entityManager.persist(transaction);
红糖糍粑
不负相思意
jeck猫
相关分类