从级联删除连接表与@ManyToMany注解 [英] Cascade deleting from join table with @ManyToMany annotation
问题描述
您好我有一个问题,我的映射实体。我使用JPA2和Hibernate实现。我的表与@ManyToMany注释
Hi I got a problem with mapping my entities. I'm using JPA2 and Hibernate implementation. I got tables with @ManyToMany annotation
http://img204.imageshack.us/img204/7558/przykladd.png
我映射的:
@Entity
@Table("employee")
class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@ManyToMany
@JoinTable(name = "proj_emp",
joinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "project_id", referencedColumnName = "id"),
uniqueConstraints = @UniqueConstraint(columnNames = {"employee_id", "project_id"}))
private List<Project> projects; ...}
@Entity
@Table("project")
class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column
private Integer budget;
@ManyToMany(mappedBy = "projects")
private List<Employee> employees; ...}
现在我想有一个级联从表中删除proj_emp当我从员工中删除记录,但是从表项目没有什么可以被删除。
Now I would like to have a cascade deleting from table proj_emp when I delete records from Employee, but nothing from table Project can be deleted.
什么是收购的最佳途径?
What is the best way to acquire that?
谢谢
的Dawid
Thanks Dawid
推荐答案
您可以将您的@ManyToMany分成@一对多,多对一,并成立了级联样式如图所示的此处虽然问题使用Hibernate的会话,你可以使用JPA的EntityManager。或者使用新的JPA功能 @ElementCollection (仅JPA 2)来映射你的加入类。请参见这里如何。仅仅通过更换@ElementCollection Hibernate的@CollectionOfElements
You can split your @ManyToMany into a @OneToMany-ManyToOne and set up a cascading style as shown here Although the question uses Hibernate's session, you can use JPA EntityManager. Or use the new JPA feature @ElementCollection (Only JPA 2) to map your joined class. See here how to. Just replace Hibernate's @CollectionOfElements by @ElementCollection
这篇关于从级联删除连接表与@ManyToMany注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!