2017-08-02 36 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

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