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() );
}
}