Hi,
in anyway I wanted to know given the following table structure:-
How do i deal with the associations of the following table structure(postgres) in mapping files & their POJOs & DAOs:-
CREATE TABLE member
(
member_id serial NOT NULL,
member_role_id int4 NOT NULL,
username varchar(50) NOT NULL,
passwd varchar(50) NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50),
email varchar(50),
active char(1),
last_login_dt timestamp,
last_logout_dt timestamp,
admin_flag char(1) NOT NULL DEFAULT 'N'::bpchar,
CONSTRAINT member_pk PRIMARY KEY (member_id),
CONSTRAINT member_role_member_fk1 FOREIGN KEY (member_role_id) REFERENCES member_role (member_role_id) ON UPDATE NO ACTION ON DELETE NO ACTION
)
CREATE TABLE member_role
(
member_role_id serial NOT NULL,
member_role varchar(20) NOT NULL,
member_role_desc varchar(50),
CONSTRAINT member_role_pk PRIMARY KEY (member_role_id)
)
CREATE TABLE team
(
team_id serial NOT NULL,
team_desc varchar(50) NOT NULL,
CONSTRAINT team_pk PRIMARY KEY (team_id)
)
CREATE TABLE team_member
(
team_member_id serial NOT NULL,
team_id int4 NOT NULL,
member_id int4 NOT NULL,
member_role_id int4 NOT NULL,
start_dt timestamp NOT NULL DEFAULT ('now'::text)::date,
end_dt timestamp,
CONSTRAINT team_member_pk PRIMARY KEY (team_member_id),
CONSTRAINT member_role_team_member_fk1 FOREIGN KEY (member_role_id) REFERENCES member_role (member_role_id) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT member_team_member_fk1 FOREIGN KEY (member_id) REFERENCES member (member_id) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT team_team_member_fk1 FOREIGN KEY (team_id) REFERENCES team (team_id) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT team_member_uq001 UNIQUE (team_id, member_id, member_role_id, start_dt)
)
I have written the following code & iam yet not able to save data to member table, join table & other related tables.
<class name="TeamMember" table="team_member">
<composite-id>
<key-many-to-one name="member" column="member_id" class="Member" />
<key-many-to-one name="memberRole" column="member_role_id" class="MemberRole" />
<key-many-to-one name="team" column="team_id" class="Team" />
<key-property name="startDt" column="start_dt" type="java.util.Calendar"/>
</composite-id>
<property name="endDt" column="end_dt" type="java.util.Date" />
</class>
<class name="Member" table="member">
<id name="memberId" column="member_id" type="java.lang.Integer">
<generator class="sequence"/>
</id>
<property name="memberRoleId" column="member_role_id" type="java.lang.Integer" not-null="true" />
<property name="username" column="username" type="java.lang.String" not-null="true" />
<property name="passwd" column="passwd" type="java.lang.String" not-null="true" />
<property name="firstName" column="first_name" type="java.lang.String" not-null="true" />
<property name="lastName" column="last_name" type="java.lang.String" />
<property name="email" column="email" type="java.lang.String" />
<property name="active" column="active" type="java.lang.String" />
<property name="lastLoginDt" column="last_login_dt" type="java.util.Date" />
<property name="lastLogoutDt" column="last_logout_dt" type="java.util.Date" />
<property name="adminFlag" column="admin_flag" type="java.lang.String" not-null="true" />
<set name="TM" inverse="true" cascade="all">
<key column="member_id">
</key>
<one-to-many class="TeamMember"/>
</set>
</class>
<class name="MemberRole" table="member_role">
<id name="memberRoleId" column="member_role_id" type="java.lang.Integer">
<generator class="sequence"/>
</id>
<property name="memberRole" column="member_role" type="java.lang.String" not-null="true" />
<property name="memberRoleDesc" column="member_role_desc" type="java.lang.String" />
<set name="TM" inverse="true" cascade="all">
<key column="member_role_id">
</key>
<one-to-many class="TeamMember"/>
</set>
</class>
<class name="Team" table="team">
<id name="teamId" column="team_id" type="java.lang.Integer">
<generator class="sequence"/>
</id>
<property name="teamDesc" column="team_desc" type="java.lang.String" not-null="true" />
<set name="TM" inverse="true" cascade="all">
<key column="team_id">
</key>
<one-to-many class="TeamMember"/>
</set>
</class>
Now for the classes.
public abstract class Member implements Serializable
{
/** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */
private int hashValue = 0;
/** The composite primary key value. */
private java.lang.Integer memberId;
/** The value of the simple memberRoleId property. */
private java.lang.Integer memberRoleId;
/** The value of the simple username property. */
private java.lang.String username;
/** The value of the simple passwd property. */
private java.lang.String passwd;
/** The value of the simple firstName property. */
private java.lang.String firstName;
/** The value of the simple lastName property. */
private java.lang.String lastName;
/** The value of the simple email property. */
private java.lang.String email;
/** The value of the simple active property. */
private java.lang.String active;
/** The value of the simple lastLoginDt property. */
private java.util.Date lastLoginDt;
/** The value of the simple lastLogoutDt property. */
private java.util.Date lastLogoutDt;
/** The value of the simple adminFlag property. */
private java.lang.String adminFlag;
private Set TM = new HashSet();
// Their getters & setters.
}
public abstract class MemberRole
implements Serializable
{
/** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */
private int hashValue = 0;
/** The composite primary key value. */
private java.lang.Integer memberRoleId;
/** The value of the simple memberRole property. */
private java.lang.String memberRole;
/** The value of the simple memberRoleDesc property. */
private java.lang.String memberRoleDesc;
private Set TM = new HashSet();
// getter & setter
}
public abstract class Team
implements Serializable
{
/** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */
private int hashValue = 0;
/** The composite primary key value. */
private java.lang.Integer teamId;
/** The value of the simple teamDesc property. */
private java.lang.String teamDesc;
private Set TM = new HashSet();
// getter & setter
}
public abstract class AbstractTeamMember
implements Serializable
{
/** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */
private int hashValue = 0;
/** The composite primary key value. */
private java.lang.Integer teamMemberId;
/** The value of the member association. */
private Member member;
/** The value of the memberRole association. */
private MemberRole memberRole;
/** The value of the team association. */
private Team team;
/** The value of the simple startDt property. */
private Calendar startDt;
/** The value of the simple endDt property. */
private java.util.Date endDt;
// getter & setter
}
And finally the DAO.
public class testAsso
{
public static void main(String[] args) throws Exception
{
//IBOSession session = null;
TeamMember objTeamMember = new TeamMember();
try
{
Session session=HibernateUtil.currentSession();
Member memberObj = null;
Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
Date date = xmas.getTime();
//session = ((BOSession)session).currentHibernateSession();
Member member = (Member)session.load(Member.class, new Integer(100));
Team team = (Team)session.load(Team.class, new Integer(40));
MemberRole memberrole = (MemberRole)session.load(MemberRole.class, new Integer(30));
Set s = new HashSet();
objTeamMember.setMember(member);
objTeamMember.setTeam(team);
objTeamMember.setMemberRole(memberrole);
objTeamMember.setStartDt(xmas);
objTeamMember.setEndDt(date);
member.setMemberRoleId(memberrole.getMemberRoleId());
s.add(objTeamMember);
member.setTM(s);
session.save(objTeamMember);
session.save(member);
//session.merge(objTeamMember);
session.flush();
TeamMember objTMember=null;
Query lst = session.createQuery("Select tm from TeamMember tm Where tm.member=100");
List lstMembers=lst.list();
if(lstMembers.size() !=0)
{
int cnt=1;
for(Iterator iter=lstMembers.iterator();iter.hasNext();)
{
objTMember=(TeamMember)iter.next();
System.out.println("Member : "+ cnt + " "+objTMember.getMember().getMemberId());
System.out.println("Team : "+ cnt + " "+objTMember.getTeam().getTeamId());
System.out.println("MemberRole : "+ cnt + " "+objTMember.getMemberRole().getMemberRoleId());
cnt = cnt+1;
}
}
session.close();
}
catch (Exception e)
{
System.out.println("submitTo() : Exception while submiting " + e.getMessage()+" : " +e.getCause()+" - " +e.fillInStackTrace());
//session.rollback();
throw e;
}
finally
{
try
{
//session.closeSession();
}
catch (Exception e)
{
System.out.println("Exception occured during closing of the session: " + e.getMessage());
throw e;
}
}
}
}
just going through my code to find out where i made mistake is all you experts need to do.
Thanks.
|