Spring,PostgreSQL:自动删除多对多条目的中间表

时间:2015-04-08 08:37:18

标签: java database spring hibernate postgresql

  • 我正在使用我正在使用的Spring-MVC应用程序 PostgreSQL作为数据库,Hibernate作为ORM工具。在里面 应用程序我有两个数据库之间的多对多映射 表(GroupAccount,GroupMembers)
    • 昨天发生了一件奇怪的事,中间人 table(MemberJunction)链接这两个表,条目是 仅删除单个GroupAccount。当我检查了 groupAccount,它就在那里,成员也在那里,但因为 没有条目,没有链接来检索用户 匹配memberJunction的groupAccountId。
    • 生产正在运行debian Wheezy,我无法找到 Postgresql登录/ var / logs /目录,所以我真的不知道怎么做 追踪这个问题。有没有人遇到过类似的问题?如 从春天开始我甚至不发出可以直接影响的查询 中间表。我发布我的SQL代码和唯一的 当我使用内部联接查询时的实例。你能帮忙的话,我会很高兴 因为这发生在生产服务器上。

SQL代码:

CREATE TABLE groupaccount
(
  groupid numeric NOT NULL,
  groupname character varying,
  adminusername character varying,
  CONSTRAINT groupid PRIMARY KEY (groupid)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE groupaccount
  OWNER TO postgres;

CREATE TABLE memberjunction
(
  memberid integer NOT NULL,
  groupid numeric NOT NULL,
  CONSTRAINT membergroupid PRIMARY KEY (memberid, groupid),
  CONSTRAINT groupaccount_memberjunction_fk FOREIGN KEY (groupid)
      REFERENCES groupaccount (groupid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT groupmembers_memberjunction_fk FOREIGN KEY (memberid)
      REFERENCES groupmembers (memberid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE memberjunction
  OWNER TO postgres;

CREATE TABLE groupmembers
(
  memberid integer NOT NULL,
  musername character varying,
  memberaccountstatus boolean NOT NULL DEFAULT false,
  memberactivated boolean NOT NULL DEFAULT false,
  groupaccid numeric NOT NULL DEFAULT 0,
  accesslevel boolean NOT NULL DEFAULT true,
  sortorder numeric NOT NULL DEFAULT 0,
  notedeletenotify boolean NOT NULL DEFAULT false,
  notecreatenotify boolean NOT NULL DEFAULT false,
  canvascreatenotify boolean NOT NULL DEFAULT false,
  attachmentactionnotify boolean NOT NULL DEFAULT false,
  accounteditnotify boolean NOT NULL DEFAULT false,
  notecount integer DEFAULT 0,
  CONSTRAINT memberid PRIMARY KEY (memberid)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE groupmembers
  OWNER TO postgres;

检索成员的Java代码:

  @Override
    public List<GroupAccount> returnMemberIdWithMatchingUsername(String memberUsername) {
        session = sessionFactory.getCurrentSession();
        org.hibernate.Query query = session.createQuery("Select ga From GroupAccount as " +
                "ga INNER JOIN ga.groupMembersSet as gm where gm.memberUsername=:memberUsername");
        query.setParameter("memberUsername",memberUsername);
        List<GroupAccount> groupAccountsList = query.list();
  // Other code ommited.
}

GroupAccountModel:

@Entity
@Table(name="groupaccount")
public class  GroupAccount {

 @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    @JoinTable(name = "memberjunction", joinColumns = {@JoinColumn(name = "groupid")},
                inverseJoinColumns = {@JoinColumn(name = "memberid")})
    private Set<GroupMembers> groupMembersSet = new HashSet<>();
}

GroupMembersModel:

@Entity
@Table(name="groupmembers")
public class GroupMembers {

    @ManyToMany(mappedBy = "groupMembersSet",fetch = FetchType.LAZY)
    private Set<GroupAccount> groupAccounts = new HashSet<>();

}

因此,这样,当删除groupMember时,groupAccount不会被删除,并且当删除groupAccount时,将删除后续成员。

RemoveGroupMember:

   @Override
    public boolean removeGroupMember(int memberId) {
        session = sessionFactory.getCurrentSession();
        GroupMembers groupMembers = (GroupMembers) session.get(GroupMembers.class, memberId);
        if(!(groupMembers == null)){
            for(GroupAccount groupAccount : groupMembers.getGroupAccounts()){
                groupAccount.getGroupMembersSet().remove(groupMembers);
                session.flush();
            }
            groupMembers.getGroupAccounts().clear();
            groupMembers.getOwnedcanvas().clear();
            session.flush();
            session.delete(groupMembers);
            session.flush();
            return true;
        } else {
            return false;
        }
    }

知道可能出现的问题,或者我如何追踪问题。非常感谢..: - )

1 个答案:

答案 0 :(得分:0)

当您从GroupAccount中从Set groupMembersSet中删除项目并保存时,更具体地更新GroupAccount时使用此映射;将删除已编辑的GroupAccount和GroupMember之间的链接,以便删除表memberjunction中的条目。但是,两端的条目都不会被删除。因此,如果您对groupMembersSet执行clear()并保存所有链接,则会将其删除。