-->
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.  [ 1 post ] 
Author Message
 Post subject: Can sql-query hydrate an object with foreign keys?
PostPosted: Mon May 09, 2005 2:54 pm 
Newbie

Joined: Mon May 09, 2005 2:29 pm
Posts: 2
Location: Irvine, CA
I have a User class with foreign keys to a BillingEntity and a UserRole, each mapped with <many-to-one> tags. I am trying to create a named <sql-query> to find a set of Users with a complex constraint. However, I don't know how to map the ENTITY_ID or ROLE_ID columns to the Id fields of the referenced classes.

So, first of all, is this possible?

And second, if it's not too much trouble, what's the syntax for mapping the return-property to a field of a lazy-referenced entity? I tried the obvious {user.entity.id} but this resulted in an "No column name found for property [entity.id]" exception.

Thanks!!!

Hibernate version:3.0

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

<hibernate-mapping>
    <class name="com.primal.model.UserProperty" table="adm_user_property">
        <id name="id" column="property_id" unsaved-value="null">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" not-null="true"/>
    </class>
   
    <class name="com.primal.model.UserRole" table="adm_role">
        <id name="id" column="role_id" unsaved-value="null">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" not-null="true"/>
        <property name="description" column="description"/>
    </class>
   
    <class name="com.primal.model.User" table="adm_user">
        <id name="id" column="user_id" unsaved-value="null">
            <generator class="increment"/>
        </id>
        <property name="loginName" column="username" not-null="true"/>
        <property name="password" column="password" not-null="true"/>
        <property name="passwordQuestion" column="password_question"/>
        <property name="passwordAnswer" column="password_answer"/>
        <property name="firstName" column="first_name"/>
        <property name="middleName" column="middle_name"/>
        <property name="lastName" column="last_name"/>
   <property name="emailAddress" column="email"/>
   <property name="enabled" column="enabled" type="true_false" not-null="true"/>

   <many-to-one name="entity" column="entity_id" class="com.primal.model.BillingEntity" lazy="true"/>

   <many-to-one name="userRole" column="role_id" class="com.primal.model.UserRole" lazy="false"/>

   <set name="properties" table="adm_user_property_value">
         <key column="user_id"/>
         <composite-element class="com.primal.model.UserPropertyValue">
                <many-to-one name="property" column="property_id"/>
                <property name="value"/>
         </composite-element>
   </set>
      
   <property name="startDate" column="start_date" not-null="true"/>
   <property name="endDate" column="end_date" not-null="true"/>      
   <property name="createDate" column="created_date" not-null="true"/>
   <property name="createdBy" column="created_by" not-null="true"/>      
   <property name="modifiedDate" column="modified_date"/>
   <property name="modifiedBy" column="modified_by"/>      
    </class>

    <sql-query name="queryConstrainedUsers">
   <return alias="user" class="com.primal.model.User"/>
   
   select  user_id           as {user.id},
      username          as {user.loginName},
      password          as {user.password},
      password_question as {user.passwordQuestion},
      password_answer   as {user.passwordAnswer},
      last_name         as {user.lastName},
      first_name        as {user.firstName},
      middle_name       as {user.middleName},
      email             as {user.emailAddress},
      enabled           as {user.enabled},
      start_date        as {user.startDate},
      end_date          as {user.endDate},
      entity_id         as {user.entity.id},
      role_id           as {user.userRole.id},
      created_date      as {user.createDate},
      created_by        as {user.createdBy},
      modified_date     as {user.modifiedDate},
      modified_by       as {user.modifiedBy}
   from    adm_user
   where   user_id = ?
    </sql-query>

</hibernate-mapping>


Name and version of the database you are using:Oracle 9i

Exception:
Code:
    [java] org.hibernate.QueryException: No column name found for property [entity.id] [select user_id as {user.id}, use
rname as {user.loginName}, password as {user.password}, password_question as {user.passwordQuestion}, password_answer as
{user.passwordAnswer}, last_name as {user.lastName}, first_name as {user.firstName}, middle_name as {user.middleName},
email as {user.emailAddress}, enabled as {user.enabled}, start_date as {user.startDate}, end_date as {user.endDate}, ent
ity_id as {user.entity.id}, role_id as {user.userRole.id}, created_date as {user.createDate}, created_by as {user.create
dBy}, modified_date as {user.modifiedDate}, modified_by as {user.modifiedBy} from adm_user where user_id = ?]
    [java]      at org.hibernate.loader.custom.SQLQueryParser.resolveProperties(SQLQueryParser.java:182)
    [java]      at org.hibernate.loader.custom.SQLQueryParser.substituteBrackets(SQLQueryParser.java:124)
    [java]      at org.hibernate.loader.custom.SQLQueryParser.process(SQLQueryParser.java:71)
    [java]      at org.hibernate.loader.custom.SQLCustomQuery.<init>(SQLCustomQuery.java:128)
    [java]      at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:142)
    [java]      at org.springframework.orm.hibernate3.HibernateTemplate$32.doInHibernate(HibernateTemplate.java:812)
    [java]      at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:310)
    [java]      at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:803)
    [java]      at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:799)
    [java]      at com.primal.dao.hibernate.UserDAOHibernate.queryConstrainedUsers(UserDAOHibernate.java:100)
    [java]      at com.primal.dao.UserDAOTest.testQueryConstrainedUsers(UserDAOTest.java:152)
    [java]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [java]      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [java]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [java]      at java.lang.reflect.Method.invoke(Method.java:585)
    [java]      at junit.framework.TestCase.runTest(TestCase.java:154)
    [java]      at junit.framework.TestCase.runBare(TestCase.java:127)
    [java]      at junit.framework.TestResult$1.protect(TestResult.java:106)
    [java]      at junit.framework.TestResult.runProtected(TestResult.java:124)
    [java]      at junit.framework.TestResult.run(TestResult.java:109)
    [java]      at junit.framework.TestCase.run(TestCase.java:118)
    [java]      at junit.framework.TestSuite.runTest(TestSuite.java:208)
    [java]      at junit.framework.TestSuite.run(TestSuite.java:203)
    [java]      at junit.swingui.TestRunner$16.run(TestRunner.java:623)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.