File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate Query problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Query problem." Watch "Hibernate Query problem." New topic
Author

Hibernate Query problem.

sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
I am trying to run a sample program with hibernate. I have two records in DB with 5 columns. all four column valus are same except one.If issue Select * from TestTable where id = 4545; I a mgetting two records. With Hybernate the collection is having two bo's but both are identical returning first row in the table twise. Could some one explain me what is wrong?

public Object find() {
try {
Query q = getSession().createQuery("from TestTable d where d.id=4545");
List list = q.list();
Data o = (Data) list.get(0);
return list;

} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (HibernateException e) {
// TODO: handle exception
}
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The id property in a Hibernate object always represents the primary key, so I'm going to guess that a query against id will always return 1 row. If id is not your primary key, rename the property and try again.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
By mistake icreated another thread. Please refer new thread fro my reply.

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by sudhakar Tadepalli (in another thread):
Paul, Thanks for your reply, Id property is not a primary ky but Hiber nate propertis file forcing to declare one Id property.

<class name="Data" table="TestTable">
<id name="id" column="SCENEID" type="long">
<generator class="assigned" />
</id>

<property name="personid" column="PERSONID" type="long" />
<property name="nodeid" column="NODEID" type="long" />
<property name="name" column="NAME" type="java.lang.String" />
<property name="value" column="VALUE" type="java.lang.String" />
</class>

Thanks



Basically Hibernate insists on a Primary Key because that is one of the rules of relational data. Its not Hibernate's rule: a table without a Primary Key is modelled incorrectly (they take the hard line view that databases which allow you to define data like this are actually broken). If your data includes 4 fields of the same value and a fith which is diflferent (and this is not the primary key) then you sound like you have a Composite Key which is comprised of every field in your table. If you redefine your mapping to reflect this then a query with ID will match against your composite key, but a query with SCENEID (presuming you remap it to a proprty other than id) will return more than one row.
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
Paul, I a mnew to Hiber nate and trying to run a small test program on a table. Could you please modify the mapping file ?. So I can better understand the defference?. I a mbreking my head sinse yesterday, I appreciate it.

Records look likes this in the table.

SCENEID PERSONID NODEID
---------- ---------- ----------
NAME
-----------------------------------------
VALUE
-----------------------------------------
4545 9595 1234
James
Yes

4545 9595 1234
John
NoValue2

Thanks
Sudhakar
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The easiest way to do this is fix your broken data model. Add a surrogate key and use that as ID.

Otherwise, as I wrote, you will need to replace your ID with a composite key:

Then you will need to remap each of the individual fields (remembering that a property called id is special). For example:

Because Composite Keys are awkward, you will then have to decide what you are actually going to use as an identifier (in the Object world, not the Relational one). This can either be your mapped class itself, or you can write a "composite identifier" class. You'll find example of how to do that in the Hibernate docs. (See what I said about it being easier to just define a surrogate key?)
[ July 29, 2005: Message edited by: Paul Sturrock ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

RE-POSTING A REPLY THAT WAS ACCIDENTALLY POSTED AS A NEW THREAD.

Paul, Thanks for your reply, Id property is not a primary ky but Hiber nate propertis file forcing to declare one Id property.

<class name="Data" table="TestTable">
<id name="id" column="SCENEID" type="long">
<generator class="assigned" />
</id>

<property name="personid" column="PERSONID" type="long" />
<property name="nodeid" column="NODEID" type="long" />
<property name="name" column="NAME" type="java.lang.String" />
<property name="value" column="VALUE" type="java.lang.String" />
</class>

Thanks


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
Paul,

Thank you for example code. After I made some changes , It worked. I would like to explain what I did.

When I remapped the properties, it throws me exception as duplicate mapping
So i did like this.
<composite-id >
<key-property name="sceneid" column="SCENEID" type="long"></key-property>
<key-property name="personid" column="PERSONID" type="long"></key-property>
<key-property name="nodeid" column="NODEID" type="long"></key-property>
<key-property name="name" column="NAME" type="java.lang.String"></key-property>
<key-property name="value" column="VALUE" type="java.lang.String"></key-property>
</composite-id>

Then it complained about Equal method. Then I have added in BO as :

public boolean equals(Object other) {
if ( !(other instanceof Data) ) return false;
Data castOther = (Data) other;
return new EqualsBuilder()
.append(this.getSceneid(), castOther.getSceneid())
.append(this.getPersonid(), castOther.getPersonid())
.append(this.getNodeid(), castOther.getNodeid())
.append(this.getName(), castOther.getName())
.append(this.getValue(), castOther.getValue())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getSceneid())
.append(getPersonid())
.append(getNodeid())
.append(getName())
.append(getValue())
.toHashCode();
}

Then it works


Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Query problem.