-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: [Mapping] manty-to-many with relation attributes
PostPosted: Tue Jan 26, 2010 11:29 am 
Beginner
Beginner

Joined: Fri May 23, 2008 4:37 am
Posts: 25
Hi everyone

Hi have these tables :
Quote:
CREATE TABLE student (
"std_id" SERIAL NOT NULL,
"std_firstname" VARCHAR(125),
"std_lastname" VARCHAR(125),
....
CONSTRAINT "pk_student" PRIMARY KEY("std_id"),
....
);

CREATE TABLE address (
"adr_id" SERIAL NOT NULL,
"adr_postcode" SMALLINT(4) NOT NULL,
...
CONSTRAINT "pk_address" PRIMARY KEY("adr_id")
);

CREATE TABLE contact (
"con_id" SERIAL NOT NULL,
"con_name" VARCHAR(255),
"adr_id" INTEGER,
...
CONSTRAINT "pk_contact" PRIMARY KEY("con_id"),
CONSTRAINT "fk_contact_address" FOREIGN KEY ("adr_id")
REFERENCES "address"("adr_id") ON DELETE RESTRICT ON UPDATE CASCADE
);

CREATE TABLE student_contact (
"std_id" INTEGER NOT NULL,
"con_id" INTEGER NOT NULL,
"default" BOOLEAN DEFAULT FALSE,
"type" INTEGER NOT NULL,
CONSTRAINT "pk_student_contact" PRIMARY KEY ("std_id", "con_id"),
CONSTRAINT "fk_student_contact_student" FOREIGN KEY ("std_id")
REFERENCES "student"("std_id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "fk_student_contact_contact" FOREIGN KEY ("con_id")
REFERENCES "contact"("con_id") ON DELETE RESTRICT ON UPDATE CASCADE
);


As you can see I have a N:M relation between Student and Contact.
- A Student can have many Contacts
- Each Contacts are represented with a "type"
- Each Student hold one default Contact

I already have functionnal mappings for Student and Contact without these relation (I rarely need a Student with contacts informations and I'm using Contact in other relations without Student). I want to have a mapping who let me retrieve Student informations and Contact informations (contact type, and who is the default contact) but I don't know how to make this mapping and hold these values without pollute my objects..

The best solution (in my case) will be to have a StudentDetails class who maintains a collection (of any type) of Contacts where I can retrieve "type" informations and the default contact. I have started this mapping :
Quote:
<hibernate-mapping >
<class name="me.StudentDetails" table="student">
<id column="std_id" name="id">
<generator class="sequence">
<param name="sequence">student_std_id_seq</param>
</generator>
</id>

<property column="std_firstname" name="firstName" />
<property column="std_lastname" name="lastName" />

<list name="contactDetails" lazy="false" table="student_contact">
<key column="std_id" />
<list-index column="con_id" />
<many-to-many class="me.StudentDetails.ContactDetails"
column="con_id" />
</list>

</class>

<class name="me.StudentDetails.ContactDetails" table="contact">
<id column="con_id" name="id" >
<generator class="sequence">
<param name="sequence">contact_con_id_seq</param>
</generator>
</id>

<property column="con_name" name="name" />

<many-to-one column="adr_id" name="address" lazy="false"
not-null="false" cascade="save-update" insert="true" />

</class>
</hibernate-mapping>

But I don't know how to retrieve the two relations attributes into my ContactDetails object.

Of course I can do that in a programmatic way :
Code:
public class StudentDao {

  public List<Contact> getContacts(Integer studentId);

  public List<Contact> getDefaultContact(Integer studentId);

}

But I'm sure that someone here can help me and give me a better solution..

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.