-->
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.  [ 5 posts ] 
Author Message
 Post subject: Many-to-one in objects within Set always null
PostPosted: Tue Jun 14, 2005 1:04 pm 
Newbie

Joined: Thu Dec 09, 2004 8:02 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Hibernate Mapping Files
(relevant parts of 4 different hbm.xml files)
Batch.hbm.xml
Code:
<hibernate-mapping>
     <class name="myproject.dataobject.Batch"
       table="batch">
         <id name="id"
            column="batch_id" type="long">
             <generator class="native" />
         </id>
        <set name="transactions" inverse="true" cascade="save-update">
            <key column="batch_id" />
            <one-to-many class="myproject.dataobject.Transaction" />
        </set>
     </class>
</hibernate-mapping>

transaction.hbm.xml
Code:
<hibernate-mapping package="myproject.dataobject">
    <class name="Transaction" table="TRANSACTION">
        <id
            column="TRANSACTION_ID"
            name="id"
            type="long"
            >
            <generator class="native" />
        </id>
        <many-to-one name="batch"
            not-null="true"
            class="myproject.dataobject.Batch"
            cascade="save-update"
            >
            <column name="BATCH_ID" />
        </many-to-one>
        <many-to-one name="errorCode"
            class="myproject.dataobject.lookup.StatusCode"
            column="ERROR_CODE"
            />
        <many-to-one name="transactionCurrency"
            class="myproject.dataobject.Currency"
            column="TRANSACTION_CURRENCY_CODE"
            />
    </class>
</hibernate-mapping>

StatusCode.hbm.xml
Code:
<hibernate-mapping package="myproject.dataobject">
   <class name="StatusCode" table="STATUS_CODE">
      <id
         column="ERROR_CODE"
         name="id"
         type="integer"
      >
         <generator class="native" />
      </id>
      <property
         column="ERROR_DESCRIPTION"
         length="50"
         name="description"
         not-null="false"
         type="string"
       />
   </class>
</hibernate-mapping>

Currency.hbm.xml
Code:
<hibernate-mapping>
  <class name="myproject.dataobject.Currency"
      table="currency">
      <id name="isoCode" column="iso_code" type="integer">
          <generator class="sequence">
              <param name="sequence">acquirer_comm_seq</param>
          </generator>
      </id>
      <property name="swiftCode"
          column="swift_code"
          not-null="true"
          type="string"
          unique="false"
          />
      <property name="name"
          column="description"
          not-null="true"
          type="string"
          unique="false"
          />
      <property name="minorUnits"
          column="minor_units"
          not-null="true"
          type="integer"
          unique="false"
          />
  </class>
</hibernate-mapping>


Name and version of the database you are using:
Oracle 10.1.0.3.0

Hi,
I'm having some difficulty getting a many-to-one relation initialized. I'm subclassing spring's HibernateDaoSupport class in the class involved.
What I have is a Batch that contains a Set of transactions. In a certain method I want to load a batch using hibernate by calling the getHibernateTemplate().findByNamedParam() method, passing in the id of the batch. I also need the set of transactions in this case, which I get back by doing a left join fetch in the hql passed in to the spring method mentioned.
This all works fine. However here I get into trouble. Each Transaction has 2 many-to-one relations (a transactionCurrency and an errorCode) that I want initialized by this query. I thought the best way to do this was by iterating through the transactions and calling a Hibernate.initialize() for each many-to-one I was interested in initializing. However this seems only to work for the transactionCurrency many-to-one (even though the hibernate-generated select SQL for both many-to-ones appears on the console).
The errorCode object is always a cglib proxy with null values for both its fields. I'm sure I'm doing something stupid here or there is a trivial mistake in my mappings. I'd be grateful if anyone could point any problem out, cause I can't see it.
Here's the spring code
Code:
    public Batch getBatchWithTransactions(final Long batchID) {
        return (Batch) getHibernateTemplate().execute(new
HibernateCallback() {
            public Object doInHibernate(final Session session) {
                Batch batch =  (Batch) session.createQuery(
                        "from com.fexcodcc.dais.dataobject.Batch b "
                        + "left join fetch b.transactions "
                        + "where b.id
= :batchID").setParameter("batchID", batchID)
                        .list().get(0);
                Iterator txns = batch.getTransactions().iterator();
                while (txns.hasNext()) {
                    Transaction t = (Transaction) txns.next();
                    Hibernate.initialize(t.getErrorCode());
                    Hibernate.initialize(t.getTransactionCurrency());
                }
                return batch;
            }
        });
    }

TIA
Denis


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 11:05 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Have you tried:

Code:
Batch batch = (Batch) getHibernateTemplate().get(Batch.class, batchID);


Maybe I'm missing something, but I don't think you need the HQL query here.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 11:07 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Also, if you are using Spring's transaction management infrastructure and are creating the transaction outside of this method call, you can avoid the doInHibernate call altogether.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 4:31 am 
Newbie

Joined: Thu Dec 09, 2004 8:02 am
Posts: 5
Hi,
The reason for the hql is that I need to populate the transactions set in the batch.
I've tried using the Spring TransactionProxyFactoryBean wrapper as well but with the same result - the first many-to-one is populated, but the second is left as an object with null fields (even though the sql is executed by hibernate_


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 7:20 am 
Newbie

Joined: Thu Dec 09, 2004 8:02 am
Posts: 5
Aargh! The problem was that getters for the StatusCode class were final. Silly me. Thanks anyway


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