-->
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: Derived Property issue in Hibernate
PostPosted: Sun Oct 12, 2008 4:03 pm 
Newbie

Joined: Sat Dec 29, 2007 9:58 am
Posts: 5
Hi ,

iam defining one derived propery in Emp.hbm.xml like below.

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

<hibernate-mapping package="events">
<class name="Emp" table="EMP">
<meta attribute="sync-DAO">false</meta>
<id name="empno" type="int" column="EMPNO">
<generator class="native">
<!-- <param name="sequence">emp_sequence</param> -->
</generator>
</id>
<property name="ename" type="string" column="ENAME"/>
<property name="job" type="string" column="JOB"/>
<property name="mgr" type="int" column="MGR"/>
<property name="hiredate" type="timestamp" column="HIREDATE"/>
<property name="sal" type="double" column="SAL"/>
<property name="comm" type="double" column="COMM"/>
<property name="deptno" type="int" column="DEPTNO" insert="false" update="false" />

<property name="empderived" type="string" formula="select sum(e.sal+e.comm) from Emp e where e.ename = ename" />

<many-to-one name="dept" class="Dept" column="DEPTNO"/>

</class>
</hibernate-mapping>

Here is the code snippet iam using to access the Emp object.

Session session = m.getSession();
Transaction txn = session.beginTransaction();
Emp list = (Emp) session.load(Emp.class, 7782);
System.out.println(list.getEmpderived());

Iam getting the following exception:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not load an entity: [events.Emp#7782]

can someone correct me where iam going wrong?

Thanks in advance.

Thanks,
Raj


Top
 Profile  
 
 Post subject: Re: Derived Property issue in Hibernate
PostPosted: Mon Oct 13, 2008 9:28 pm 
Newbie

Joined: Fri Jul 14, 2006 12:05 pm
Posts: 19
Raj,

I believe that the formula has to be enclosed in parentheses because it's doing a subquery.

Also, it probably makes more sense to declare the data type to be some numeric type (BigDecimal?) because it's a sum, although I doubt that's causing the problem.

So, try something like this (untested):

Code:
<property name="empderived" type="double" formula="(select sum(e.sal+e.comm) from Emp e where e.ename = ename)" />


I used double because that's what your mapping of sal used, but you really want to be using BigDecimal for money fields.

Good luck,
Oscar


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.