[英]Hibernate @Filter entity by @ManyToOne field
I'm trying to use Java's Hibernate @Filter on a many to one relationship field, but when I enable the filter and try to call a getAll method using the StockDailyRecord Service from an AJAX I get this: 我试图在多对一关系字段上使用Java的Hibernate @Filter,但是当我启用过滤器并尝试使用AJAX的StockDailyRecord Service调用getAll方法时,我得到了:
HTTP Status 500 - Request processing failed;
nested exception is javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
This is my entity where I define the filter: 这是我定义过滤器的实体:
@Entity
@Table(name = "stock_daily_record", catalog = "mkyong")
@FilterDef(name="stockDailyRecordFilter")
@Filters({
@Filter(name="stockDailyRecordFilter", condition="stock.name = 'My stock'"),
})
public class StockDailyRecord extends Entity implements Serializable {
...
@ManyToOne
@JoinColumn(name = "stock_id", referencedColumnName = "id", nullable = false )
public Stock getStock() {
return this.stock;
}
}
However when I change the filter with the actual column in the database "stock_id" and compare it with the id I want the filter works without any exceptions. 但是,当我使用数据库“ stock_id”中的实际列更改过滤器并将其与ID进行比较时,我希望过滤器能够正常工作。
@Filter(name="stockDailyRecordFilter", condition="stock_id = 10")
Here is how I enable the filter in the StockDailyRecordService: 这是我在StockDailyRecordService中启用过滤器的方式:
Session hSession = getSession();
if (isAnonymous) {
hSession.enableFilter("stockDailyRecordFilter");
} else {
hSession.disableFilter("stockDailyRecordFilter");
}
Also, when I try the filter on other properties (say the StockDailyRecord has field "description") it works just fine. 另外,当我尝试对其他属性进行过滤(例如StockDailyRecord具有字段“ description”)时,它也可以正常工作。 Is there a way to access the "name" property of the Stock entity from the Filter condition, because I don't really want to do the approach with the "stock_id = 10"?
是否有一种方法可以从“过滤器”条件访问Stock实体的“名称”属性,因为我真的不想使用“ stock_id = 10”来执行此方法?
Try to remove the stock
from filter definition 尝试从过滤器定义中删除
stock
@Filter(name="stockDailyRecordFilter", condition="name = 'My stock'")
UPDATE So stock here is related table not the main table. 更新因此,这里的库存是相关表而不是主表。 You can try to join to subselect with IN
您可以尝试加入IN进行子选择
condition="stock_id in (select id from stock where name = 'My stock')"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.