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.  [ 4 posts ] 
Author Message
 Post subject: how to write the find query with composite key mapping
PostPosted: Mon Mar 07, 2005 10:25 pm 
Newbie

Joined: Wed Mar 02, 2005 3:05 am
Posts: 2
I have composite-id mapping, when I issue one find query,always get exception.

List beans = session.find("from Description d where d.id=? ",
(DESCRIPTIONPK) key, Hibernate.OBJECT);


Can somebody tell me the reason?

thanks first.


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

<hibernate-mapping package="jp.co.fuyoauto.batch.bean">
<class name="Description" table="DESCRIPTION">
<composite-id class="DESCRIPTIONPK" name="id">
<key-property
column="CODE"
name="code"
type="string"
/>
<key-property
column="CODE_DIVISION"
name="codeDivision"
type="string"
/>
</composite-id>
<property
column="UPDATE_TIME"
length="8"
name="updateTime"
not-null="false"
type="time"
/>
<property
column="CODE_NAME"
length="82"
name="codeName"
not-null="false"
type="string"
/>
<property
column="REG_DT"
length="10"
name="regDt"
not-null="false"
type="date"
/>
<property
column="REG_TIME"
length="8"
name="regTime"
not-null="false"
type="time"
/>
<property
column="UPDATE_DT"
length="10"
name="updateDt"
not-null="false"
type="date"
/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
List beans = session.find("from Description d where d.id=? ",
(DESCRIPTIONPK) key, Hibernate.OBJECT);
if ((beans != null) && beans.size() == 0) {
return null;
}
if ((beans != null) && beans.size() == 1) {
return beans.get(0);
}

Full stack trace of any exception that occurs:

net.sf.hibernate.MappingException: Unknown entity class: jp.co.fuyoauto.batch.bean.DESCRIPTIONPK
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:347)
at net.sf.hibernate.type.EntityType.toString(EntityType.java:80)
at net.sf.hibernate.type.ObjectType.toString(ObjectType.java:169)
at net.sf.hibernate.impl.Printer.toString(Printer.java:57)
at net.sf.hibernate.engine.QueryParameters.traceParameters(QueryParameters.java:119)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1538)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1527)
at jp.co.fuyoauto.batch.dao.DescriptionParser.loadBean(DescriptionParser.java:86)
at jp.co.fuyoauto.batch.dao.CSVBatch.processImport(CSVBatch.java:62)
at jp.co.fuyoauto.batch.dao.DescriptionParser.main(DescriptionParser.java:56)

Name and version of the database you are using:
db2 8.1
The generated SQL (show_sql=true):
no
Debug level Hibernate log excerpt:
info


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 12:24 pm 
Newbie

Joined: Thu Apr 07, 2005 7:21 pm
Posts: 12
I am seeing something similar: org.hibernate.MappingException: Unknown entity, and it's complaining about my composite identifier class.
I'm using Hibernate 3, mySql and using criteria query. The mapping file, persistent class and composite identifier class were generated by Middlegen.

Have you resolved this yet?

-m


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 1:42 pm 
Newbie

Joined: Mon Apr 11, 2005 2:08 pm
Posts: 8
To use a composite PK in a query, you have to implement a UserType for your Primary Key class:

Code:
public abstract class DESCRIPTIONPK_UserType implements UserType
{
    private static final int SQLTYPES[] =
       {
          Types.VARCHAR, // code
          Types.VARCHAR, // codeDivision
          Types.TIME,    // updateTime
          ....
       };
   
       
   
    public Object assemble(Serializable aCached, Object aOwner) throws HibernateException
    {
        return aCached;
    }


    public Object deepCopy(Object aValue) throws HibernateException
    {
        return aValue;
    }


    public Serializable disassemble(Object aValue) throws HibernateException
    {
        return (Serializable)aValue;
    }


    public boolean equals(Object aX, Object aY) throws HibernateException
    {
        return aX==aY;
    }


    public int hashCode(Object aX) throws HibernateException
    {
        if (aX==null)
            return 0;
        return aX.hashCode();
    }


    public boolean isMutable()
    {
        return false;
    }


    public Object nullSafeGet(ResultSet aRs, String[] aNames, Object aOwner)
            throws HibernateException, SQLException
    {
        String code = aRs.getString(aNames[0]);
        String codeDivision = aRs.getString(aNames[1]);
        ...

        return new DESCRIPTIONPK(.....);
    }


    public void nullSafeSet(PreparedStatement aSt, Object aValue, int aIndex)
            throws HibernateException, SQLException
    {
        if (aValue==null)
        {
            aSt.setNull(aIndex, Types.VARCHAR);
        }
        else
        {
            aSt.set....
        }
    }


    public Object replace(Object aOriginal, Object aTarget, Object aOwner)
            throws HibernateException
    {
        return null;
    }


    public Class returnedClass()
    {
        return enumType;
    }

   
    public int[] sqlTypes()
    {
        return SQLTYPES;
    }
}



The query looks like this:

Code:
Query q = session.createQuery("from Description d where d.id= :id");
q.setParameter("id", key, Hibernate.custom(DESCRIPTIONPK_UserType.class));

List result = q.list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 1:55 pm 
Newbie

Joined: Thu Apr 07, 2005 7:21 pm
Posts: 12
I got my query to work. I was following some instructions from another forum, where it was suggested to do something like ht.load( Application.class, applicationPK);

This was the source of my problem. When I omit this, everything works. I don't think my problem is quite the same as yours.

good luck,
m


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.