-->
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.  [ 10 posts ] 
Author Message
 Post subject: Create POJO class for the table without primary key
PostPosted: Tue Mar 22, 2011 10:50 am 
Newbie

Joined: Tue Mar 22, 2011 10:44 am
Posts: 11
I have been using NetBeans 6.9.1 to create the POJO class and mapping files for the tables in the DB. There I can create both for tables with primary name but can't create for one without primary key. Is there any reason we should not or can't create POJO for table without primary key? There must be workaround for it am I right?

Anjib


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Tue Mar 22, 2011 5:14 pm 
Newbie

Joined: Tue Mar 22, 2011 4:28 pm
Posts: 9
Hello anjibman,

If there is no primary key, then you can create the pojo and hbm like this:

lets say there's a table Emp with 2 columns empid and ename both having not-null constraints and no primary key is there.

then you create Emp.java, EmpID.java and Emp.hbm.xml . EmpID.java will be having the properties empid and ename corresponding to the columns and Emp.java will be having a reference to the EmpID "eid"object. In the Emp.hbm.xml , you will be having a composite-id. The hbm looks similar to this:

<class name="com.Emp" table="Emp">
<composite-id name="eid" class="com.EmpID">
<key-property name="empid" column="empid"/>
<key-property name="ename" column="ename"/>
</composite-id>
</class>

using this you can work on the emp table which is not having a primary key. So, I think ur scenario is like this.

Prasad


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Tue Mar 22, 2011 5:25 pm 
Newbie

Joined: Tue Mar 22, 2011 10:44 am
Posts: 11
My problem is I am using existing DB (Oracle). In this DB I have table called "USER" with fields user_id, name, email and phone but none of these fields are defined as primary key and I cannot alter the table schema.
Now my question is can I write mapping specifying user_id as <id> even its not a primary key in the DB?


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Tue Mar 22, 2011 8:56 pm 
Newbie

Joined: Tue Mar 22, 2011 4:28 pm
Posts: 9
Are there any other constraints like unique, not-null on those columns?


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Tue Mar 22, 2011 8:58 pm 
Newbie

Joined: Tue Mar 22, 2011 10:44 am
Posts: 11
No constraints at all.
What difference will it make with or without constraints?


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Tue Mar 22, 2011 9:21 pm 
Newbie

Joined: Tue Mar 22, 2011 4:28 pm
Posts: 9
Now my question is can I write mapping specifying user_id as <id> even its not a primary key in the DB?
You can map it with an <id> element.
but it throws an exception if more than one record with a given id exists.
To explain it clearly,
Lets take the table u have mentioned: USER with USER_ID,NAME,EMAIL,PHONE as columns. If you create a POJO class by name User and have the properties as userId,name,email,phone and have the getters and setters. Then write the hbm file
with an id element with the property userId (generator assigned), and all other variables as normal properties.
Lets say there are 2 records with 101 as user_id. When you do a get like session.get(User.class,"101"); then hibernate throws an exception stating more than one row with the given identifier was found.


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Tue Mar 22, 2011 11:25 pm 
Newbie

Joined: Tue Mar 22, 2011 10:44 am
Posts: 11
This bring to my next concern of generating incremental number on the field user_id. I think if I can generate incremental number in this field then exception can be avoided. Am I right?


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Wed Mar 23, 2011 10:25 am 
Newbie

Joined: Tue Mar 22, 2011 4:28 pm
Posts: 9
Yes, If you made sure that the column which you have given as the id field has no duplicate values, then I think you are fine.


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Wed Mar 23, 2011 11:05 am 
Newbie

Joined: Tue Mar 22, 2011 10:44 am
Posts: 11
Just to make sure is that having
Code:
<class name="User" table="USER">
<id name="id" column="USER_ID" type="integer">
<generator class="increment"/>
</id>
...
</class>


Top
 Profile  
 
 Post subject: Re: Create POJO class for the table without primary key
PostPosted: Wed Mar 23, 2011 1:17 pm 
Newbie

Joined: Tue Mar 22, 2011 4:28 pm
Posts: 9
Also make sure that no external source source other than ur application is updating the table in the database. There might be a chance that another record with the same id can be entered as there is no unique constraint on the USER_ID field.


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