使用视图作为 Hibernate 的连接表

发布于 2024-10-22 12:53:38 字数 1494 浏览 1 评论 0原文

我有两个实体,我想通过一个公共字符串连接它们。 我创建了一个视图,我想将其用作连接表。除了当我尝试删除实体时之外,这一切都工作正常。然后 Hibernate 尝试从该视图中删除,这当然会失败。使用的数据库是MySQL。

所以我已经

@Entity
public class Event {

   ...
   String productId;
   Date eventDatetime;
   ...
}

@Entity
public class Stock {
   ...
   String productId;
   ...
}

在MySQL中创建了一个视图,

DROP VIEW IF EXISTS EVENT_STOCK_VIEW;
create view EVENT_STOCK_VIEW AS
SELECT EVENT.EVENT_ID, STOCK.STOCK_ID 
FROM EVENT, STOCK 
where STOCK.PRODUCT_ID = EVENT.PRODUCT_ID;

在我添加的事件中:

@ManyToOne(fetch=FetchType.LAZY)
@JoinTable(name="EVENT_STOCK_VIEW",
    joinColumns=@JoinColumn(name="EVENT_ID"),
    inverseJoinColumns=@JoinColumn(name="STOCK_ID",updatable=false,insertable=false))
public Stock getStock(){
    return this.stock;
}

和在库存中:

@OneToMany(fetch=FetchType.LAZY)
    @JoinTable(name="EVENT_STOCK_VIEW",
    joinColumns=@JoinColumn(name="STOCK_ID",updatable=false,insertable=false),       inverseJoinColumns=@JoinColumn(name="EVENT_ID",updatable=false,insertable=false))
    @OrderBy("eventDatetime DESC")
    public List<Event> getEvents(){
        return events;
}

我用谷歌搜索了一下,发现了这个网站。 但解决方案并不是那么好(您必须在库存和事件之间使用实体)。

还有其他解决方案吗?

我可以使用 Hibernate 拦截器并覆盖 onPrepareStatement(String sql) 并检查 SQL 字符串是否包含从 EVENT_STOCK_VIEW 中删除并返回虚拟命令。显然这是我试图避免的黑客攻击。

I've got two entities which I want to join via a common String.
I've created a view which I want to use as the join table. This all works fine except for when I try to delete an entity. Hibernate then tries to delete from that view which of course fails. The database used is MySQL.

So I've got

@Entity
public class Event {

   ...
   String productId;
   Date eventDatetime;
   ...
}

@Entity
public class Stock {
   ...
   String productId;
   ...
}

I've created a view in MySQL

DROP VIEW IF EXISTS EVENT_STOCK_VIEW;
create view EVENT_STOCK_VIEW AS
SELECT EVENT.EVENT_ID, STOCK.STOCK_ID 
FROM EVENT, STOCK 
where STOCK.PRODUCT_ID = EVENT.PRODUCT_ID;

in Event I've added:

@ManyToOne(fetch=FetchType.LAZY)
@JoinTable(name="EVENT_STOCK_VIEW",
    joinColumns=@JoinColumn(name="EVENT_ID"),
    inverseJoinColumns=@JoinColumn(name="STOCK_ID",updatable=false,insertable=false))
public Stock getStock(){
    return this.stock;
}

and in Stock:

@OneToMany(fetch=FetchType.LAZY)
    @JoinTable(name="EVENT_STOCK_VIEW",
    joinColumns=@JoinColumn(name="STOCK_ID",updatable=false,insertable=false),       inverseJoinColumns=@JoinColumn(name="EVENT_ID",updatable=false,insertable=false))
    @OrderBy("eventDatetime DESC")
    public List<Event> getEvents(){
        return events;
}

I've googled a bit and found this site.
But the solution isn't really that nice (you have to use entity in between stock and event).

Are there any other solutions?

I could use a Hibernate Interceptor and override onPrepareStatement(String sql) and check whether the SQL string contains delete from EVENT_STOCK_VIEW and return an dummy command. Clearly a hack which I try to avoid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2024-10-29 12:53:38

难道根本不用连接表就做不到吗?据我了解,您的关系实际上是只读的,因此以下方法应该可以正常工作:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "productId", 
    referencedColumnName = "productId", insertable = false, updateable = false)
public Stock getStock(){
    return this.stock;
}

...

@OneToMany(fetch=FetchType.LAZY, mappedBy = "stock")
@OrderBy("eventDatetime DESC") 
public List<Event> getEvents(){
    return events;
} 

Can't you do it without join table at all? As far as I understand, your relationship is effectively read-only, so that the following approach should work fine:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "productId", 
    referencedColumnName = "productId", insertable = false, updateable = false)
public Stock getStock(){
    return this.stock;
}

...

@OneToMany(fetch=FetchType.LAZY, mappedBy = "stock")
@OrderBy("eventDatetime DESC") 
public List<Event> getEvents(){
    return events;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文