Hibernate在加入“多对多”关联时创建了错误的查询

时间:2013-10-25 07:28:14

标签: java oracle hibernate many-to-many hibernate-criteria

这是我的实体类:

AccService.java

@ManyToMany(mappedBy = "accServices")
private List<BaseSpecification> baseSpecifications;

@ManyToMany(mappedBy = "accServices")
private List<IndustrySpecification> industrySpecifications;

BaseSpecification.java

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "BASE_SPEC_SERVICES",
joinColumns = {@JoinColumn(name = "SPEC_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "SRV_ID", referencedColumnName = "ID")})
private List<AccService> accServices;</code>

IndustrySpecification.java

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "IND_SPEC_SERVICES",
joinColumns = {@JoinColumn(name = "SPEC_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "SRV_ID", referencedColumnName = "ID")})
private List<AccService> accServices;</code>

我正在使用Criteria API进行查询:

Criteria criteria = session.createCriteria(BaseSpecification.class);
criteria.createAlias("accServices", "as");
criteria.createCriteria("as.industrySpecifications", "asis", JoinType.LEFT_OUTER_JOIN,      Restrictions.eq("asis.id", industrySpecId));

Hibernate生成以下查询,由于在第一个“左外连接”子句中使用了错误的别名而失败

select this_.name as y0_, asis3_.name as y1_, 
from BASE_SPEC this_ 
inner join BASE_SPEC_SERVICES accservice5_ on this_.id=accservice5_.SPEC_ID 
inner join ACC_SERVICE as2_ on accservice5_.SRV_ID=as2_.id 
left outer join IND_SPEC_SERVICES industrysp7_ on as2_.id=industrysp7_.SRV_ID and ( asis3_.id=? ) 
left outer join INDUSTRY_SPEC asis3_ on industrysp7_.SPEC_ID=asis3_.id and ( asis3_.id=? ) ;

0 个答案:

没有答案