-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: New user -> problem with many-to-one relation ship
PostPosted: Wed Nov 26, 2003 4:33 pm 
Newbie

Joined: Wed Nov 26, 2003 1:27 pm
Posts: 9
Location: Overland Park KS
I'm a new user.. started using Hibernate last week.

I've the following relationship.
[b]Many-to-one relation between AnnualTract and Tract Objects.[/b]

What i'm trying to accomplish is when I find AnnualTractPersistenceObject I need to have the associated TractPersistenceObject also populated.

Here is the mapping i'm using followed with java code.

<hibernate-mapping>
<class name="gov.usda.fsa.farmrecords.frs.persistenceobject.AnnualTractPersistenceObject" table="tractyear">

<!--<jcs-cache usage="read-write"/>-->

<id column="id" name="oid">
<generator class="identity"/>
</id>

<property name="fiscalYear" column="fiscalyear" not-null="true" length="4"/>
<property name="tractID" column="tract_id" not-null="true" length="4"/>
<property name="farmLandAcreage" column="farmland_acreage" length="4"/>
<property name="cropLandAcreage" column="cropland_acreage" length="4"/>

<many-to-one name="tractPersistenceObject" column="tract_id" cascade="none" outer-join="true" insert="false" update="false" not-null="true"/>

</class>

<class name="gov.usda.fsa.farmrecords.frs.persistenceobject.TractPersistenceObject" table="tract">

<!--<jcs-cache usage="read-write"/>-->

<id column="id" name="oid">
<generator class="identity"/>
</id>

<property name="number" column="nb" not-null="true" length="7"/>
<property name="stateFSACode" column="state_code" not-null="true" length="2"/>
<property name="countyFSACode" column="county_code" not-null="true" length="3"/>

</class>
</hibernate-mapping>


Java Class:

public class AnnualTractPersistenceObject {

private static String FIND_ALL = "from gov.usda.fsa.farmrecords.frs.persistenceobject.AnnualTractPersistenceObject as tractyear, gov.usda.fsa.farmrecords.frs.persistenceobject.TractPersistenceObject as tract";
private static String FIND = FIND_ALL + " where tractyear.oid = ? and tractyear.tractID = tract.oid";


private Integer oid = null;
private Integer fiscalYear = null;
private Integer tractID = null;
private Integer farmLandAcreage = null;
private Integer cropLandAcreage = null;
private TractPersistenceObject lTract = null;

public AnnualTractPersistenceObject()
{
}

public static AnnualTractPersistenceObject find( Integer aOid)
throws HibernateException
{
Object lObject = getSession().find(
AnnualTractPersistenceObject.FIND, aOid, Hibernate.INTEGER).get(0);
AnnualTractPersistenceObject lUnit = (AnnualTractPersistenceObject) lObject;
return lUnit;
}

public Integer getOid()
{
return oid;
}

protected void setOid(Integer aOid)
{
oid = aOid;
}

/**
* @return
*/
public Integer getCropLandAcreage() {
return cropLandAcreage;
}

/**
* @return
*/
public Integer getFarmLandAcreage() {
return farmLandAcreage;
}

/**
* @return
*/
public Integer getFiscalYear() {
return fiscalYear;
}

/**
* @param integer
*/
public void setCropLandAcreage(Integer integer) {
cropLandAcreage = integer;
}

/**
* @param integer
*/
public void setFarmLandAcreage(Integer integer) {
farmLandAcreage = integer;
}

/**
* @param integer
*/
public void setFiscalYear(Integer integer) {
fiscalYear = integer;
}

/**
* @return
*/
public TractPersistenceObject getLTract() {
return lTract;
}

/**
* @param object
*/
public void setLTract(TractPersistenceObject object) {
lTract = object;
}

/**
* @return
*/
public Integer getTractID() {
return tractID;
}

/**
* @param integer
*/
public void setTractID(Integer integer) {
tractID = integer;
}

}



TractPersistenceObject:
-----------------------------
public class TractPersistenceObject {

private static String FIND_ALL = "from t1 in class gov.usda.fsa.farmrecords.frs.persistenceobject.TractPersistenceObject";
private static String FIND = FIND_ALL + " where t1.oid = ?";

private String number = null;
private String stateFSACode = null;
private String countyFSACode = null;

public TractPersistenceObject()
{
}

public static TractPersistenceObject find(Integer aOid)
throws HibernateException
{
Object lObject = getSession().find(
TractPersistenceObject.FIND, aOid, Hibernate.INTEGER).get(0);
TractPersistenceObject lUnit = (TractPersistenceObject) lObject;
return lUnit;
}

public Integer getOid()
{
return oid;
}

protected void setOid(Integer aOid)
{
oid = aOid;
}
/**
* @return
*/
public String getCountyFSACode() {
return countyFSACode;
}

/**
* @return
*/
public String getNumber() {
return number;
}

/**
* @return
*/
public String getStateFSACode() {
return stateFSACode;
}

/**
* @param string
*/
public void setCountyFSACode(String string) {
countyFSACode = string;
}

/**
* @param string
*/
public void setNumber(String string) {
number = string;
}

/**
* @param string
*/
public void setStateFSACode(String string) {
stateFSACode = string;
}

}


I got the following error when I called the method find on AnnualTractPersistentObject:

java.lang.ExceptionInInitializerError: java.lang.RuntimeException: Fail to build SessionFactory: Problem trying to set association type by reflection: Could not find a getter for tractPersistenceObject in class gov.usda.fsa.farmrecords.frs.persistenceobject.AnnualTractPersistenceObject
at gov.usda.fsa.farmrecords.common.refarch.persistence.HibernateSession.initSessionFactory(HibernateSession.java:38)
at gov.usda.fsa.farmrecords.common.refarch.persistence.HibernateSession.<clinit>(HibernateSession.java:22)
at gov.usda.fsa.farmrecords.common.refarch.FRPersistenceObject.getSession(FRPersistenceObject.java:41)
at gov.usda.fsa.farmrecords.common.refarch.persistence.TxPersistenceObject.begin(TxPersistenceObject.java:53)
at gov.usda.fsa.farmrecords.common.refarch.service.FRTransactionControllerForPersistenceService.txBegin(FRTransactionControllerForPersistenceService.java:39)
at gov.usda.fsa.farmrecords.common.refarch.persistence.TxPersistenceService.beginTx(TxPersistenceService.java:38)
at gov.usda.fsa.farmrecords.common.refarch.test.BasePersistentServiceTest.txBegin(BasePersistentServiceTest.java:31)
at gov.usda.fsa.farmrecords.common.refarch.test.FRTestCase.runBare(FRTestCase.java:65)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)



Please help me what are all I need to change on this make this working. Let me know if I need to provide you guys with anymore information. One thing I'm not sure is how to form the query to retrieve values.

Also please pass on any information if I can find any examples on Hibernate.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 4:55 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
It looks like the source of the problem is in your getSession method. You need to configure the SessionFactory with both persistent objects you are using. Chapter 2 (Hibernate reference) gives details.

mota


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 4:58 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 10:54 am
Posts: 67
The name you used in the many-to-one mapping tractPersistenceObject means hibernate expects there to be methods getTractPersistenceObject()
and setTractPersistenceObject() in class AnnualTractPersistenceObject but you have called these methods get/setLTract().


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 5:20 pm 
Newbie

Joined: Wed Nov 26, 2003 1:27 pm
Posts: 9
Location: Overland Park KS
I already tried changing the name 'lTract', But still getting the same error.


'java.lang.ExceptionInInitializerError: java.lang.RuntimeException: Fail to build SessionFactory: Problem trying to set association type by reflection: Could not find a getter for lTract in class gov.usda.fsa.farmrecords.frs.persistenceobject.AnnualTractPersistenceObject
at gov.usda.fsa.farmrecords.common.refarch.persistence.HibernateSession.initSessionFactory(HibernateSession.java:38)
at gov.usda.fsa.farmrecords.common.refarch.persistence.HibernateSession.<clinit>(HibernateSession.java:22)

also I configured the SeesionFactory correctly. I could confidently say this because I was successfully able to retrieve(find) AnnaulTractPersistenceObject correctly and i've all the hibernate-mappings defined in the same file.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 5:29 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Is your mapping file in the classpath? If the problem is not solved you need th follow the instruction in Chapter 2 regarding how to build the session factory.

mota


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 5:41 pm 
Newbie

Joined: Wed Nov 26, 2003 1:27 pm
Posts: 9
Location: Overland Park KS
Yes.. The mapping file is is in the classpath and I was able to successfully intialize the sessionFactory without <many-to-one> tag in the AnnualTractPersistenceObject class mapping xml node.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 5:48 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Then make sure that you implement what teknokrat said: The reference to the parent in the child should be in sync with its get/set method and the entry in the many-to-one.

mota


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 5:51 pm 
Newbie

Joined: Wed Nov 26, 2003 1:27 pm
Posts: 9
Location: Overland Park KS
I already tried what teknokrat said and posted a reply on that. Please refer back to my 2nd message.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 6:02 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Did you replace the field name "ITract" by "tractPersistenceObject"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 6:05 pm 
Newbie

Joined: Wed Nov 26, 2003 1:27 pm
Posts: 9
Location: Overland Park KS
Yes.. I tried replacing 'tractPersistentObject' with 'lTract' in the mapping-file.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 7:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Gowtham Nalluri wrote:
Yes.. I tried replacing 'tractPersistentObject' with 'lTract' in the mapping-file.

As per the JavaBean spec, replace name="lTract" by name="LTract"

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 6:40 pm 
Newbie

Joined: Wed Nov 26, 2003 1:27 pm
Posts: 9
Location: Overland Park KS
Thanks Bernard. I made progress when I replaced 'lTract' with 'LTract' in the mapping file.

Now i'm getting the following error:

java.lang.Object
at gov.usda.fsa.farmrecords.frs.persistenceservice.TractPersistenceService.findAnnualTract(TractPersistenceService.java:72)
at gov.usda.fsa.farmrecords.frs.persistenceservice.test.AnnualTractPersistenceServiceTest.testFindAnnualTract(AnnualTractPersistenceServiceTest.java:35)
at java.lang.reflect.Method.invoke(Native Method)
at junit.framework.TestCase.runTest(TestCase.java:154)
at gov.usda.fsa.farmrecords.common.refarch.test.FRTestCase.runBare(FRTestCase.java:68)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)


When I printed the stacktrace its a ClassCast Exception(java.lang.ClassCastException: java.lang.Object).

Does anyone know about this error?

Thanks for anyhelp on this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Looking at your stack trace, it seems to be a pb of yours. Nowhere hibernate stacktrace is involved.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:43 pm 
Newbie

Joined: Wed Nov 26, 2003 1:27 pm
Posts: 9
Location: Overland Park KS
I'm calling this method to find the AnnualTractPersistenceObject.

I'm trying to cast like this:
AnnualTractPersistenceObject lUnit = (AnnualTractPersistenceObject) lObject;

This is where I'm getting the cast error.


Here is the listing of the complete method. I'm using the query like this:

"from gov.usda.fsa.farmrecords.frs.persistenceobject.AnnualTractPersistenceObject as tractyear, gov.usda.fsa.farmrecords.frs.persistenceobject.TractPersistenceObject as tract where tractyear.oid = ? and tractyear.tractID = tract.oid"



public static AnnualTractPersistenceObject find( Integer aOid)
throws HibernateException
{
Object lObject = getSession().find(
AnnualTractPersistenceObject.FIND, aOid, Hibernate.INTEGER).get(0);
AnnualTractPersistenceObject lUnit = (AnnualTractPersistenceObject) lObject;
return lUnit;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
That query returns an ordered pair, ie. an Object[], not a single object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.