简体   繁体   English

Hibernate生成模棱两可的SQL查询

[英]Hibernate generates ambiguous SQL query

Consider the following ParentClass entity: 考虑以下ParentClass实体:

@Entity
@Table(schema = "iwrs")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class ParentClass {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PK_IWRS")
    public int id;

    public String name;

    public ParentClass(String name) {
        this.name = name;
    }
}

And the following ChildClass entity: 以及以下ChildClass实体:

@Entity
@Table(schema = "iwrs")
public class ChildClass extends ParentClass {

    @ManyToOne(targetEntity=ParentClass.class)
    @JoinColumn(name="parent_id")
    private ParentClass parent;

    public ChildClass(String name) {
        super(name);
    }
}

As you can see, this ChildClass extends from ParentClass. 如您所见,此ChildClass继承自ParentClass。 Moreover, it contains a reference for ParentClass mapped in parent field. 此外,它包含在字段中映射的ParentClass的引用。

There is a point where I want to get all instances of ParentClass, but not the instances that are ChildClass. 有一点我想获取ParentClass的所有实例,而不是ChildClass的实例。

I searched around and found that could be achieved with this criteria: 我四处搜寻,发现可以通过以下条件实现:

Criteria criteria = sessionFactory.getCurrentSession()
                .createCriteria(ParentClass.class, "parentClass")
                .add(Restrictions.eq("class", ParentClass.class));

However, when I try to list it, I get the following error: 但是,当我尝试列出它时,出现以下错误:

ERROR SqlExceptionHelper:147 - ERROR: column reference "clazz_" is ambiguous 错误SqlExceptionHelper:147-错误:列引用“ clazz_”不明确

If I remove the last line of the criteria, the query is successfully executed. 如果删除条件的最后一行,则查询将成功执行。 But the ChildClass instances are also returned, which is not what I want. 但是还会返回ChildClass实例,这不是我想要的。

Here is the query generated by hibernate when I have all the restrictions: 当我有所有限制时,这是由休眠生成的查询:

select this_.id as id1_29_1_, this_.name as name2_29_1_, this_.parent_id as parent_i1_14_1_, this_.clazz_ as clazz_1_, parentclas2_.id as id1_29_0_, parentclas2_.name as name2_29_0_, parentclas2_.parent_id as parent_i1_14_0_, parentclas2_.clazz_ as clazz_0_ from ( select id, name, null::int4 as parent_id, 0 as clazz_ from iwrs.parent_class union all select id, name, parent_id, 1 as clazz_ from iwrs.child_class ) this_ left outer join ( select id, name, null::int4 as parent_id, 0 as clazz_ from iwrs.parent_class union all select id, name, parent_id, 1 as clazz_ from iwrs.child_class ) parentclas2_ on this_.parent_id=parentclas2_.id where clazz_=? 选择this_.id作为id1_29_1_,this_.name作为name2_29_1_,this_.parent_id作为parent_i1_14_1_,this_.clazz_作为clazz_1_,parentclas2_.id作为id1_29_0_,parentclas2_.name作为name2_29_0_,parentclas2_.parent_id从parent_i_14作为父母_i1。选择id,name,null :: int4作为parent_id,从iwrs.parent_class联合中选择0作为clazz_,都从idrs.child_class中选择id,name,parent_id,作为clazz_)this_左外部联接(选择id,name,null :: int4作为parent_id,0作为来自iwrs.parent_class联合的clazz_都选择id,名称,parent_id,1作为来自iwrs.child_class的clazz_)this_.parent_id = parentclas2_.id上的parentclas2_其中clazz_ =?

Working example available here: https://github.com/mmalmeida/hibernateTest , just run the test RetrieveParentTest.java. 此处提供工作示例: https : //github.com/mmalmeida/hibernateTest ,只需运行测试RetrieveParentTest.java。

Do you know how I can work around this problem? 您知道我如何解决这个问题吗?

Thanks in advance! 提前致谢!

Try using the alias you defined: 尝试使用您定义的别名:

Criteria criteria = sessionFactory.getCurrentSession()
                    .createCriteria(ParentClass.class, "parentClass")
                    .add(Restrictions.eq("parentClass.class", ParentClass.class));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM