8

I'm getting this error with my hibernate model, and I can't figure out what's wrong.

Tag.java:

@Entity
@Table(name = "tag")
public class Tag implements java.io.Serializable {
    private Integer idTag;
    private String name;
    private Set<Question> questions = new HashSet<Question>(0);

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "idtag", unique = true, nullable = false)
    public Integer getIdTag() {
        return this.idTag;
    }

    public void setIdTag(Integer idtag) {
        this.idTag = idtag;
    }

    [...]

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tag")
    public Set<Question> getQuestions() {
        return this.questions;
    }

    public void setQuestions(Set<Question> questions) {
        this.questions = questions;
    }

}

Question.java:

@Entity
@Table(name = "question")
@Inheritance(strategy=InheritanceType.JOINED)
public class Question implements java.io.Serializable {
    protected Integer idQuestion;
    protected Tag tag;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "idquestion", unique = true, nullable = false)
    public Integer getIdQuestion() {
        return this.idQuestion;
    }

    public void setIdQuestion(Integer idquestion) {
        this.idQuestion = idquestion;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "idtag")
    public Tag getTag() {
        return this.tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    [...]
}

QuestionText.java:

@Entity
@Table(name = "question_text")
@PrimaryKeyJoinColumn(name="idquestion")
public class QuestionText extends Question implements java.io.Serializable {    
    [...]
}

And here is when this error appears (on query.list()):

q = "FROM QuestionText WHERE tag = :tag";
query = (Query) session.createQuery(q);
query.setParameter("tag", tag);
List<Question> data = query.list();

Stacktrace:

org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of model.Tag.idtag

    org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:187)
    org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:344)
    org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4537)
    org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4259)
    org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:209)
    org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:248)
    org.hibernate.type.EntityType.getIdentifier(EntityType.java:510)
    org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)
    org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:66)
    org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:612)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1875)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
    org.hibernate.loader.Loader.doQuery(Loader.java:900)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    org.hibernate.loader.Loader.doList(Loader.java:2526)
    org.hibernate.loader.Loader.doList(Loader.java:2512)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
    org.hibernate.loader.Loader.list(Loader.java:2337)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    my.project.service.QuestionService.findCatItems(QuestionService.java:34)

I thought it might be an issue related to my JOINED inheritance, but I get the same error with TABLE_PER_CLASS. Do you see anything which I did wrong in this ?

Arthur
  • 3,717
  • 7
  • 28
  • 44

3 Answers3

9

Try this way

Tag  tag = tagDAO.findTagbyId(1);
q = "FROM QuestionText qt WHERE qt.tag = :tag";
query = (Query) session.createQuery(q);
query.setParameter("tag", tag);
List<QuestionText> data = query.list();
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • 1
    Oh! I need to give an object parameter, and not only the ID. Thanks a lot, it works. – Arthur Oct 21 '13 at 12:14
  • You can instead use [`setParameter("tag.id", tagId)`](http://stackoverflow.com/a/24777487/320036). This avoids fetching the tag object from the database just to use the ID. – z0r May 27 '16 at 07:27
0

Make sure you table have the same type as the Idtag. What the error report is :type mismatch with Integer.

Bensson
  • 4,509
  • 4
  • 19
  • 24
  • Thanks for you answer, but `idtag` is an `int(11)` in both tables. And variable `tag` use in the query is also an `Integer`. – Arthur Oct 17 '13 at 09:17
  • Can i have you DB script? Actually I can run successful with code in my local. Maybe still have issue with DB script. I suggest drop whole table let JPA help you to create table(e.g.: update ) and then test it again. If can run successful. then compare you DB script and the script which JPA created. – Bensson Oct 17 '13 at 12:47
  • My DB script has already been generated from Entities by JPA Tools in Eclipse. You can get it [here](http://0bin.hoa.ro/paste/3aee85e7ac5025d019daebd3c67abaec02d47173#mEighrrVZFlUbe9l2O7Dgsbnn1GJ6FhLKgHR+mK+TZ0=). – Arthur Oct 17 '13 at 12:51
0

Are these methods named according to the bean naming conventions? I would say no.

Try setIdTag() and getIdTag().

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "idtag", unique = true, nullable = false)
public Integer getIdtag() {
    return this.idtag;
}

public void setIdtag(Integer idtag) {
    this.idtag = idtag;
}
Ali Ben Messaoud
  • 11,690
  • 8
  • 54
  • 87
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • I agree, that's better. But it doesn't solve my issue. I will edit my question with the right naming conventions. – Arthur Oct 17 '13 at 12:16