-->
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.  [ 2 posts ] 
Author Message
 Post subject: could not resolve prperty
PostPosted: Sun Aug 13, 2006 9:20 am 
Beginner
Beginner

Joined: Sun Aug 13, 2006 8:48 am
Posts: 23
Hibernate version: 3.2

Mapping documents:

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

<hibernate-mapping>

   <class name="ergal.Order" table="ORDERS">
      <id name="id" type="long" column="ID">
         <generator class="increment" />
      </id>
   
      <property name="orderNumber" type="string">
         <column name="ORDERNUMBER" length="15" />
      </property>
      
      <many-to-one
         name="Customer"
         column="CUSTOMER_ID"
         class="ergal.Customer"
         not-null="true"
         cascade="save-update"
      />
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
public List findOrdersByCustomer(Customer customer)throws Exception
   {
      Session session=sessionFactory.openSession();
      Transaction tx=null;
      try
      {
         tx=session.beginTransaction();
         List orders = session.createQuery("from Order as o where o.customer.id=" + customer.getId()).list();
         tx.commit();
         return orders;
      }
      catch(Exception e)
      {
         if(tx!=null)
         {
            tx.rollback();
         }
         throw e;
      }
      finally
      {
         session.close();
      }
   }

Full stack trace of any exception that occurs:

[java] Exception in thread "main" org.hibernate.QueryException: could not resolve property: customer of: ergal.Order [from ergal.Order as o where o.customer.id=1]
[java] at org.hibernate.persister.entity.AbstractPropertyMapping.throwP
ropertyException(AbstractPropertyMapping.java:43)
[java] at org.hibernate.persister.entity.AbstractPropertyMapping.toType
(AbstractPropertyMapping.java:37)
[java] at org.hibernate.persister.entity.AbstractEntityPersister.toType
(AbstractEntityPersister.java:1308)
[java] at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(Fr
omElementType.java:280)
[java] at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromEl
ement.java:373)
[java] at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:5
39)
[java] at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:22
1)
[java] at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:172)
[java] at org.hibernate.hql.ast.tree.DotNode.resolveFirstChild(DotNode.
java:139)
[java] at org.hibernate.hql.ast.HqlSqlWalker.lookupProperty(HqlSqlWalke
r.java:467)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseW
alker.java:4326)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalke
r.java:1212)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSq
lBaseWalker.java:4041)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSq
lBaseWalker.java:3525)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBa
seWalker.java:1762)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBa
seWalker.java:776)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalk
er.java:577)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlS
qlBaseWalker.java:281)
[java] at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBase
Walker.java:229)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTransl
atorImpl.java:228)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTran
slatorImpl.java:160)
[java] at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTransl
atorImpl.java:111)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.j
ava:77)
[java] at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.j
ava:56)
[java] at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(Que
ryPlanCache.java:72)
[java] at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(Abstra
ctSessionImpl.java:133)
[java] at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSe
ssionImpl.java:112)
[java] at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1
623)
[java] at ergal.BusinessService.findOrdersByCustomer(Unknown Source)
[java] at ergal.BusinessService.test(Unknown Source)
[java] at ergal.BusinessService.main(Unknown Source)

Name and version of the database you are using:

Mysql 5.0 22
conn:
mysql-connector-java-5.0.0-beta-bin.jar



The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

This is My first topic

I have a problem about HB when query a HQL

most of the code could run without exception

This is my project

Code:
.
+bin
    hibernate.cfg.xml
    +ergal
        BusinessService.class
        Customer.class
        Order.class
        Customer.hbm.xml
        Order.hbm.xml
+lib
    ......

build.xml


Code

build.xml

Code:
<?xml version="1.0" encoding="GBK"?>

<project name="hibernate-tutorial" default="compile">

   <property name="sourcedir" value="${basedir}/src"/>
   <property name="targetdir" value="${basedir}/bin"/>
   <property name="librarydir" value="${basedir}/lib"/>
   <property name="schema.dir" value="${basedir}/data"/>
   
   <path id="libraries">
      <fileset dir="${librarydir}">
         <include name="*.jar"/>
      </fileset>
   </path>
   <path id="project.class.path">
   <!-- Include our own classes, of course -->
      <pathelement location="${targetdir}" />
   <!-- Include jars in the project library directory -->
      <fileset dir="${librarydir}">
         <include name="*.jar"/>
      </fileset>
   </path>

   <target name="clean">
      <delete dir="${targetdir}"/>
      <mkdir dir="${targetdir}"/>
   </target>

   <target name="copy-resources">
      <copy todir="${targetdir}">
         <fileset dir="${sourcedir}">
            <exclude name="**/*.java"/>
         </fileset>
      </copy>
   </target>

   <target name="compile" depends="clean, copy-resources">
      <javac srcdir="${sourcedir}"
         destdir="${targetdir}"
         classpathref="libraries"/>
   </target>

   <target name="run" depends="compile">
      <java fork="true" classname="ergal.BusinessService" classpathref="libraries">
         <classpath path="${targetdir}"/>
         <arg value="${action}"/>
      </java>
   </target>
   <!-- create .java form  *.hbm.xml -->
   <target name="hbm2java" depends="compile"
      description="Generate Java source from the O/R mapping files">
      <taskdef name="hbm2java"
         classname="org.hibernate.tool.ant.HibernateToolTask"
         classpathref="project.class.path"/>
         <hbm2java destdir="${targetdir}">
            <configuration configurationfile="${targetdir}/hibernate.cfg.xml" /> 
            <hbm2java  jdk5="true"/>
            <!-- <cfg2hbm/> -->
         </hbm2java> 
   
   </target>
   
   <!-- create ddl form  *.hbm.xml -->
   <target name="hbm2ddl" depends="compile"
         description="Generate DB schema from the O/R mapping files">
      <taskdef name="hbm2ddl"
         classname="org.hibernate.tool.ant.HibernateToolTask"
         classpathref="project.class.path"/>
      <hbm2ddl destdir="${schema.dir}">
         <configuration configurationfile="${targetdir}/hibernate.cfg.xml" />
         <hbm2ddl export="true" console="false" create="true" update="false" drop="false" outputfilename="ahtest.sql"/>
      </hbm2ddl>   
   </target>

</project>



hibernate.cfg.xml


Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/ahtest</property>
        <property name="connection.username">root</property>
        <property name="connection.password">********</property>
       


        <!-- JDBC connection pool (use the built-in)-->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
   <property name="hibernate.c3p0.max_size">20</property>
   
   <property name="hibernate.c3p0.min_size">5</property>
   
   <property name="hibernate.c3p0.timeout">120</property>
   
   <property name="hibernate.c3p0.max_statements">100</property>
   
   <property name="hibernate.c3p0.idle_test_period">120</property>
   
   <property name="hibernate.c3p0.acquire_increment">2</property>


        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management-->
        <property name="current_session_context_class">thread</property>
   
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
   
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup-->
        <property name="hbm2ddl.auto">create</property>
   
        <mapping resource="ergal/Customer.hbm.xml"/>
        <mapping resource="ergal/Order.hbm.xml"/> 

    </session-factory>

</hibernate-configuration>


Customer.hbm.xml

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

<hibernate-mapping>

   <class name="ergal.Customer" table="CUSTOMERS">
      <id name="id" type="long" column="ID">
         <generator class="increment" />
      </id>
   
      <property name="name" type="string">
         <column name="NAME" length="15" />
      </property>
   </class>
</hibernate-mapping>


Customer.java

Code:
package ergal;
// Generated 2006-8-13 14:57:41 by Hibernate Tools 3.2.0.beta6a



/**
* Customer generated by hbm2java
*/
public class Customer  implements java.io.Serializable {

    // Fields   

     private long id;
     private String name;

     // Constructors

    /** default constructor */
    public Customer() {
    }

    /** full constructor */
    public Customer(String name) {
       this.name = name;
    }
   
   
    // Property accessors
    public long getId() {
        return this.id;
    }
   
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }

}


Order.java

Code:
package ergal;
// Generated 2006-8-13 14:57:41 by Hibernate Tools 3.2.0.beta6a



/**
* Order generated by hbm2java
*/
public class Order  implements java.io.Serializable {

    // Fields   

     private long id;
     private String orderNumber;
     private ergal.Customer customer;

     // Constructors

    /** default constructor */
    public Order() {
    }

   /** minimal constructor */
    public Order(ergal.Customer customer) {
        this.customer = customer;
    }
    /** full constructor */
    public Order(String orderNumber, ergal.Customer customer) {
       this.orderNumber = orderNumber;
       this.customer = customer;
    }
   
   
    // Property accessors
    public long getId() {
        return this.id;
    }
   
    public void setId(long id) {
        this.id = id;
    }
    public String getOrderNumber() {
        return this.orderNumber;
    }
   
    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }
    public ergal.Customer getCustomer() {
        return this.customer;
    }
   
    public void setCustomer(ergal.Customer customer) {
        this.customer = customer;
    }

}



BusinessService.java

Code:
package ergal;

import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class BusinessService
{
   public static SessionFactory sessionFactory;
   static
   {
      try
      {
         Configuration config = new Configuration();
         sessionFactory = config.configure().buildSessionFactory();
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
   
   public List findOrdersByCustomer(Customer customer)throws Exception
   {
      Session session=sessionFactory.openSession();
      Transaction tx=null;
      try
      {
         tx=session.beginTransaction();
         List orders = session.createQuery("from Order as o where o.customer.id=" + customer.getId()).list();
         tx.commit();
         return orders;
      }
      catch(Exception e)
      {
         if(tx!=null)
         {
            tx.rollback();
         }
         throw e;
      }
      finally
      {
         session.close();
      }
   }
   
   public Customer findCustomer(long customer_id)throws Exception
   {
      Session session=sessionFactory.openSession();
      Transaction tx=null;
      try
      {
         tx=session.beginTransaction();
         Customer customer = (Customer)session.load(Customer.class , new Long(customer_id));

         tx.commit();
         return customer;
      }
      catch(Exception e)
      {
         if(tx!=null)
         {
            tx.rollback();
         }
         throw e;
      }
      finally
      {
         session.close();
      }
   }
   
   public void saveCustomerAndOrderWithCascade()throws Exception
   {
      Session session=sessionFactory.openSession();
      Transaction tx=null;
      try
      {
         tx=session.beginTransaction();
         Customer customer = new Customer("wang");
         Order order1=new Order("wang_001", customer);
         Order order2=new Order("wang_002", customer);
         
         session.save(order1);
         session.save(order2);
         
         tx.commit();
      }
      catch(Exception e)
      {
         if(tx!=null)
         {
            tx.rollback();
         }
         throw e;
      }
      finally
      {
         session.close();
      }
   }
   
   public void saveCustomerAndOrder()throws Exception
   {
      Session session=sessionFactory.openSession();
      Transaction tx=null;
      try
      {
         tx=session.beginTransaction();
         Customer customer = new Customer("zhang");
         
         session.save(customer);
         
         Order order1=new Order("zhang_001", customer);
         Order order2=new Order("zhang_002", customer);
         
         session.save(order1);
         session.save(order2);
         
         tx.commit();
      }
      catch(Exception e)
      {
         if(tx!=null)
         {
            tx.rollback();
         }
         throw e;
      }
      finally
      {
         session.close();
      }
   }
   
   public void printOrders(List orders)throws Exception
   {
      for(Iterator it=orders.iterator(); it.hasNext();)
      {
         Order order=(Order)it.next();
         System.out.println("OrderNumber of "+order.getCustomer().getName()+ " :"+order.getOrderNumber());
      }
   }
   
   public void test()throws Exception
   {
      saveCustomerAndOrder();
      saveCustomerAndOrderWithCascade();
      Customer customer=findCustomer(1);
      List orders=findOrdersByCustomer(customer);
      printOrders(orders);
   }
   
   public static void main(String[] args)throws Exception
   {
      new BusinessService().test();
      sessionFactory.close();
   }
}



Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 13, 2006 2:11 pm 
Beginner
Beginner

Joined: Sun Aug 13, 2006 8:48 am
Posts: 23
I know what's the problem
The project could run normally now


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