使用视图作为 Hibernate 的连接表
我有两个实体,我想通过一个公共字符串连接它们。 我创建了一个视图,我想将其用作连接表。除了当我尝试删除实体时之外,这一切都工作正常。然后 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道根本不用连接表就做不到吗?据我了解,您的关系实际上是只读的,因此以下方法应该可以正常工作:
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: