mercredi 8 avril 2015

Spring, PostgreSQL : Intermediate table for many-to-many entries were automatically deleted


  • I am working on a Spring-MVC application in which I am using PostgreSQL as the database with Hibernate as the ORM tool. In the application I have a many-to-many mapping between two database tables(GroupAccount, GroupMembers)

    • A weird thing happened yesterday was, the intermediate table(MemberJunction) which links both these tables, the entries were deleted only for a single GroupAccount. When I checked the groupAccount, it was there, the members were also there, but because there was no entry, there was no linkage to retrieve the users with matching groupAccountId from the memberJunction.

    • The production is running debian Wheezy, and I am unable to find Postgresql logs in /var/logs/ directory, so I really don't know how to trace this issue. Has anyone experienced any similar problem? As from spring I don't even issue queries which can directly effect intermediate tables. I am posting my SQL code and the only one instance when I am querying with inner join. Any help would be nice as this happened on a production server.




SQL code :



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 Code to retrieve Members :



@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.
}


Any idea what might have gone wrong, or how I can trace the issue. Thanks a lot.. :-)


Aucun commentaire:

Enregistrer un commentaire