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;
}
}
知道可能出现的问题,或者我如何追踪问题。非常感谢..: - )
答案 0 :(得分:0)
当您从GroupAccount中从Set groupMembersSet中删除项目并保存时,更具体地更新GroupAccount时使用此映射;将删除已编辑的GroupAccount和GroupMember之间的链接,以便删除表memberjunction中的条目。但是,两端的条目都不会被删除。因此,如果您对groupMembersSet执行clear()并保存所有链接,则会将其删除。