删除实体时从休眠连接表中删除项目
我有 2 个班级,其中 @ManyToMany 关系
是一流的:Clip 第二类:位置
主要思想是一个剪辑可以在多个位置可用,
这是剪辑类。
@Entity
@Table(name = "CLIP")
public class Clip {
private Long id;
private List<Location> locations;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToMany( )
@JoinTable(name="CLIP_LOCATION",joinColumns=@JoinColumn(name="CLIP_ID",nullable=false),inverseJoinColumns=@JoinColumn(name="LOCATION_ID",nullable=false) )
@ForeignKey(name="FK_CLIP_LOCATION",inverseName="FK_LOCATION_CLIP")
public List<Location> getLocations() {
return locations;
}
public void setLocations(List<Location> locations) {
this.locations = locations;
}
}
这是位置类,
@Entity
@Table(name = "LOCATION")
public class Location {
}
这不是双向关系
我的问题: 当我从用户界面中删除某个位置时,我希望自动删除连接表中的相关行,
我可以这样做吗?
我应该使用什么注释?
请注意,Location 类和 Clip 类也用于其他关系中,
谢谢 玛雅 我使用了错误的关系吗?
I have 2 classes with @ManyToMany relationship
first class: Clip
second class: Location
the main idea is that a clip can be available in several locations
this is the clip class.
@Entity
@Table(name = "CLIP")
public class Clip {
private Long id;
private List<Location> locations;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToMany( )
@JoinTable(name="CLIP_LOCATION",joinColumns=@JoinColumn(name="CLIP_ID",nullable=false),inverseJoinColumns=@JoinColumn(name="LOCATION_ID",nullable=false) )
@ForeignKey(name="FK_CLIP_LOCATION",inverseName="FK_LOCATION_CLIP")
public List<Location> getLocations() {
return locations;
}
public void setLocations(List<Location> locations) {
this.locations = locations;
}
}
this is the Location class
@Entity
@Table(name = "LOCATION")
public class Location {
}
this is not a bi-directional relationship
My Question:
When i remove a location from the user interface , i want the relevant row in the join-table to be removed automatically,
Can i do that?
what annotation should i use?
Note that the Location class and Clip class are used in other relationships too
Thanks
Maayan
Am i using the wrong relationship?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的。在删除位置之前,您必须从位置列表中删除该位置。由于您的关联是单向的,因此您必须执行查询来查找该位置上可用的所有剪辑,并从每个剪辑中删除该位置。
另一种选择是执行本机 SQL 查询以从连接表中删除行,但请记住,会话中已加载的实体不会意识到更改。
This is not possible. You have to remove the location from the lists of locations before deleting the location. Since your association is unidirectional, you'll have to execute a query to find all the clips available on the location, and remove the location from each clip.
Another option is to execute a native SQL query to remove rows from the join table, but keep in mind that the entities aready loaded in the session won't be aware of the change.