Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

java - Ascend HQL inheritance hierarchy from subclass to superclass

Given three entities A, B and C where B extends A such as :

@Entity
@Table(name = "a")
public class A {
    private C c;
    //omitted source code

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "c_id", nullable = false)
    public C getC() {
       return c;
    }
    public void setC(C c){
       this.c=c;
    }
}

@Entity
@Table(name = "b")
public class B extends A {
    private Integer id;
    //omitted source code
}

@Entity
@Table(name = "c")
public class C {
    private Integer id;
    private String status;
    //omitted source code
}

How can I convert to following native SQL to HQL :

select b.* from b b_
inner join a a_ on a_.id=b_.id
inner join c c_ on a_.c_id=c_.id
where c_.status='ACCEPTED';

I tried the following : select b from B b join b.c c where c.status = 'ACCEPTED' however I get this exception : org.hibernate.QueryException: could not resolve property: c of: B. I thought Hibernate would resolve the superclass fields at runtime.

What I am trying to achieve here is to ascend the inheritance hierarchy starting exactly from from B and read the superclass A inner fields, in particular c. I wonder if this is doable in hibernate. Otherwise any alternative solution will be more than welcome.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change the visibility of the C instance variable inside A:

public class A {
    protected C c;
    //....

protected ensures the children of A (and also other members in the same package where A is located) to be able to directly access c, as it becomes an inherited variable.

This way, c is visible from any B instance, leading to bInstance.c being valid. As a result, hibernate would identify select b from B b join b.c c as a valid command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...