-->
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: Strange createSQLQuery() method behaviour
PostPosted: Thu Nov 30, 2006 12:36 pm 
Newbie

Joined: Thu Nov 30, 2006 11:44 am
Posts: 3
Location: Ukraine
Hello,

I'm newcomer of this forum. And I think You can give me advise about problem below.

Thanks in advance!

Best regards,
Alex

Hibernate version: 3.2

Mapping documents:

I have annotated Java class:

@Entity
@Table(name = "TEST", uniqueConstraints = {})
public class Test {
////////////////////////////////////////////////
// Fields
////////////////////////////////////////////////
private Integer id;
private Integer klasse;
private Integer parentId;
///////////////////////////////////////////////////////
// Constructors
///////////////////////////////////////////////////////
/**
* Creates new instance of Test class
*/
public Test() {
}
/**
* Creates new instance of Test class
*
* @param id specific test identifier
*/
public Test(Integer id) {
this.id = id;
}
///////////////////////////////////////////////////////
// Property accessors
///////////////////////////////////////////////////////
@Id
@Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, precision = 22, scale = 0)
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name = "KLASSE", unique = false, nullable = true, insertable = true, updatable = true, precision = 22, scale = 0)
public Integer getKlasse() {
return this.klasse;
}

public void setKlasse(Integer klasse) {
this.klasse = klasse;
}

@Column(name = "PARENT_ID", unique = false, nullable = true, insertable = true, updatable = true, precision = 22, scale = 0)
public Integer getParentId() {
return this.parentId;
}

public void setParentId(Integer parentId) {
this.parentId = parentId;
}
}

Code between sessionFactory.openSession() and session.close():

I've created method:

List list = session.createSQLQuery("select t.ID as {currentTest.id} from Test t where connect_by_isleaf=1 start with t.ID=2 connect by prior t.ID=t.PARENT_ID order by t.KLASSE").
addEntity("currentTest", Test.class).list()

It issues exception below.

If I replace

select t.ID as {currentTest.id}

with

*

all is working without problems.

So I just need to get not all data to given entity but only some attributes.

Full stack trace of any exception that occurs:

org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not execute query; bad SQL grammar [select t.ID as ID0_0_ from Test t where connect_by_isleaf=1 start with t.ID=2 connect by prior t.ID=t.PARENT_ID order by t.KLASSE]; nested exception is java.sql.SQLException: Не.о.уст.мое .м. сто...а
java.sql.SQLException: Не.о.уст.мое .м. сто...а
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:158)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:305)
at oracle.jdbc.driver.OracleStatement.get_column_index(OracleStatement.java:3718)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2583)
at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:1605)
at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:236)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:102)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2031)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1371)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1299)
at org.hibernate.loader.Loader.getRow(Loader.java:1197)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:568)
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:2144)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
at org.hibernate.loader.Loader.list(Loader.java:2023)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150)
at test.TestDAOImpl$2.doInHibernate(TestDAOImpl.java:173)

Name and version of the database you are using:

Oracle Database 10g

The generated SQL (show_sql=true):

select t.ID as ID0_0_ from Test t where connect_by_isleaf=1 start with t.ID=2 connect by prior t.ID=t.PARENT_ID order by t.KLASSE


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 01, 2006 4:51 am 
Newbie

Joined: Thu Nov 30, 2006 11:44 am
Posts: 3
Location: Ukraine
Hello,

So are there any ways not to fill all fields of annotated class using createSQLQuery() method with "*" in SELECT query?

-----------------------------------------------------------------------------------
Example:

String sql1 = "select"
+ " u.FIRSTNAME as {uzer.firstname},"
+ " u.LASTNAME as {uzer.lastname} from USERS u";

String sql2 = "select * from USERS u";

List results =
session.createSQLQuery(sql1, "uzer", User.class).list();

List results =
session.createSQLQuery(sql2, "uzer", User.class).list();

Assuming that User class has three fields id, firstname,lastname.

In my own example first SQL query with * is working good and only mapping not all table attributes to the same entity doesn't work
----------------------------------------------------------------------------------

Because selecting all data is time consuming in such cases. But creating one annotated class for each view is not very good design (I mean one annotated class for selecting all attributes from table, another one for selecting only half of them and so on).

Thanks in advance!

Best regards,
Alex


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.