-->
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.  [ 9 posts ] 
Author Message
 Post subject: Finding a column's Java attribute name?
PostPosted: Wed Aug 27, 2003 4:47 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
First my question is pointless if there already is query by example functionality somewhere in Hibernate and I just can't seem to find it.

Assuming there isn't any existing query by example functionality, I'm building it. However, I need to access each column's Java attribute name. I'm getting the columns like this:
Code:
Class stateClass = state.getClass();
Iterator columnItr = config.getClassMapping(stateClass).getTable().getColumnIterator();
while (columnItr.hasNext()) {
    Column column = (Column) columnItr.next();
    String columnName = column.getName();
    String fieldName = column.getFieldName(); // <-- doesn't exist
    // now use reflection to add all non-null attributes to the where clause
    ReflectHelper.Getter getter = null;
    try {
        getter = ReflectHelper.getGetter(stateClass, fieldName);
        value = getter.get(state);
        type = TypeFactory.hueristicType(getter.getReturnType().getName());
    } catch (PropertyNotFoundException ignore) {
        // ignore
    } catch (HibernateException he) {
        throw new DAOException(he);
    }
    // other where clause building stuff continues here
    // ...
}

I'm looking for something like
Code:
String fieldName = column.getFieldName();

I checked the source code and don't even see that being stored in the Column class after the mapping is read. Is there an alternate way to get that information, or is this something that might be added in the future? If this seems like a reasonable request, I might even be able to submit a patch or two.

Thanks,

Eric


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 4:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I STRONGLY recommend that if you want query-by-example, built it as a Criterion (ie. as part of the Criteria API). Then you will not need to do any of this yucky stuff!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 4:53 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
I'll take a look at that tomorrow. Time to go home. :)
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 4:58 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
Actually, now that I'm looking at the API, I remember being here before. This scared me off:
Quote:
Hibernate's query language is much more general and should be used for non-simple cases.
This is an experimental API

When I saw that I decided to build the HQL manually. If this is going to stick around and not fundamentally change in later releases, I'll give it another look.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 5:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The criteria query stuff is now working really well. All the "experimental" mark means is: I reserve the right to change something that turns out to be Bad.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 10:26 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
OK, I've got Criteria stuff (mostly) working for query-by-example and the code is simpler than building HQL, so that's good. However, I can't figure out how to build the correct criteria to do something like this:
Code:
TariffState tariffState = new TariffState();
TariffDelegate tariffDelegate = new TariffDelegate();
TariffPlaceState tariffPlace = new TariffPlaceState();
tariffPlace.setBillParentCode("DOD%");
Set tariffPlaces = new HashSet(1);
tariffPlaces.add(tariffPlace);
tariffState.setTariffPlaceStates(tariffPlaces);
System.out.println("Retrieving Tariffs w/ DOD% as bill parent");
PersistentStateList tariffs = tariffDelegate.find(tariffState);
for (PersistentStateIterator itr = tariffs.iterator(); itr.hasNext();) {
    tariffState = (TariffState) itr.next();
    System.out.println(tariffState);
}

Here's the relevant parts of my mappings:
Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class
      name="com.*****.cc.objectlib.tariff.TariffState"
      schema="eousr"
      table="TARIFF">
    <meta attribute="extends">com.*****.cc.objectlib.PersistentState</meta>
    <id name="id" type="java.lang.Long">
      <column name="TARIFF_ID"/>
      <generator class="assigned"/>
    </id>
    <version
        name="lockCol"
        type="long"
        column="LOCK_COL"/>
    <property
        name="name"
        type="java.lang.String"
        column="NAME"
        length="30">
      <meta attribute="use-in-tostring">true</meta>
    </property>
<!-- other properties deleted -->
    <!-- associations -->
    <!-- bi-directional one-to-many association to TariffPlaceState -->
    <set
        name="tariffPlaceStates"
        lazy="true"
        inverse="true">
      <key>
        <column name="TARIFF_ID"/>
      </key>
      <one-to-many
          class="com.*****.cc.objectlib.tariff.TariffPlaceState"/>
    </set>
<!-- other associations deleted -->
</hibernate-mapping>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

  <class
      name="com.*****.cc.objectlib.tariff.TariffPlaceState"
      schema="eousr"
      table="TARIFF_PLACE">
    <meta attribute="extends">com.*****.cc.objectlib.PersistentState</meta>
    <composite-id name="id" class="com.*****.cc.objectlib.tariff.TariffPlaceIdentity">
      <key-property name="seqId" column="SEQ_ID" type="java.lang.Long"/>
      <key-property name="tariffId" column="TARIFF_ID" type="java.lang.Long"/>
      <!-- <meta attribute="extends">com.*****.cc.objectlib.CompoundIdentity</meta> -->
    </composite-id>
    <version
        name="lockCol"
        type="long"
        column="LOCK_COL"/>
    <property
        name="billParentCode"
        type="java.lang.String"
        column="BILL_PARENT_CD"
        length="8">
      <meta attribute="use-in-tostring">true</meta>
    </property>
<!-- other properties deleted -->
    <!-- associations -->
    <!-- bi-directional many-to-one association to TariffState -->
    <many-to-one
        name="tariffState"
        class="com.fedex.cc.objectlib.tariff.TariffState"
        not-null="true"
        update="false"
        insert="false">
      <column name="TARIFF_ID"/>
    </many-to-one>
  </class>
</hibernate-mapping>


Currently, my criteria looks like this (System.out.println(criteria);)
[tariffPlaceStates.billParentCode like DOD%]
But that throws an Exception:
Code:
Exception in thread "main" net.sf.hibernate.QueryException: unresolved property: tariffPlaceStates.billParentCode [null]
Start server side stack trace:
net.sf.hibernate.QueryException: unresolved property: tariffPlaceStates.billParentCode [null]
        at net.sf.hibernate.persister.EntityPersister.toColumns(EntityPersister.java:965)
        at net.sf.hibernate.expression.Expression.getColumns(Expression.java:282)
        at net.sf.hibernate.expression.SimpleExpression.toSqlString(SimpleExpression.java:24)
        at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:43)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3149)
        at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:65)
        at com.*****.cc.objectlib.dao.HibernateDAO.find(HibernateDAO.java:269)


Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 11:01 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
I tried it another way. First query-by-example on the child, then pass that list to the parent.
Code:
TariffState tariffState = new TariffState();
TariffDelegate tariffDelegate = new TariffDelegate();
System.out.println("Retrieving Tariffs w/ DOD% as bill parent");
TariffPlaceState tariffPlace = new TariffPlaceState();
tariffPlace.setBillParentCode("DOD%");
PersistentStateList places = tariffDelegate.find(tariffPlace);
Set tariffPlaces = new HashSet(places.getList());
tariffState.setTariffPlaceStates(tariffPlaces);
PersistentStateList tariffs = tariffDelegate.find(tariffState);
for (PersistentStateIterator itr = tariffs.iterator(); itr.hasNext();) {
    tariffState = (TariffState) itr.next();
    System.out.println(tariffState);
}

In my Criteria builder, I'm using
public static Expression in(String propertyName, Collection values)
and that's giving me this Criteria:
[tariffPlaceStates in (com.*****.cc.objectlib.tariff.TariffPlaceState... (huge list follows))]
Now I'm getting a different Exception:
Code:
[WARN] JDBCExceptionReporter - -SQL Error: 1008, SQLState: 72000
[ERROR] JDBCExceptionReporter - -ORA-01008: not all variables bound
[ERROR] JDBCExceptionReporter - -SQLException occurred <java.sql.SQLException: ORA-01008: not all variables bound
>java.sql.SQLException: ORA-01008: not all variables bound
        at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
        at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
        at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:573)
        at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1891)
        at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:830)
        at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2391)
        at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2672)
        at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:589)
        at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:527)
        at weblogic.jdbc.pool.PreparedStatement.executeQuery(PreparedStatement.java:51)
        at weblogic.jdbc.rmi.internal.PreparedStatementImpl.executeQuery(PreparedStatementImpl.java:56)
        at weblogic.jdbc.rmi.SerialPreparedStatement.executeQuery(SerialPreparedStatement.java:42)
        at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:71)
        at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:551)
        at net.sf.hibernate.loader.Loader.doFind(Loader.java:140)
        at net.sf.hibernate.loader.Loader.find(Loader.java:620)
        at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:81)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3157)
        at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:65)
        at com.*****.cc.objectlib.dao.HibernateDAO.find(HibernateDAO.java:269)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 3:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I have added a QBE API to the current CVS (v21branch).



You need to use:

Code:
Criteria.addJoin()



or

Code:
Criteria.createCriteria()



if you wish to navigate associations in Hibernate QBC API.


Top
 Profile  
 
 Post subject: Re: Finding a column's Java attribute name?
PostPosted: Sun Apr 15, 2012 6:03 am 
Newbie

Joined: Sun Apr 15, 2012 6:01 am
Posts: 1
that code is awesome ))


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