-->
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.  [ 5 posts ] 
Author Message
 Post subject: Composite key mapping problem, please help!
PostPosted: Tue Jun 08, 2004 12:15 am 
Newbie

Joined: Tue Jun 08, 2004 12:04 am
Posts: 12
Hi, I've spent my previous 2 days tryinyg to get hibernate up and running. I've not been successuful.

I am on the edge of giving hibernate up and go for an easier to use product, despite hibernate's reputation.

Hibernate is giving me a stacktrace that I cannot understand a thing.

My goal for this POC is just to query a database with 2 fields as a composite PK : sd_rec_key and sd_instance_id

It is just as simple as that.

Please help if you can comprehen the errors. Thanks.

-Rob


Hibernate version 2.1.4
Database : Informix 9

------------------------
full Stack Trace
------------------------
net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.dhl.test.hibernate.ShipmentDetail.sdRecKey
at net.sf.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:110)
at net.sf.hibernate.type.ComponentType.getPropertyValue(ComponentType.java:179)
at net.sf.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:205)
at net.sf.hibernate.type.ComponentType.nullSafeGetValues(ComponentType.java:164)
at net.sf.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:151)
at net.sf.hibernate.loader.Loader.bindPositionalParameters(Loader.java:674)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:713)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:185)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:836)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:856)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2113)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1987)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1916)
at com.dhl.test.hibernate.TestRun.main(TestRun.java:34)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at net.sf.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:96)
... 17 more


--------------------------
Mapping document :
---------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.dhl.test.hibernate.ShipmentDetail" table="ship_detail">
<composite-id>
<key-property name="sdRecKey" column="sd_rec_key"/>
<key-property name="sdInstanceId" column="sd_instance_id"/>
</composite-id>
<property name="awbNumber">
<column name="sd_awb_no" length="35" not-null="true"/>
</property>
</class>
</hibernate-mapping>
----------------------------
Java Code
----------------------------
package com.dhl.test.hibernate;

import java.io.Serializable;
import java.math.BigDecimal;

/*
* Created on Jun 7, 2004
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/

/**
* @author Robert.Soo
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class ShipmentDetail implements Serializable {

private String awbNumber;
private BigDecimal sdRecKey;
private short sdInstanceId;

public ShipmentDetail() {
}

public int hashCode() {
return sdRecKey.hashCode() + sdInstanceId;
}
/**
* @return
*/
public String getAwbNumber() {
return awbNumber;
}

/**
* @return
*/
public short getSdInstanceId() {
return sdInstanceId;
}

/**
* @param string
*/
public void setAwbNumber(String string) {
awbNumber = string;
}

/**
* @param s
*/
public void setSdInstanceId(short s) {
sdInstanceId = s;
}

/**
* @return
*/
public BigDecimal getSdRecKey() {
return sdRecKey;
}

/**
* @param l
*/
public void setSdRecKey(BigDecimal l) {
sdRecKey = l;
}

public boolean equals(Object anObject) {
try {
ShipmentDetail anObject2 = (ShipmentDetail) anObject;
if (anObject2.getSdInstanceId() == getSdInstanceId()
&& anObject2.getSdRecKey().equals(getSdRecKey()))
return true;
} catch (ClassCastException e) {
return false;
}
return false;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 08, 2004 1:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You of course also need to show the code that calls load().
Quote:
I am on the edge of giving hibernate up and go for an easier to use product

This will be a trivial newbie problem, so no need to insult us.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 08, 2004 2:09 am 
Newbie

Joined: Tue Jun 08, 2004 12:04 am
Posts: 12
Hi there,

Thanks for pointing out that it is a trival newbie's problem. I hope that with you guys' experience this problem should be easily identified and resolved before long. So I be happily checking the response to this email in a timely fashion so that I can benefit from you guys' experience and guidance.

Thanks for help and with warmest regards.

This is the test run code:
public class TestRun {

public static void main(String[] args) throws Exception {
Session session = HibernateUtil.currentSession();
ShipmentDetail dept =
(ShipmentDetail) session.load(ShipmentDetail.class, "1");
session.close();
}
}

and this is the HibernateUtil class:

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
Configuration cfg =
new Configuration().addClass(
com.dhl.test.hibernate.ShipmentDetail.class);
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException(
"Configuration problem: " + ex.getMessage(),
ex);
} catch (Exception e) {
throw new RuntimeException(
"Configuration problem: " + e.getMessage(),
e);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 08, 2004 2:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Since you have mapped a composite identifier, "1" is clearly not a valid identifier value.

Since this is an "embedded" composite id (against the recommendation of the documentation), a valid id would be an instance of ShipmentDetail.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 08, 2004 2:59 am 
Newbie

Joined: Tue Jun 08, 2004 12:04 am
Posts: 12
Hi Gavin,

Thanks for your advice and clarification...
I observed your recommendation and turned my stuff into a non-embedded composite keys and everythings worked out.

Many thanks for your help and I find hibernate's forum indeed very useful.

I do, however, have some suggestions:

1. Composite ID seems to be a big topic (especially in many corporate databases), therefore please consider expanding the FAQ to cover more of those.

2. The stacktrace for the composite keys related errors are really not that helpful. Please consider evaluating it again.

3. Would be great if there is a troubleshooting guides (for beginners), that focuses on different scenarios, such as:
(i) A helloworld app that is all POJOs, all basic mapping and no composite keys. (I know there is one designed to work with tomcat but ppl are looking for, yet, easier ones)
(ii) Trouble shooting guides covering composite mapping and many-to-many mappings. This is more like a "if you run into this kind of exception, this is what you need to check"...

Overall, thank you for helping out. It is good to know there are always good guys out there looking for one's shoulder...

:)


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