Hibernate org.hibernate.hql.ast.QuerySyntaxException错误

时间:2012-04-18 06:23:27

标签: java hibernate

我已经看到了一些关于此的问题,但我无法找到解决问题的方法。

我声明了以下PositionHistory实体......

@Entity
@Table(name = "position_history")
@Cache (usage=CacheConcurrencyStrategy.READ_WRITE)
@AttributeOverride (name="id", column = @Column(name=PositionHistory.PRIMARY_KEY))
public class PositionHistory extends AbstractPersistable {

    public static final String PRIMARY_KEY = "s_posn_history_id";
    public static final String KEY = "positionHistory";
    public static final String KEY_DATE = "date";
    public static final String KEY_COMMENT = "comment";

    // String field length definitions
    public static final int LENGTH_COMMENTS = 1000;

    // Instance Variables
    private Date date;
    private String comment;

    @Column(name = "d_date", nullable = false)
    public Date getDate() {
    return date;
    }

    public void setDate(Date date) {
    this.date = date;
    }

    @Column(name = "t_comment", length = LENGTH_COMMENTS)
    public String getComment() {
    return comment;
    }

    public void setComment(String comment) {
    this.comment = comment;
    }

    // hashCode()
    // equals()
    // toString()
}

PositionHistoryDaoImp我试图删除一些行......

public void deletePositionHistory(Long positionId) {
    getSession().createQuery("delete from position_history a where a.position_id=:id")
        .setParameter("id", positionId).executeUpdate();
}

当我致电deletePositionHistory时,我收到以下错误...

ERROR a.g.q.d.v.s.c.CommandProcessorImpl:75 - org.hibernate.hql.ast.QuerySyntaxException: position_history is not mapped 

如您所见,该表已映射到PositionHisotry实体中,而PositionHistoryDaoImp扩展了该实体。

我错过了什么或做错了什么?

非常感谢您的帮助。

谢谢。


Position实体......

@Entity
@Table(name = "position")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@AttributeOverride(name = "id", column = @Column(name = Position.PRIMARY_KEY))
public class Position extends AbstractPersistableEffective {
    ...
    public static final String PRIMARY_KEY = "s_posn_id";
    public static final String KEY_ID = "positionId";
    private List<PositionHistory> history = new ArrayList<PositionHistory>();

    ...

    @OneToMany(fetch = FetchType.LAZY)
    @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
    @JoinColumn(name = Position.PRIMARY_KEY)
    @IndexColumn(name = AbstractPersistable.INDEX_COLUMN)
    public List<PositionHistory> getHistory() {
        return history;
    }

    public void setHistory(List<PositionHistory> history) {
        this.history = history;
    }
}

2 个答案:

答案 0 :(得分:2)

您在查询中使用表名,而不是实体名称。改变这个:

getSession().createQuery("delete from position_history a where a.position_id=:id")

要:

getSession().createQuery("delete from PositionHistory a where a.id =:id")

答案 1 :(得分:1)

更改hibernate.cfg.xml文件中的hibernate.query.factory_class属性,如下所示....我希望它可以帮到你..

<property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>