0

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

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;
    }
}
4

2 回答 2

2

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

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

至:

getSession().createQuery("delete from PositionHistory a where a.id =:id")
于 2012-04-18T06:34:20.200 回答
1

更改hibernate.cfg.xml文件中的 hibernate.query.factory_class 属性,如下所示..希望对您有所帮助..

<property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
于 2012-10-11T09:25:12.057 回答