I am using two formula field in my POJO class.
When I am using native SQL query to get information Null Pointer Exception is throwing.
Same SQL query is working fine if I executing that SQL in SQL Prompt.
HQL query is working fine. Issue is with SQL query only.
Please help me, how can I used SQL query with formula.
Sample code is given below which I am using to fetch data from database.
I am using Oracle 10g and Hibernate 3.2.6 ga.
Stack Trace
--------------------Exception in thread "main" java.lang.NullPointerException
at org.hibernate.loader.DefaultEntityAliases.intern(DefaultEntityAliases.java:133)
at org.hibernate.loader.DefaultEntityAliases.getSuffixedPropertyAliases(DefaultEntityAliases.java:106)
at org.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:52)
at org.hibernate.loader.ColumnEntityAliases.<init>(ColumnEntityAliases.java:16)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.generateCustomReturns(SQLQueryReturnProcessor.java:174)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:129)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:43)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:114)
at org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:137)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at com.capgent.cpt.service.database.utils.HibernateDatabaseCalls.findSpecificRecords(HibernateDatabaseCalls.java:214)
at com.capgent.cpt.service.database.utils.HibernateDatabaseCalls.main(HibernateDatabaseCalls.java:685)
2028496404Creating new session factory object org.hibernate.impl.SessionFactoryImpl@78e86614
AddHdr.java (POJO class)
-------------------------------Code:
public class AddHdr implements Serializable
{
/** identifier field */
private Integer adId;
/** nullable persistent field */
private String adFname;
/** nullable persistent field */
private String adLname;
private String adName;
public AddHdr()
{
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "address_sequence")
@SequenceGenerator(name = "address_sequence", sequenceName = "ADDRESS_SEQ")
@Column(name = "AD_ID", unique = true, nullable = false)
public Integer getAdId()
{
return this.adId;
}
public void setAdId(Integer adId)
{
this.adId = adId;
}
public String getAdFname()
{
return this.adFname;
}
public void setAdFname(String adFname)
{
this.adFname = adFname;
}
public String getAdLname()
{
return this.adLname;
}
public void setAdLname(String adLname)
{
this.adLname = adLname;
}
public String getAdName()
{
return adName;
}
public void setAdName(String adName)
{
this.adName = adName;
}
}
Mapping File
-----------------------AddHdr.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.2
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="com.capgent.cpt.common.to.AddHdr"
table="ADD_HDR"
lazy="true"
>
<cache usage="read-write"/>
<id
name="adId"
type="java.lang.Integer"
column="AD_ID"
>
<generator class="sequence">
<param name="sequence">ADDRESS_SEQ</param>
</generator>
</id>
<property
name="adFname"
type="java.lang.String"
column="AD_FNAME"
length="50"
/>
<property
name="adLname"
type="java.lang.String"
column="AD_LNAME"
length="50"
/>
<property
name="adName"
type="java.lang.String"
formula="AD_LNAME|| ' ' ||AD_FNAME"
length="100"
insert="false"
update="false"
/>
</class>
</hibernate-mapping>
Used following code to fetch data from database
String queryTable = "select * from add_hdr";
String conditions = " where ad_id =843";
List list = obj.findSpecificRecords(queryTable, conditions, AddHdr.class);
Method
---------
Code:
public List findSpecificRecords(String queryTable, String conditions, Class classObj)
throws ServiceLocatorException, HibernateDatabaseException
{
Session dbSession = getDataSource();
long time = System.currentTimeMillis();
Query query = dbSession.createSQLQuery(queryTable + conditions).addEntity(conditions, classObj).setCacheable(true);
List objectTos = query.list();
dbSession.close();
return objectTos;
}