我有两个实体。运动的:
@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;
...
}
和运动设施:
@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;
....
}
然后我在 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 或 PERSIST,因为表中的运动很少,而且我总是不需要用相同的运动创建新记录...谢谢。