-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using native sql when working with embeddable entities
PostPosted: Mon Jun 19, 2006 4:37 am 
Newbie

Joined: Sat May 06, 2006 6:31 am
Posts: 9
Hi,
I have the following relation - Person which owns PersonAddress (as embeddable). This relation is optional.

I'm trying to build native sql query (small as possible) in order to load Person objects.

The result query as following :
Select id,name from Person
I don't want to load optional PersonAddress

when running query.list() - I'm getting the following error:
could not read column value from result set: street; Invalid column name
('street' - is one of PersonAddress fields, it is optional field)

How can I avoid PersonAddress loading ?

Thank you,
Igor

Hibernate version: 3.2


Full stack trace of any exception that occurs:



Caused by: java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3287)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1914)
at oracle.jdbc.driver.OracleResultSet.getLong(OracleResultSet.java:1575)
at org.hibernate.type.LongType.get(LongType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:102)
at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:95)
at org.hibernate.type.ComponentType.hydrate(ComponentType.java:506)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:1983)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1372)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1300)
at org.hibernate.loader.Loader.getRow(Loader.java:1197)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 1:35 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You cannot do that. Java is strongly typed: all objects must be exactly the type they are declared to be, including all fields. Values in those fields may be optional, though.

There are alternatives:
  1. Use "select new Person(id, name) from ...", though this is HQL, not SQL. You'll also have to write the correct constructor for Person, obviously.
  2. Use plain old HQL, and don't use fetch join: hibernate will create a proxy object and not load the PersonAddress.
Hibernate's main job is to do what you're trying to do here. You've chosen to not reinvent the wheel by having hibernate do all the grunt work for you, but here you are, reinventing the wheel. Don't try to optimize hibernate in this way: it's already optimal. If you really, really want to be this explicit about everything, you're better off dropping hibernate and doing it all manually.

_________________
Code tags are your friend. Know them and use them.


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