Similar problem here... Difference I use annotations and postgres 8.3. I'm trying to execute this SQL Query:
Code:
Session session = HibernateUtil.getSessionFactory().openSession();
String sql = "SELECT * FROM " +
"(planoprocedimento AS PP INNER JOIN plano AS PL ON PL.plan_id = PP.plan_id) " +
"INNER JOIN procedimento AS P ON P.proc_id = PP.proc_id " +
"WHERE PP.plan_id = :id AND P.proc_descricao SIMILAR TO :name " +
"ORDER BY P.proc_descricao";
List<PlanoProcedimento> list = (ArrayList<PlanoProcedimento>) session.createSQLQuery(sql)
.addEntity("planoProc", PlanoProcedimento.class)
.addJoin("plano", "planoProc.plano")
.addJoin("procedimento", "planoProc.procedimento")
.setLong("id", id)
.setString("name", name + '%')
.list();
The thing is, I tested this SQL and it worked pretty well, but when trying to execute it with hibernate (code above), I got this exception:
Code:
18:34:47,687 ERROR JDBCExceptionReporter:72 - The column name clazz_ wasn't found in this ResultSet.
08/05/2008 18:34:47 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
org.postgresql.util.PSQLException: The column name clazz_ wasn't found in this ResultSet.
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.findColumn(AbstractJdbc2ResultSet.java:2450)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2312)
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:139)
at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1433)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1275)
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:2157)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2041)
at org.hibernate.loader.Loader.list(Loader.java:2036)
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 br.ufms.sipas.dao.impl.PlanoDAOImpl.findProcsByName(PlanoDAOImpl.java:83)
at br.ufms.sipas.action.PlanoAction.selectProcsByLetter(PlanoAction.java:97)
I don't have any column called "clazz_", for sure. It seems that Hibernate is assuming that this column exists because I use a
Joined Inheritance class. Here some code of the classes involved:
Class Procedimento:
Code:
package br.ufms.sipas.bo;
import javax.persistence.*;
@Entity
@Table(name="procedimento")
@Inheritance(strategy=InheritanceType.JOINED)
public class Procedimento implements java.io.Serializable, Comparable<Procedimento>{
private static final long serialVersionUID = 1L;
// Several attributes, all correctly mapped
...
}
Class PlanoProcedimento:
Code:
package br.ufms.sipas.bo;
import javax.persistence.*;
@Entity
@Table(name="planoprocedimento", uniqueConstraints=@UniqueConstraint(columnNames={"plan_id", "proc_id"}))
public class PlanoProcedimento implements java.io.Serializable, Comparable<PlanoProcedimento>{
private static final long serialVersionUID = 1L;
// Several attributes
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="plan_id", nullable=false)
private Plano plano;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="proc_id", nullable=false)
private Procedimento procedimento;
// Rest of the class
...
}
Class Plano:
Code:
package br.ufms.sipas.bo;
import javax.persistence.*;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import java.util.Date;
import java.util.SortedSet;
import java.util.TreeSet;
@Entity
@Table(name="plano")
public class Plano implements java.io.Serializable, Comparable<Plano>{
private static final long serialVersionUID = 1L;
// Several attributes
...
@OneToMany(mappedBy="plano", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@Sort(type=SortType.NATURAL)
private SortedSet<PlanoProcedimento> procedimentos = new TreeSet<PlanoProcedimento>();
// Rest of the class
...
}
Basically, Plano and Procedimento have a n-to-n relationship with extra attributes, and that's why I use the PlanoProcedimento class. Procedimento can be extended, and as I already sayed, this is done using Joined Inheritance. My mapping works well and that's the only problem I had till the moment.
Any ideas? Is this a bug? I'm doing something wrong? Any help will be appreciated! If any futher information is need, please let me know. And sorry for the bad english.
Thanks in advance,
Marcelo.
-------------------------
Brazil