Hi, I'm relatively new to Hibernate. While trying to retrieve values from two tables I'm getting the following error.
Error: org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped. [Select C.customerName from Customer C, Account A WHERE A.id.customerId = C.customerId AND A.id.serialId = '2' AND A.id.accountId='102' ] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:157) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87) at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70) at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:265) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3049) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
Main method: CustomerDAO obj = new CustomerDAO(); Customer customerObj = obj.findByProp(2,102).get(0); String customerName = customerObj.getCustomerName(); System.out.println("customerName ::" + customerName);
Method in CustomerDAO: public List<Customer> findByProp(Integer serialId, Integer accountId){ String strHQL = "Select C.customerName from Customer C, Account A WHERE A.id.customerId = C.customerId AND A.id.serialId = '%s' AND A.id.accountId='%s' "; strHQL = String.format(strHQL, serialId, accountId); Query queryObject = getSession().createQuery(strHQL); return queryObject.list(); }
Customer.hbm.xml:
<hibernate-mapping> <class name="com.pojo.Customer" table="CUSTOMER" schema="SYSTEM"> <id name="customerId" type="java.lang.Integer"> <column name="CUSTOMER_ID" precision="22" scale="0" /> <generator class="assigned" /> </id> <property name="customerName" type="java.lang.String"> <column name="CUSTOMER_NAME" length="30" /> </property> <property name="customerNumber" type="java.lang.Integer"> <column name="CUSTOMER_NUMBER" precision="22" scale="0" /> </property> <set name="accounts" inverse="true"> <key> <column name="CUSTOMER_ID" precision="22" scale="0" not-null="true" /> </key> <one-to-many class="com.pojo.Account" /> </set> </class> </hibernate-mapping>
Account.hbm.xml:
<hibernate-mapping> <class name="com.pojo.Account" table="ACCOUNT" schema="SYSTEM"> <composite-id name="id" class="com.pojo.AccountId"> <key-property name="serialId" type="java.lang.Integer"> <column name="SERIAL_ID" precision="22" scale="0" /> </key-property> <key-property name="accountId" type="java.lang.Integer"> <column name="ACCOUNT_ID" precision="22" scale="0" /> </key-property> <key-many-to-one name="customer" class="com.pojo.Customer"> <column name="CUSTOMER_ID" precision="22" scale="0" /> </key-many-to-one> </composite-id> </class> </hibernate-mapping>
com.pojo.AccountId:
// Fields
private Integer serialId; private Integer accountId; private Customer customer;
// Constructors
/** default constructor */ public AccountId() { }
/** full constructor */ public AccountId(Integer serialId, Integer accountId, Customer customer) { this.serialId = serialId; this.accountId = accountId; this.customer = customer; }
Can anyone please help me on this? Is there any problem with my HQL/SQL query ?
|