-->
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.  [ 2 posts ] 
Author Message
 Post subject: make composit primary key
PostPosted: Sat Jul 18, 2009 4:44 am 
Newbie

Joined: Sat Jul 11, 2009 2:46 am
Posts: 7
Hi I have this table:
CREATE TABLE `buildingtel` (
`username` varchar(50) NOT NULL,
`buildingname` varchar(50) NOT NULL,
`tel` varchar(15) NOT NULL,
PRIMARY KEY (`username`,`buildingname`,`tel`),
KEY `FK_buildingtel_2` (`buildingname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
How can i Map these to and Java entity
I can do like this:
@Entity
@Table(name = "btel")
public class Btel {

@Id
@GeneratedValue
@Column(name = "id")
private int id;;
@Column(name = "username", length=50)
private String username;
@Column(name = "buildingname", length=50)
private int buildingname;
@Column(name = "tel", length=15)
private int tel;
//some setter & getter
}

but i want to set (`username`,`buildingname`,`tel`) together as a primary key how I can do it by annotation in hibernate

_________________
-*-*-*-*-*
[Am1rr3zA]


Top
 Profile  
 
 Post subject: Re: make composit primary key
PostPosted: Sun Jul 19, 2009 9:21 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
You can use:
- annotate the component property as @Id and make the component class @Embeddable
- annotate the component property as @EmbeddedId
- annotate the class as @IdClass and annotate each property of the entity involved in the primary key with @Id

Please refer to this link

Am1rr3zA wrote:
Hi I have this table:
CREATE TABLE `buildingtel` (
`username` varchar(50) NOT NULL,
`buildingname` varchar(50) NOT NULL,
`tel` varchar(15) NOT NULL,
PRIMARY KEY (`username`,`buildingname`,`tel`),
KEY `FK_buildingtel_2` (`buildingname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
How can i Map these to and Java entity
I can do like this:
@Entity
@Table(name = "btel")
public class Btel {

@Id
@GeneratedValue
@Column(name = "id")
private int id;;
@Column(name = "username", length=50)
private String username;
@Column(name = "buildingname", length=50)
private int buildingname;
@Column(name = "tel", length=15)
private int tel;
//some setter & getter
}

but i want to set (`username`,`buildingname`,`tel`) together as a primary key how I can do it by annotation in hibernate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.