2017-08-02 38 views
0

我哈瓦一個BaseEntity限定了@Id String id。我想在與匹配的@ManyToOne關係上使用休眠過濾器。我還有一層層次結構,我不知道它是否有所作爲,所以我會將其納入以防萬一。休眠過濾條件:訪問嵌套屬性/用途別名

@Entity 
public class Market extends BaseEntity {} 

@MappedSuperclass 
@FilterDef(name = "market", parameters = @ParamDef(name = "marketId", type = "string")) 
@Filter(name = "market", condition = "{alias}.market.id = :marketId") 
public abstract class MarketSpecificEntity extends BaseEntity { 
    @ManyToOne 
    private Market market; 
} 

@Entity 
public class Product extends MarketSpecificEntity {} 

據我所知,{alias}應該替換爲hibernate使用的別名。從ProductRepository.findAll() SQL:

select product0_.id as id1_1_, product0_.market_id as market_i2_1_ from product product0_ where {alias}.market.id = ? 

Ommitting別名工作在非嵌套屬性,但在嵌套id它不會(如預期):

select product0_.id as id1_1_, product0_.market_id as market_i2_1_ from product product0_ where market.id = ? 

我使用aliases參數也嘗試@Filter建議in this answer但我不明白如何適應。

還要說明一點:比較不能導致JOIN。這是因爲market.id是保存在Product表中的外鍵,對嗎?

回答

0

好吧,好像語法condition沒有JPQL但實際的SQL(爲什麼?)。這確實有效:condition = "market_id = :marketId"

而且,他們是用注射作爲$FILTER_PLACEHOLDER$看到here別名的方式。

0

儘量保護,而不是私人

public abstract class MarketSpecificEntity extends BaseEntity { 
    @ManyToOne 
    protected Market market; 
} 
+0

它仍然不能取代'{別名}'。 –