从级联删除连接表与@ManyToMany注解

编程入门 行业动态 更新时间:2024-10-28 00:17:15
本文介绍了从级联删除连接表与@ManyToMany注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好我有一个问题,我的映射实体。我使用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

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注解

本文发布于:2023-10-16 05:12:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注解   级联   ManyToMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!