-->
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.  [ 3 posts ] 
Author Message
 Post subject: HowTo Map a table whose Primary key consists of 2 columns???
PostPosted: Sun Nov 21, 2004 6:50 pm 
Newbie

Joined: Sun Nov 21, 2004 6:20 pm
Posts: 11
Hi,
I am a complete Newbie to Hibernate and my problem is the following. I do not know howto correctly map a table whose primary key consists of two columns....The other question is more general. Is it better to generate the DDL for the databases before doing the hibernate-mappings or should one design the database tables on the basis of the hibernate mappings and java-classes???

So here is an example...I have 2 tables and I have also created the 2 corresponding Java- classes...the mapping of the class Question.java is not a problem, but how can I tell Hibernate that class Options.java has a primary key with 2 colums and that there is a relationship between Options and Question (1 Question can have many options...)??? (I have read something about composite id...is this the right thing?? )

Thanks for any help and code examples....Are my class-definitions correct??

create table Question
(
question_id integer not null,
question_text varchar(100) not null,
primary key(question_id)
)

create table Options
(
option_id integer not null,
question_id integer not null,
option_text char(20) not null,
primary key(option_id, question_id), foreign key(question_id) references Question(question_id)
)


/***********/
I modelled the corresponding classes like this....is this correct??
public class Question
{
public int question_id;
public String question_text;

//getter and setter....
}


public class Options
{
public int question_id;
public int option_id;
public String option_text;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 21, 2004 6:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please read the reference doc, for example http://www.hibernate.org/hib_docs/reference/en/html/components.html#components-compositeid


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 5:30 pm 
Newbie

Joined: Tue Mar 30, 2004 9:58 am
Posts: 8
Location: Chicago, IL
Hi,
I had a similar requirements. You need to look into chapter 18 of the hibernate reference doc. Look into the example Customer/Order/LineItem/Product.

But this pattern seems to have a related bug while reading the list from db. See a related post by me called, Hibernate Bug- Problem reading the Composite-element.

Matt

_________________
Matt Madhavan, Consultant


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.