GeekInterview.com
Series: Subject: Topic:
Question: 41 of 49

how to create primary key using hibernate?

Asked by: Interview Candidate | Asked on: Oct 23rd, 2006
Showing Answers 1 - 7 of 7 Answers
siddharth

Answered On : Oct 25th, 2006

In your hbm.xml file use field.this field create a primary key for u .

  
Login to rate this answer.
Sachin Yadav

Answered On : Feb 10th, 2007

Id field in hbm.xml file is used to specify the primary key in database. We also use generator to specify the way primary key is generated to the database. For example
< id name="testId" type="string" >
< column name="testColumn" length="40" / >
< generator class="increment" / >
< /id >
here the primary key field name in datbase is testColumn and it will autonmatically incremented by one as the generator is specified as increment.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Sony v George

Answered On : Apr 5th, 2007

In .hbm file there is a tag called id, there we mention the primary key.</hibernate-mapping>Here Id ="empid", that will act as primary key of the table "pojo1"

Yes  1 User has rated as useful.
  
Login to rate this answer.
sarikapj

Answered On : Dec 28th, 2007

View all answers by sarikapj

We can also specify primary key using annotations in the POJO using:@javax.persistence.Id before the appropriate field in POJO with @Column @Id@Column(name="user_id")

  
Login to rate this answer.
chanmaha25

Answered On : Jul 29th, 2008

View all answers by chanmaha25

Ccreate primary key? Not sure but you can defined it in the hbm file by specifying the field with the primary column in the db.

  
Login to rate this answer.

First you have to do is when you are creating table itself you have to give the primary key for any one of the column and then in the hibernate take hbm file  in that there is a tag like in that tag u have to give the name of the column in table which is considered as primary key and also give the name of the property in pojo class which is considered as primary key.So the name of the property which is placed in is considered as primary key by hibernate.

  
Login to rate this answer.
vimala

Answered On : Jul 13th, 2012

In hibernate mapping file

Code
  1. .hbm.xml add <id> under <class>
  2. <class name="class name" table="table name">
  3.    <id name="pojo variable name" column="table column name"/>//primarykey mapping
  4.    <property name="pojo variable name" column="table column name/>//non-primarykey mapping
  5. </class>

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.