Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: "Could not resolve type" exception when self reference used
PostPosted: Thu May 05, 2011 2:33 pm 
Newbie

Joined: Thu May 05, 2011 1:56 pm
Posts: 4
Location: China
Hi all! I am learning hibernate and it's really a great tool. While I was doing a little test on self reference, I found an exception. I have been searching for solutions for many times but no luck:( I would really appreciate it if you can help me with it. Thanks in advance!

Here is the class that has children of the same type as it self in my test. The orgnization(Org)
also has other sub companies as its children:
Code:
@Entity
public class Org {
   int id;
   String name;
   Org pOrg;
   
   @Id
   @GeneratedValue
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
             @ManyToOne
   public Org getPOrg() {
      return pOrg;
   }
   public void setPOrg(Org org) {
      pOrg = org;
   }
   }


The following are the sql generated by hibernate when creating the table:
Code:
    create table Org (
        id integer not null auto_increment,
        name varchar(255),
        POrg_id integer,
        primary key (id)
    )

    alter table Org
        add index FK136C474BAA8DD (POrg_id),
        add constraint FK136C474BAA8DD
        foreign key (POrg_id)
        references Org (id)


When I was doing the following query, I got "could not resolve property" exception:
Code:
List<Org> orgList = hibernatetemplate.find("from Org o where o.pOrg.id=1");

exception:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: pOrg of: com.test.oa.model.Org [from com.test.oa.model.Org o where o.pOrg.id=1]; nested exception is org.hibernate.QueryException: could not resolve property: pOrg of: com.test.oa.model.Org [from com.test.oa.model.Org o where o.pOrg.id=1]
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:657)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
   at com.bjsxt.oa.dao.impl.OADaoImpl.getOrgByParent(OADaoImpl.java:21)
   at com.bjsxt.oa.service.impl.OAManagerImpl.topOrgFind(OAManagerImpl.java:21)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy10.topOrgFind(Unknown Source)
   at com.bjsxt.oa.service.OAManagerTest.findOrgTest(OAManagerTest.java:72)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
   at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
   at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
   at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
   at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
   at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
   at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
   at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
   at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
   at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.QueryException: could not resolve property: pOrg of: com.test.oa.model.Org [from com.test.oa.model.Org o where o.pOrg.id=1]
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:61)
   at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1375)
   at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:304)
   at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:410)
   at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:617)
   at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:263)
   at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:210)
   at org.hibernate.hql.ast.tree.DotNode.resolveFirstChild(DotNode.java:175)
   at org.hibernate.hql.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:550)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4543)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1289)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4243)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3722)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1864)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:818)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:604)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
   at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
   ... 39 more



When I have the parent/children structure with two different types of classes using exactly the same grammar, it works fine. So I guess exception may have something to do with self reference:)

Here is the code. In this structure a organization(Org) has many staff(Person).
I removed some codes in case this post is too long:)
Code:
@Entity
public class Org {
   int id;
   String name;
   Set<Person> personnel;

@OneToMany(mappedBy="org")
   public Set<Person> getPersonnel() {
      return personnel;
   }
   public void setPersonnel(Set<Person> personnel) {
      this.personnel = personnel;
   }
}

@Entity
public class Person {
   int id;
   String name;
   Org org;
@ManyToOne
   public Org getOrg() {
      return org;
   }
   public void setOrg(Org org) {
      this.org = org;
   }
}



Top
 Profile  
 
 Post subject: Re: "Could not resolve type" exception when self reference used
PostPosted: Thu May 05, 2011 2:46 pm 
Newbie

Joined: Thu May 05, 2011 1:56 pm
Posts: 4
Location: China
When I make another different self reference in my test. I got the same exception. Sorry for my poor English, please let me describe:)
In the first test there is a Org pOrg reference(used to find its parent Org) in the Org class. In the second test there are a Set<Org> cOrg for searching children organization:

Again I removed the some codes to make it shorter:)

Code:

@Entity
public class Org {
   int id;
   String name;
   String description;
   Org pOrg;
   Set<Org> cOrg;
   Set<Person>   personnel;
   
      
   @ManyToOne
   public Org getPOrg() {
      return pOrg;
   }
   public void setPOrg(Org org) {
      pOrg = org;
   }

   @OneToMany(mappedBy="pOrg")
   public Set<Org> getCOrg() {
      return cOrg;
   }
   public void setCOrg(Set<Org> org) {
      cOrg = org;
   }   
   
}



When I use the following codes to auto generate the tables, I met a similar exception mentioned in the first post
Code:
   @Test
   public void testSchemaExport() {
      new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
   }

exception:

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.bjsxt.oa.model.Org.pOrg in com.bjsxt.oa.model.Org.COrg
   at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:578)
   at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:543)
   at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1177)
   at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329)
   at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:803)
   at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:128)
   at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:91)
   at com.bjsxt.registration.entity.EntityTest.testSchemaExport(EntityTest.java:24)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
   at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
   at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
   at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
   at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
   at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
   at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
   at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
   at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
   at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)




Again it seems that hibernate cannot recognize the Org pOrg property. I did a test using two different classes and it worked fine.


Top
 Profile  
 
 Post subject: Re: "Could not resolve type" exception when self reference used
PostPosted: Thu May 05, 2011 5:45 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
When your getter method start with (after the 'get') two upper-case letters like in getPOrg(), the first letter in the property name is no converted into lower-case. So the solution is to use 'POrg' in your queries instead of 'pOrg'.


Top
 Profile  
 
 Post subject: Re: "Could not resolve type" exception when self reference used
PostPosted: Thu May 05, 2011 6:11 pm 
Newbie

Joined: Thu May 05, 2011 1:56 pm
Posts: 4
Location: China
nordborg wrote:
When your getter method start with (after the 'get') two upper-case letters like in getPOrg(), the first letter in the property name is no converted into lower-case. So the solution is to use 'POrg' in your queries instead of 'pOrg'.


Hi nordborg,
Thanks veeeery much for your reply. I just edited the variable name as you suggested and ran the test again and it finally worked fine!!
It turns out that it was the variable name that caused the exception, but not the self reference thing that I was focusing on:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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.