1

我正在研究数据库视图的休眠实体映射;当我对它进行条件查询时,hibernate 正在生成错误的 SQL。任何帮助找出我的映射问题的帮助将不胜感激!

我有两个试图从数据库视图中获取的映射实体;该视图没有其他列,只有每个实体的 FK。这些 FK 之一可以被视为主键,因为视图对于每个主实体都有一行。所以我的视图数据库架构如下所示:

primary(primary_id, some_other_fields)
history(history_id, primary_id, some_other_fields)
view_latest_status_history(primary_id, history_id)

请注意,使用该视图是因为我只想选择每个主节点的最新历史记录,而不是所有映射的历史记录。这是我用于视图的对象,带有实体注释:

@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "view_latest_status_history")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ViewLatestStatusHistoryRow implements Serializable {
    private Primary primary;
    private History history;

    /**
     * @return Returns the history.
     */
    @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY)
    @JoinColumn(name = "history_id", nullable = true)
    @AccessType("field")
    public History getHistory() {
        return history;
    }

    //equals() and hashCode() implementations are omitted

    /**
     * @return Returns the primary.
     */
    @Id
    @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY)
    @JoinColumn(name = "primary_id", nullable = false)
    @AccessType("field")
    public Primary getPrimary() {
        return primary;
    }
}

Primary 和 History 对象都具有完整的工作实体注释。

我的标准设置:

criteria.add(Restrictions.in("primary", [collection of primary objects]));
criteria.setFetchMode("primary", FetchMode.JOIN);
criteria.setFetchMode("history", FetchMode.JOIN);

以及(错误的)生成的 SQL:

select this_.primary as primary78_1_, this_.primary_id as prim2_78_1_, primary2_.history_id as unique1_56_0_, ...history fields
from DB_CATALOG.dbo.view_latest_status_history this_ 
left outer join DB_CATALOG.dbo.history primary2_ on this_.primary_id=primary2_.primary_id 
where this_.specChange in (?, ?...)

在编辑项目数据库模式的细节时,我可能搞砸了一些事情,但关键是“选择”子句中的第一个字段是错误的:

this_.primary (view_latest_status_history.primary) 不是一个字段;该字段应称为primary_id。我认为这可能与主字段上的@Id 注释有关?任何想法如何解决这一问题?如果我删除 @Id,我会收到一条错误消息,告诉我该实体没有主键。

更新:

我不再使用连接表表示法将视图映射为字段(如下所示)。注释已修改如下。此解决方案在 HQL 中正常工作,并在启用 hbm2ddl 时生成预期的架构,但我没有使用条件查询重新测试它。

@Entity
@Table(name = "view_latest_status_history")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ViewLatestStatusHistoryRow implements Serializable {
    private String id;
    private Primary primary;
    private History history;

    /**
     * @return Returns the history.
     */
    @OneToOne(optional = true)
    @JoinColumn(name = "history_id", nullable = true)
    @AccessType("field")
    public History getHistory() {
        return history;
    }

    //equals() and hashCode() implementations are omitted

    @Id
    @Column(name = "primary_id", nullable = false)
    @Override
    @AccessType(value = "field")
    public String getId() {
        return id;
    }

    /**
     * @return Returns the primary.
     */
    @PrimaryKeyJoinColumn(name = "primary_id", referencedColumnName = "unique_id")
    @OneToOne(optional = false)
    @AccessType("field")
    public Primary getPrimary() {
        return primary;
    }
}
4

1 回答 1

1

这肯定是由于@Id注释 - 在这种情况下,primary_id 不是主键。您实际上也不能在同一属性上拥有@Id 和@ManyToOne。

让我问你这个——你为什么ViewLatestStatusHistoryRow要从一个实体开始映射?这不像你会坚持下去。考虑将您的最新历史条目直接(只读)映射到主要(多对一)上,并将您的视图用作连接表。

于 2009-09-17T22:51:38.337 回答