Hi,
I want to create a query which selects a country object with a specified id.
I tried this in that way:
Code:
public Country getCountryByID(Long id) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Country country = (Country) session.createQuery("from Country where id = ?")
.setEntity(0, id);
session.getTransaction().commit();
return country;
}
Hibernate does not like my Long parameter. I don't know how I should pass the id instead of that, perhaps someone can help me.
The country class has the following structure:
Code:
public class Country {
private Long id;
private String countryName;
// getter and setter methods
}
Unfortunately it seem like I made a mistake in the way of using the setEntity() method because I get the following exception:
Code:
Static select for action ACTION_MERGE on entity com.mycompany.gmt.dao.transferobjects.Vendor: select vendor0_.id_vendor as id1_2_0_, vendor0_.name as name2_0_, vendor0_.postcode as postcode2_0_, vendor0_.street as street2_0_, vendor0_.housenumber as housenum5_2_0_, vendor0_.city as city2_0_, vendor0_.country as country2_0_, vendor0_.phonenumber as phonenum8_2_0_, vendor0_.faxnumber as faxnumber2_0_ from tbl_vendor vendor0_ where vendor0_.id_vendor=java.lang.ClassCastException
at com.mycompany.gmt.dao.CountryDAOImpl.getCountryByID(CountryDAOImpl.java:29)
at tests.DAOTest.getCountryByID(DAOTest.java:35)
at tests.DAOTest.main(DAOTest.java:78)
?
09:21:34,004 DEBUG EntityLoader:34 - Static select for action ACTION_REFRESH on entity com.mycompany.gmt.dao.transferobjects.Vendor: select vendor0_.id_vendor as id1_2_0_, vendor0_.name as name2_0_, vendor0_.postcode as postcode2_0_, vendor0_.street as street2_0_, vendor0_.housenumber as housenum5_2_0_, vendor0_.city as city2_0_, vendor0_.country as country2_0_, vendor0_.phonenumber as phonenum8_2_0_, vendor0_.faxnumber as faxnumber2_0_ from tbl_vendor vendor0_ where vendor0_.id_vendor=?
09:21:34,004 DEBUG SessionFactoryObjectFactory:39 - initializing class SessionFactoryObjectFactory
09:21:34,014 DEBUG SessionFactoryObjectFactory:76 - registered: 2c97c9fb1231ea87011231ea8c340000 (unnamed)
09:21:34,014 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
09:21:34,014 DEBUG SessionFactoryImpl:308 - instantiated session factory
09:21:34,024 DEBUG SessionFactoryImpl:392 - Checking 0 named HQL queries
09:21:34,024 DEBUG SessionFactoryImpl:412 - Checking 0 named SQL queries
09:21:34,184 DEBUG SessionImpl:220 - opened session at timestamp: 11776584940
09:21:34,264 DEBUG ThreadLocalSessionContext:290 - allowing method [beginTransaction] in non-transacted context
09:21:34,264 DEBUG ThreadLocalSessionContext:300 - allowing proxied method [beginTransaction] to proceed to real session
09:21:34,264 DEBUG JDBCTransaction:54 - begin
09:21:34,264 DEBUG ConnectionManager:419 - opening JDBC connection
09:21:34,264 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 0
09:21:34,264 DEBUG DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
09:21:34,264 DEBUG JDBCTransaction:59 - current autocommit status: false
09:21:34,264 DEBUG JDBCContext:210 - after transaction begin
09:21:34,344 DEBUG ThreadLocalSessionContext:300 - allowing proxied method [createQuery] to proceed to real session
09:21:34,344 DEBUG QueryPlanCache:70 - unable to locate HQL query plan in cache; generating (from Country where id = ?)
09:21:34,424 DEBUG QueryTranslatorImpl:246 - parse() - HQL: from com.mycompany.gmt.dao.transferobjects.Country where id = ?
09:21:34,444 DEBUG AST:266 - --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| \-[FROM] 'from'
| \-[RANGE] 'RANGE'
| \-[DOT] '.'
| +-[DOT] '.'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[IDENT] 'com'
| | | | | \-[IDENT] 'mycompany'
| | | | \-[IDENT] 'gmt'
| | | \-[IDENT] 'dao'
| | \-[IDENT] 'transferobjects'
| \-[IDENT] 'Country'
\-[WHERE] 'where'
\-[EQ] '='
+-[IDENT] 'id'
\-[PARAM] '?'
09:21:34,455 DEBUG ErrorCounter:68 - throwQueryException() : no errors
09:21:34,505 DEBUG HqlSqlBaseWalker:111 - select << begin [level=1, statement=select]
09:21:34,535 DEBUG FromElement:108 - FromClause{level=1} : com.mycompany.gmt.dao.transferobjects.Country (no alias) -> country0_
09:21:34,535 DEBUG HqlSqlWalker:475 - attempting to resolve property [id] as a non-qualified ref
09:21:34,535 DEBUG FromReferenceNode:51 - Resolved : {synthetic-alias} -> {synthetic-alias}
09:21:34,545 DEBUG FromElement:525 - handling property dereference [com.mycompany.gmt.dao.transferobjects.Country (null) -> id (class)]
09:21:34,545 DEBUG DotNode:555 - getDataType() : id -> org.hibernate.type.LongType@8c4a77
09:21:34,545 DEBUG FromReferenceNode:51 - Resolved : {synthetic-alias}.id -> country0_.id_country
09:21:34,555 DEBUG HqlSqlBaseWalker:117 - select : finishing up [level=1, statement=select]
09:21:34,555 DEBUG HqlSqlWalker:509 - processQuery() : ( SELECT ( FromClause{level=1} tbl_country country0_ ) ( where ( = ( country0_.id_country {synthetic-alias} id ) ? ) ) )
09:21:34,565 DEBUG HqlSqlWalker:716 - Derived SELECT clause created.
09:21:34,575 DEBUG JoinProcessor:148 - Using FROM fragment [tbl_country country0_]
09:21:34,575 DEBUG HqlSqlBaseWalker:123 - select >> end [level=1, statement=select]
09:21:34,575 DEBUG AST:232 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (tbl_country)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'country0_.id_country as id1_1_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=tbl_country,tableAlias=country0_,origin=null,colums={,className=com.mycompany.gmt.dao.transferobjects.Country}}}
| \-[SQL_TOKEN] SqlFragment: 'country0_.country_name as country2_1_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[country0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'tbl_country country0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=tbl_country,tableAlias=country0_,origin=null,colums={,className=com.mycompany.gmt.dao.transferobjects.Country}}
\-[WHERE] SqlNode: 'where'
\-[EQ] BinaryLogicOperatorNode: '='
+-[DOT] DotNode: 'country0_.id_country' {propertyName=id,dereferenceType=4,propertyPath=id,path={synthetic-alias}.id,tableAlias=country0_,className=com.mycompany.gmt.dao.transferobjects.Country,classAlias=null}
| +-[IDENT] IdentNode: '{synthetic-alias}' {originalText={synthetic-alias}}
| \-[IDENT] IdentNode: 'id' {originalText=id}
\-[PARAM] ParameterNode: '?' {ordinal=0, expectedType=org.hibernate.type.LongType@8c4a77}
09:21:34,575 DEBUG ErrorCounter:68 - throwQueryException() : no errors
09:21:34,595 DEBUG QueryTranslatorImpl:216 - HQL: from com.mycompany.gmt.dao.transferobjects.Country where id = ?
09:21:34,595 DEBUG QueryTranslatorImpl:217 - SQL: select country0_.id_country as id1_1_, country0_.country_name as country2_1_ from tbl_country country0_ where country0_.id_country=?
09:21:34,595 DEBUG ErrorCounter:68 - throwQueryException() : no errors
09:21:34,605 DEBUG HQLQueryPlan:269 - HQL param location recognition took 0 mills (from Country where id = ?)
Exception in thread "main"
Regards,
TMK