我有兩個實體。 體育:JPA(EclipseLink)支持一對多的關係沒有Cascade.PERSIST
@Entity
public class Sports implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "SPORT_NAME")
private String name;
...
}
和SportFacility:
@Entity
public class SportFacility implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "SPORTFAC_NAME")
private String name;
@OneToMany
private List<Sports> sports;
....
}
然後,我有一些創建體育在體育表,例如:
--id-----name-----
- 0 - Football -
- 1 - Basketball-
- 2 - Golf -
------------------
,我需要創建一些運動體育設施。讓我們參加所有運動
List<Sports> listSports = sb.getAllSports(); //JPQL: SELECT s FROM Sports s
SportFacility sportFacility = new SportFacility("Stadium", listSports);
em.getTransaction().begin();
em.persist(sportFacility);
em.getTransaction().commit();
錯誤:
java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: database.entities.sports[ id=0 ].
我不希望使用Cascade.ALL或者持續becase的有表只有很少的體育活動,並一直不需要用相同的運動創造新的紀錄...... 謝謝。