hibernategetterillegalargumentexceptioncreatequery

hibernate curious mistake in consult


im doing my first trying in hibernate and i get this question.

When i do this consult i get correct result

List result = session.createQuery("select t from Tarea t where t.subtipo = 1").list();

but if i use one parameter give me a error

List result = session.createQuery("select t from Tarea t where t.subtipo = :subtipo").
            setParameter("subtipo", 1).list();

the error is:

ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: com.hibernate.gip.modelo.SubTipo, getter method of property: id_subtipo
[ERROR] IllegalArgumentException occurred calling getter of com.hibernate.gip.modelo.SubTipo.id_subtipo
java.lang.reflect.InvocationTargetException

maybe i forgot something like newbe... can anyone solve it?? thanks in advance!

EDITING...

SubTipo.java

public class SubTipo {

// Atributos
private int id_subtipo;
private String nombre;
private Tipo tipo;

private Set<Incidencia> incidencias = new HashSet<Incidencia>();
private Set<Tarea> tareas = new HashSet<Tarea>();

// Constructores
public SubTipo() {}

public SubTipo(int id, String nom, Tipo tip, Set<Tarea> tar, Set<Incidencia> inc) {
    this.id_subtipo = id;
    this.nombre = nom;
    this.tipo = tip;
    this.incidencias = inc;
    this.tareas = tar;
}

// Getters & Setters
public int getId_subtipo() {
    return id_subtipo;
}

public void setId_subtipo(int id_subtipo) {
    this.id_subtipo = id_subtipo;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public Tipo getTipo() {
    return tipo;
}

public void setTipo(Tipo tipo) {
    this.tipo = tipo;
}

public Set<Incidencia> getIncidencias() {
    return incidencias;
}

public void setIncidencias(Set<Incidencia> incidencias) {
    this.incidencias = incidencias;
}

public Set<Tarea> getTareas() {
    return tareas;
}

public void setTareas(Set<Tarea> tareas) {
    this.tareas = tareas;
}

}

SubTipo mapping file

<hibernate-mapping package="com.hibernate.gip.modelo">

<class name="SubTipo" table="SUBTIPO">

    <id name="id_subtipo" type="int">
        <column name="ID_SUBTIPO" />
        <generator class="assigned" />
    </id>

    <property name="nombre" type="java.lang.String">
        <column name="NOMBRE" />
    </property>

    <many-to-one name="tipo" class="com.hibernate.gip.modelo.Tipo">
        <column name="TIPO" not-null="true" />
    </many-to-one>

    <set name="incidencias" table="INCIDENCIA">
        <key>
            <column name="SUBTIPO" />
        </key>
        <one-to-many class="com.hibernate.gip.modelo.Incidencia" />
    </set>

    <set name="tareas" table="TAREA">
        <key>
            <column name="SUBTIPO" />
        </key>
        <one-to-many class="com.hibernate.gip.modelo.Tarea" />
    </set>

</class>

</hibernate-mapping>

its necessary other class? thanks

REEDITING

Tarea.java

public class Tarea {

// Atributos
private String id_tarea;
private String nombre;
private int t_limite;

private Tarea precedente;
private SubTipo subtipo;

private Set<iTarea> iTareas = new HashSet<iTarea>();

// Constructores
public Tarea() {}

public Tarea(String id, String nom, int lim, Tarea pre, SubTipo sub, Set<iTarea> iTa) {
    this.id_tarea = id;
    this.nombre = nom;
    this.t_limite = lim;
    this.precedente = pre;
    this.subtipo = sub;
    this.iTareas = iTa;
}

// Getters & Setters
public String getId_tarea() {
    return id_tarea;
}

public void setId_tarea(String id_tarea) {
    this.id_tarea = id_tarea;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public int getT_limite() {
    return t_limite;
}

public void setT_limite(int t_limite) {
    this.t_limite = t_limite;
}

public Tarea getPrecedente() {
    return precedente;
}

public void setPrecedente(Tarea precedente) {
    this.precedente = precedente;
}

public SubTipo getSubtipo() {
    return subtipo;
}

public void setSubtipo(SubTipo subtipo) {
    this.subtipo = subtipo;
}

public Set<iTarea> getiTareas() {
    return iTareas;
}

public void setiTareas(Set<iTarea> iTareas) {
    this.iTareas = iTareas;
}   

}

Tarea mapping

<hibernate-mapping package="com.hibernate.gip.modelo">

<class name="Tarea" table="TAREA">

    <id name="id_tarea" type="java.lang.String">
        <column name="ID_TAREA" />
        <generator class="assigned" />
    </id>

    <property name="nombre" type="java.lang.String">
        <column name="NOMBRE" />
    </property>

    <property name="t_limite" type="int">
        <column name="T_LIMITE" />
    </property>

    <many-to-one name="precedente" class="com.hibernate.gip.modelo.Tarea" unique="true"> </many-to-one>

    <many-to-one name="subtipo" class="com.hibernate.gip.modelo.SubTipo">
        <column name="SUBTIPO" not-null="true"/>
    </many-to-one>

    <set name="iTareas" table="ITAREA">
        <key>
            <column name="TAREA" />
        </key>
        <one-to-many class="com.hibernate.gip.modelo.iTarea" />
    </set>

</class>

</hibernate-mapping>

Solution

  • When you are working in hql, don't forget you are working on objects, not on row.

    The parameter you pass in your query is the id and not the object. You can try :

    List result = session.createQuery("select t from Tarea t where t.subtipo.id_subtipo = :subtipoId").setParameter("subtipoId", 1).list();
    

    The query you tried might work if you pass to it a persited instance of subtipo and not an id.