Hello,
I have a problem concerning HQL querys.
I can't get Integer or Short Object using the syntax "select new ......"
the code is this one:
*********************************************************
public List getListeParametreFCP() throws RunTimeApplicationException {
Session session = newSession("fcp");
List listeTestHibernate = null;
Logger log = Logger.getLogger(this.getClass());
try {
StringBuffer requete = new StringBuffer();
requete.append("select new fr.bdpme.fcp.business.service.util.TestHibernate( ");
requete.append("temescli.nmecli, ");
requete.append("temescli.dfvame");
requete.append(", temescli.ddvame, temescli.lmesdi, ");
requete.append("temescli.sdemaj, temescli.udemaj ) ");
requete.append(" from fr.business.bean.Temescli as temescli ");
listeTestHibernate = session.find(requete.toString());
} catch (HibernateException ex) {
ex.printStackTrace();
throw new RunTimeApplicationException("Impossible de lancer la recherche des TestHibernate");
} finally {
try {
session.close();
} catch (HibernateException ex) {
throw new RunTimeApplicationException("Impossible de fermer la session hibernate");
}
return listeTestHibernate;
}
}
************************************************************
the pseudo Bean object that have the same propertys as my mapping object.
package fr.business.service.util;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class TestHibernate {
/** identifier field */
private Integer nmecli;
/** nullable persistent field */
private java.util.Date dfvame;
/** nullable persistent field */
private java.util.Date ddvame;
/** nullable persistent field */
private String lmesdi;
/** nullable persistent field */
private String sdemaj;
/** nullable persistent field */
private String udemaj;
/** full constructor */
public TestHibernate(java.lang.Integer nmecli, java.util.Date dfvame, java.util.Date ddvame, java.lang.String lmesdi, java.lang.String sdemaj, java.lang.String udemaj) {
this.nmecli = nmecli;
this.dfvame = dfvame;
this.ddvame = ddvame;
this.lmesdi = lmesdi;
this.sdemaj = sdemaj;
this.udemaj = udemaj;
}
/** full constructor*/
public TestHibernate(int nmecli, java.util.Date dfvame, java.util.Date ddvame, java.lang.String lmesdi, java.lang.String sdemaj, java.lang.String udemaj) {
this.nmecli = new Integer(nmecli);
this.dfvame = dfvame;
this.ddvame = ddvame;
this.lmesdi = lmesdi;
this.sdemaj = sdemaj;
this.udemaj = udemaj;
}
/** default constructor */
public TestHibernate() {
}
/** minimal constructor */
public TestHibernate(java.lang.Integer nmecli) {
this.nmecli = nmecli;
}
public java.lang.Integer getNmecli() {
return this.nmecli;
}
public void setNmecli(java.lang.Integer nmecli) {
this.nmecli = nmecli;
}
public java.util.Date getDfvame() {
return this.dfvame;
}
public void setDfvame(java.util.Date dfvame) {
this.dfvame = dfvame;
}
public java.util.Date getDdvame() {
return this.ddvame;
}
public void setDdvame(java.util.Date ddvame) {
this.ddvame = ddvame;
}
public java.lang.String getLmesdi() {
return this.lmesdi;
}
public void setLmesdi(java.lang.String lmesdi) {
this.lmesdi = lmesdi;
}
public String toString() {
return new ToStringBuilder(this)
.append("nmecli", getNmecli())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof TestHibernate) ) return false;
TestHibernate castOther = (TestHibernate) other;
return new EqualsBuilder()
.append(this.getNmecli(), castOther.getNmecli())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getNmecli())
.toHashCode();
}
/**
* @return
*/
public String getSdemaj() {
return sdemaj;
}
/**
* @return
*/
public String getUdemaj() {
return udemaj;
}
/**
* @param string
*/
public void setSdemaj(String string) {
sdemaj = string;
}
/**
* @param string
*/
public void setUdemaj(String string) {
udemaj = string;
}
}
**************************************************************
My mapping object
package fr.business.bean;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Temescli implements Serializable {
/** identifier field */
private Integer nmecli;
/** nullable persistent field */
private java.util.Date dfvame;
/** nullable persistent field */
private java.util.Date ddvame;
/** nullable persistent field */
private String lmesdi;
/** nullable persistent field */
private String sdemaj;
/** nullable persistent field */
private String udemaj;
/** full constructor */
public Temescli(java.lang.Integer nmecli, java.util.Date dfvame, java.util.Date ddvame, java.lang.String lmesdi, java.lang.String sdemaj, java.lang.String udemaj) {
this.nmecli = nmecli;
this.dfvame = dfvame;
this.ddvame = ddvame;
this.lmesdi = lmesdi;
this.sdemaj = sdemaj;
this.udemaj = udemaj;
}
/** default constructor */
public Temescli() {
}
/** minimal constructor */
public Temescli(java.lang.Integer nmecli) {
this.nmecli = nmecli;
}
public java.lang.Integer getNmecli() {
return this.nmecli;
}
public void setNmecli(java.lang.Integer nmecli) {
this.nmecli = nmecli;
}
public java.util.Date getDfvame() {
return this.dfvame;
}
public void setDfvame(java.util.Date dfvame) {
this.dfvame = dfvame;
}
public java.util.Date getDdvame() {
return this.ddvame;
}
public void setDdvame(java.util.Date ddvame) {
this.ddvame = ddvame;
}
public java.lang.String getLmesdi() {
return this.lmesdi;
}
public void setLmesdi(java.lang.String lmesdi) {
this.lmesdi = lmesdi;
}
public String toString() {
return new ToStringBuilder(this)
.append("nmecli", getNmecli())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof Temescli) ) return false;
Temescli castOther = (Temescli) other;
return new EqualsBuilder()
.append(this.getNmecli(), castOther.getNmecli())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getNmecli())
.toHashCode();
}
/**
* @return
*/
public String getSdemaj() {
return sdemaj;
}
/**
* @return
*/
public String getUdemaj() {
return udemaj;
}
/**
* @param string
*/
public void setSdemaj(String string) {
sdemaj = string;
}
/**
* @param string
*/
public void setUdemaj(String string) {
udemaj = string;
}
}
*************************************************************
The mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="fr.business.bean.Temescli"
table="TEMESCLI"
>
<id
name="nmecli"
type="java.lang.Integer"
column="NMECLI"
>
<generator class="assigned" />
</id>
<property
name="dfvame"
type="java.sql.Date"
column="DFVAME"
length="10"
/>
<property
name="ddvame"
type="java.sql.Date"
column="DDVAME"
length="10"
/>
<property
name="lmesdi"
type="java.lang.String"
column="LMESDI"
length="240"
/>
<property
name="sdemaj"
type="java.lang.String"
column="SDEMAJ"
length="26"
/>
<property
name="udemaj"
type="java.lang.String"
column="UDEMAJ"
length="8"
/>
<!-- associations -->
</class>
</hibernate-mapping>
************************************************************
If I use the constructor with an int type there's no problem ....
If I use the constructor with Integer I have One ....
The problem is that there 's a lot of nullable values in my Database ...
not only in an ID but in propertys too .....
the stack is here:
java.lang.NoSuchMethodException: <init>
at java.lang.Class.getConstructor0(Native Method)
at java.lang.Class.getConstructor(Class.java(Compiled Code))
at net.sf.hibernate.hql.QueryTranslator.renderSQL(QueryTranslator.java:534)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:152)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:133)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:352)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:330)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1368)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1332)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1314)
at fr.business.service.util.ServiceTestHibernate.getListeParametreFCP(ServiceTestHibernate.java:59)
at fr.business.service.util.ServiceTestHibernateTest.testGetListeParametreFCP(ServiceTestHibernateTest.java:44)
at java.lang.reflect.Method.invoke(Native Method)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)
rethrown as net.sf.hibernate.QueryException: could not find constructor for: fr.business.service.util.TestHibernate: <init> [select new fr.business.service.util.TestHibernate( temescli.nmecli, temescli.dfvame, temescli.ddvame, temescli.lmesdi, temescli.sdemaj, temescli.udemaj ) from fr.bdpme.fcp.business.bean.Temescli as temescli ]
at net.sf.hibernate.hql.QueryTranslator.renderSQL(QueryTranslator.java:537)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:152)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:133)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:352)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:330)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1368)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1332)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1314)
at fr.business.service.util.ServiceTestHibernate.getListeParametreFCP(ServiceTestHibernate.java:59)
at fr.business.service.util.ServiceTestHibernateTest.testGetListeParametreFCP(ServiceTestHibernateTest.java:44)
at java.lang.reflect.Method.invoke(Native Method)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)
************************************************************
who can help me?
Thanks a lot by advance,
Best regards,
Mehdi