-->
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.  [ 7 posts ] 
Author Message
 Post subject: ID mapped to wrong property
PostPosted: Wed Dec 07, 2005 10:45 pm 
Newbie

Joined: Wed Dec 07, 2005 9:32 pm
Posts: 3
Location: Perth, Australia
Hello

I have a class with a property whose corresponding database column name is similar to that of the class ID. Hibernate is mapping the ID’s value into the property instead of the property’s actual database value.

Code:
class-field: id          db-column: POSTCODE_ID
class-field: postcode   db-column: POSTCODE


e.g. I expect to get an object with
Code:
id: 420
postcode: 6532
localityName: northampton


what I get is

Code:
id: 420
postcode: 420
localityName: northampton


My database is Oracle10 and my Hibernate version is 3.05. The SQL dialect configured is org.hibernate.dialect.OracleDialect. Is this a feature or a bug? Has anyone else seen something similar and know a workaround?

I have triple checked my configuration and it looks correct. I inspected the hibernate generated SQL and it looks wrong. It did not retrieve the postcode db-column value at all.

Code:
select * from ( select postcode0_.POSTCODE_ID as POSTCODE1_, postcode0_.LOCALITY_NAME as LOCALITY2_1_ from SYS_POSTCODES postcode0_ ) where rownum <= ?



The following is my database schema, hibernate config file, object mapping file, class file( getters/setters omitted to ease readability) and test code respectively.

Code:
CREATE TABLE SYS_POSTCODES
(
  POSTCODE_ID    NUMBER(6)      PRIMARY KEY,
  POSTCODE       VARCHAR2(10 BYTE)   NOT NULL,
  LOCALITY_NAME  VARCHAR2(30 BYTE)   NOT NULL
)


Code:
<hibernate-configuration>
    <session-factory>

        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@testserv78:1521:devdb10g</property>
        <property name="connection.username">devscratch</property>
        <property name="connection.password">devscratch</property>
        <property name="connection.pool_size">10</property>
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>

        <mapping resource="Postcode.hbm.xml"/>
               
    </session-factory>
</hibernate-configuration>


Code:
<hibernate-mapping>
    <class name="Postcode" table="SYS_POSTCODES">
        <id
            name="id"
            column="POSTCODE_ID"
            type="java.lang.Long"
            unsaved-value="null" >
            <generator class="increment"/>
        </id>

        <property
            name="postcode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="POSTCODE"
            not-null="true" />

        <property
            name="localityName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="LOCALITY_NAME"
            not-null="true" />
    </class>

</hibernate-mapping>




Code:
public class Postcode
{
   private Long id;
   private String postcode;
   private String localityName;
   
   public Postcode()
   {
      super();
   }   
}


Code:
public void testPostcodeMappings(){
        List postcodes = session.createQuery("from Postcode")
                          .setMaxResults(5)       
                          .list();
       
        for( int i=0; i < postcodes.size() ; i++){
           Postcode pc = (Postcode)postcodes.get(i);
           System.out.println( "ID:"+pc.getId()
                                 +" Postcode:"+pc.getPostcode()
                                 +" Locality:"+pc.getLocalityName() );
           
        }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 12:36 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Postcode doesn't implement Serializable ... you're sure this is the whole story?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 2:25 am 
Newbie

Joined: Wed Dec 07, 2005 9:32 pm
Posts: 3
Location: Perth, Australia
Hello Dennis


The code posted here is from a stripped down test rig I made from my main application to isolate the mapping problem. I chopped off the Serialization declaration by accident when I was cutting out all the nonrelevant code. However it actually works exactly the same with and without the Serialization declaration, I've actually tested it.

The test rig itself is barebones, with just enough code and config files to run it from a console. I've chopped off a couple of session management lines from the original posted test function to make it more readable.

The following is the full code of my test rig and a utility I got off a hibernate tutorial site. As you can see there is barely anything there which can possibly interfere with the results.

Code:
import java.util.List;

import org.hibernate.Session;

public class TestManager {

    public static void main(String[] args) {
        TestManager mgr = new TestManager();
       
        mgr.testPostcodeMappings();
       
        HibernateUtil.sessionFactory.close();
    }

    private void testPostcodeMappings() {
        Session session = HibernateUtil.currentSession();
   
        List postcodes = session.createQuery("from Postcode")
                          .setMaxResults(5)       
                          .list();
       
        for( int i=0; i < postcodes.size() ; i++){
           Postcode pc = (Postcode)postcodes.get(i);
           System.out.println( "ID:"+pc.getId()
                          +" Postcode:"+pc.getPostcode()
                          +" Locality:"+pc.getLocalityName());
           
        }

        HibernateUtil.closeSession();
    }         
}


Code:
import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

    public static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            // Store it in the ThreadLocal variable
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s != null)
            s.close();
        session.set(null);
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 3:13 am 
Newbie

Joined: Tue Sep 09, 2003 3:39 am
Posts: 14
Location: Atlanta, GA
dennisbyrne wrote:
Postcode doesn't implement Serializable ... you're sure this is the whole story?


It doesn't have to.

That restritction is for composite ids.

A basic persistent pojo like this one doesn't have to implement or override anything special.

_________________
John Lindsey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 3:48 am 
Newbie

Joined: Tue Sep 09, 2003 3:39 am
Posts: 14
Location: Atlanta, GA
For what it is worth Hawke, I tried your setup (albeit with postgres) and things worked fine.

Have you tried renaming your postcode or postcode_id columns/properties? Maybe to something that does not have "id" in the name?

_________________
John Lindsey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 4:04 am 
Newbie

Joined: Wed Dec 07, 2005 9:32 pm
Posts: 3
Location: Perth, Australia
Hi Nisroc

Thank you for running the test with Postgres. It gave me a good clue.

I just ran another experiment with HypersonicDB , changing only the absolute minimum amount of config and it works correctly. This indicates the problem might be in Oracle dialect implementation in the Hibernate 3.0 series. Your test with Posgres seems to support this.

I ran some extra test with Oracle, but with Hibernate 3.02 and 3.1rc3. The 3.02 version shows the same erroneous results as 3.05. However 3.1rc3 works correctly. No configuration or code was changed, just a direct replacement of the jars.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 4:12 am 
Newbie

Joined: Tue Sep 09, 2003 3:39 am
Posts: 14
Location: Atlanta, GA
No worries, glad you found the issue. I just noticed your topic because I had a problem that *smelled* sort of similiar once with an id column getting mis-mapped to something else (though in my case it was by-design and not a bug).

http://forum.hibernate.org/viewtopic.php?t=932155

Maybe some sort of wonky stuff with the id keyword is going on in the 3.0x series of the Oracle dialect?

_________________
John Lindsey


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