java - 使用 Hibernate Annotations 的 View 对象的 SQL 错误

标签 java sql sql-server hibernate hibernate-annotations

我正在研究数据库 View 的 hibernate 实体映射;当我对其进行标准查询时,hibernate 会生成错误的 SQL。任何帮助找出我的映射问题所在的帮助将不胜感激!

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

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

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

@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 (?, ?...)

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

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

更新:

我不再使用连接表符号将 View 映射为字段(如下建议)。注释已修改如下。该解决方案在 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;
    }
}

最佳答案

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

让我问你这个问题 - 为什么首先将 ViewLatestStatusHistoryRow 映射为实体?这不像你会坚持下去。考虑将您的最新历史条目直接(作为只读)映射到主(多对一)上,并将您的 View 用作联接表。

关于java - 使用 Hibernate Annotations 的 View 对象的 SQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1441671/

相关文章:

java - 如何在调用自身的方法中使用 ArrayList

java - 如何在运行时动态改变SeekBar的进度颜色?

java - 如何在 Spring 应用程序中添加 SASL 身份验证

php - 预订日历 到达和离开日期

sql - 元素为null时如何返回null

java - 朱单元 5 : build from source leads to exception

sql - 错误 : ORA-00955: name is already used by an existing object in Oracle Function

java - 使用 JDBC 将变量添加到数据库

php - 无法从 Linux 上的 laravel 连接 azure sql 服务器

sql-server - 用于批量插入的触发触发器