如何使用Hibernate / JPA删除带有额外列的ManyToMany表

时间:2014-07-27 03:03:59

标签: hibernate jpa

我在一个表中关联了两个表,就像这个模型一样:

Model

如何删除PersonOnGroup表中的记录而不删除Person或Groups表?使用Cascade还是删除Orphan?或者Hibernate还有另一种方式吗?

这些是实体:

    @Entity
    @Table(name = "groups")
    public class Groups {

        private long idGroup;
        private String name;
        private String description;
        private Collection<Event> eventsByIdGroup;
        private Collection<PersonOnGroup> personOnGroupByIdGroup;


        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "idGroup")
        public long getIdGroup() {
            return idGroup;
        }

        public void setIdGroup(long idGroup) {
            this.idGroup = idGroup;
        }
    //Other Setter and getters
        @OneToMany(mappedBy = "eventByIdGroups")
        public Collection<Event> getEventsByIdGroup() {
            return eventsByIdGroup;
        }

        public void setEventsByIdGroup(Collection<Event> eventsByIdGroup) {
            this.eventsByIdGroup = eventsByIdGroup;
        }

        @OneToMany
        public Collection<PersonOnGroup> getPersonOnGroupByIdGroup() {
            return personOnGroupByIdGroup;
        }

        public void setPersonOnGroupByIdGroup(Collection<PersonOnGroup> personOnGroup) {
            this.personOnGroupByIdGroup = personOnGroup;
        }
    }






    @Entity
    @Table(name = "person")
    public class Person {

        private long idPerson;
        private String firstName;
        private String lastName;
        private String username;
        private String email;
        private String password;
        private String imageUrl;
        private boolean enable = true;

        private Collection<Comment> commentsByIdPerson;
        private AccessRights accessRightsByIdAccessRights;
        private Collection<PersonOnGroup> personOnGroupByIdPerson;

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "idPerson")
        public long getIdPerson() {
            return idPerson;
        }

        public void setIdPerson(long idPerson) {
            this.idPerson = idPerson;
        }
    //Other getter and setter methods

        @OneToMany(mappedBy = "commentByIdPerson")
        public Collection<Comment> getCommentsByIdPerson() {
            return commentsByIdPerson;
        }

        public void setCommentsByIdPerson(Collection<Comment> commentsByIdPerson) {
            this.commentsByIdPerson = commentsByIdPerson;
        }

        @ManyToOne
        @JoinColumn(name = "idAccessRights", referencedColumnName = "idAccessRights", nullable = false)
        public AccessRights getAccessRightsByIdAccessRights() {
            return accessRightsByIdAccessRights;
        }

        public void setAccessRightsByIdAccessRights(AccessRights accessRightsByIdAccessRights) {
            this.accessRightsByIdAccessRights = accessRightsByIdAccessRights;
        }

        @OneToMany
        public Collection<PersonOnGroup> getPersonOnGroupByIdPerson() {
            return personOnGroupByIdPerson;
        }

        public void setPersonOnGroupByIdPerson(Collection<PersonOnGroup> personOnGroupByIdPerson) {
            this.personOnGroupByIdPerson = personOnGroupByIdPerson;
        }
    }



@Embeddable
public class PersonOnGroupPK implements Serializable {

    private Person personPK;
    private Groups groupsPK;

    @ManyToOne
    public Person getPersonPK() {
        return personPK;
    }

    public void setPersonPK(Person person) {
        this.personPK = person;
    }

    @ManyToOne
    public Groups getGroupsPK() {
        return groupsPK;
    }

    public void setGroupsPK(Groups groups) {
        this.groupsPK = groups;
    }
}


@Entity
@Table(name = "personongroup")
public class PersonOnGroup {

        private Timestamp joinDate;
        private PersonOnGroupPK personOnGroupPK;

        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        @Column(name = "joinDate")
        public Timestamp getJoinDate() {
                return joinDate;
        }

        public void setJoinDate(Timestamp createdAt) {
                this.joinDate = createdAt;
        }


        @EmbeddedId
        public PersonOnGroupPK getPersonOnGroupPK() {
                return personOnGroupPK;
        }

        public void setPersonOnGroupPK(PersonOnGroupPK personOnGroupPK) {
                this.personOnGroupPK = personOnGroupPK;
        }
}

0 个答案:

没有答案