8

我的休眠模型出现了这个错误,我不知道出了什么问题。

标签.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;
    }

}

问题.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 {    
    [...]
}

这是出现此错误的时间(on query.list()):

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

堆栈跟踪:

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)

我认为这可能是与我的JOINED继承有关的问题,但我得到了同样的错误TABLE_PER_CLASS。你看到我在这方面做错了什么吗?

4

3 回答 3

9

试试这个方法

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();
于 2013-10-21T11:07:17.750 回答
0

确保您的表格与 Idtag 具有相同的类型。错误报告是什么:类型与整数不匹配。

于 2013-10-17T08:37:06.220 回答
0

这些方法是否根据 bean 命名约定命名?我会说不。

尝试 setIdTag() 和 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;
}
于 2013-10-17T11:29:45.243 回答