Hi.
I have an entity which also defines a SqlResultSetMapping:
Code:
package model;
@Entity
@Table(name = "BIDDER")
@SqlResultSetMapping(name = "test",
entities = @EntityResult(entityClass = Bidder.class))
public class Bidder {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BIDDER_GENERATOR")
@SequenceGenerator(name = "BIDDER_GENERATOR", sequenceName = "BIDDER_SEQ", allocationSize = 1)
private long id;
@Column(name = "NAME")
private String name;
/* package */Bidder() {
// default
}
public Bidder(String name) {
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Bidder: " + name + ", id = " + id;
}
}
Then, I execute a native SQL query:
Code:
Query q = em.createNativeQuery("select id from bidder where id = 999", "test");
Bidder bidder = (Bidder) q.getSingleResult();
and an exception is thrown, and the log output is:
Code:
INFO: 13:13:30,933 DEBUG SQL:111 - select * from ( select id from bidder where id = 999 ) where rownum <= ?
INFO: Hibernate: select * from ( select id from bidder where id = 999 ) where rownum <= ?
INFO: 13:13:30,956 TRACE LongType:193 - returning '999' as column: ID
INFO: 13:13:31,004 INFO StringType:203 - could not read column value from result set: NAME; Invalid column name
INFO: 13:13:31,019 WARN JDBCExceptionReporter:100 - SQL Error: 17006, SQLState: null
INFO: 13:13:31,025 ERROR JDBCExceptionReporter:101 - Invalid column name
When I change the SqlResultSetMapping to be:
Code:
@Entity
@Table(name = "BIDDER")
@SqlResultSetMapping(name = "test",
entities = @EntityResult(
entityClass = Bidder.class,
fields = @FieldResult(name = "id", column = "ID")))
the only thing that changes is the alias for the NAME column:
Code:
INFO: 13:23:04,502 DEBUG SQL:111 - select * from ( select id from bidder where id = 999 ) where rownum <= ?
INFO: Hibernate: select * from ( select id from bidder where id = 999 ) where rownum <= ?
INFO: 13:23:04,730 TRACE LongType:193 - returning '999' as column: ID
INFO: 13:23:04,748 INFO StringType:203 - could not read column value from result set: NAME1_0_; Invalid column name
INFO: 13:23:04,754 WARN JDBCExceptionReporter:100 - SQL Error: 17006, SQLState: null
INFO: 13:23:04,754 ERROR JDBCExceptionReporter:101 - Invalid column name
In Enterprise JavaBeans by Bill Burke, example 9.3.3.1, they do a similar thing - they map an entity but specify only a few columns.
Looks like Hibernate is trying to extract all fields from the entity, even though the SQL retrieves only 1 field. And why is the "where rownum <= ?" used in the query? This doesn't seem to be relevant here.
What am I doing wrong?
DB: Oracle XE 10g
OS: Kubuntu 8.04, amd64
Hibernate core: 3.3.0.SP1 (bundled in entitymanager download)
Hibernate EntityManager: 3.4.0.GA
Application server: Glassfish v2