简体   繁体   中英

Hibernate: CascadeType.ALL not saving all entities because of PERSIST on OneToOne relation

Using the properties below:

@Entity
@Table(name = "b")
public class B extends AbstractEntity implements Serializable, Comparable<B>, Cloneable {
  ...
  @JsonBackReference
  @OneToOne
  @JoinColumn(name ="a_id")
  @Cascade( value = { CascadeType.ALL } )
  private A an;
}

@Entity
@Table(name = "a")
public class A implements Serializable{
  @Id
  @Column(name="a_id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @OneToOne(mappedBy = "a", fetch = FetchType.LAZY, orphanRemoval = true)
  private B b;
}

it saves only the first entity, after that it returns this error for the other ones:

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: A; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: A

I'm not familiar with PERSIST. I don't know if using only CascadeType.MERGE or CascadeType.SAVE_UPDATE would solve the problem, I mean they worked, but I'd rather know how to fix the persistance.

How do you try to save or entities ? By calling "SaveOrUpdate" method or "Persist" ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM