Java Developer
Java Developer

Reputation: 1901

How Join hibernate Value objects using HQL query?

Hi i'm New to write HQL Query please help me..... my Hibernate have three ValueObjects ie.

@Entity
@Table(name="user")
public class UserVO {

    @Id
    @Column(name="S_ID")
    private String s_id;

    @Column(name="FIRSTNAME")
    private String firstName;
    private String email;
}

CourseVO class

    @Entity
@Table(name="course")
public class CourseVO
{
    @Id
    @Column(name="S_ID")
    public String s_id;

    @Column(name="NAME")
    public String name;
}

Skillset VO

@Entity
@Table(name="skillset")
public class SkillsetVO 
{
    @Id
    @Column(name="S_ID")
    public String s_id;

    @Column(name="COURSE_ID")//Foreign Key "USER"
    public String course_id;

    @Column(name="USER_ID")//Foreign key "COURSE"
    public String user_id;

    @Column(name="TEACH_EXP")
    public String teach_Exp;
}

Now How to get Values of FirstName,NAME,TEACH_EXP values using EMAIL of USER table using HQL query

Upvotes: 1

Views: 704

Answers (1)

ssedano
ssedano

Reputation: 8432

If you want to use join syntax in HQL you must work out your mapping accordingly as joins are enabled by mapping.

@Entity class A { @OneToMany private List<B> bs; }
@Entity class B { @Basic int n; }

Enables

select b from A a inner join a.b where a = :id

But with your mapping this is not possible. Also bear in mind that in terms of efficiency most of the RDBMs will perform an inner join on a where a.id = b.id.

select u.firstName, c.name, s.teach_Exp
from UserVO u, CourseVO c, SkillsetVO s 
where
    u.s_id = s.user_id 
    and c.s_id = s.course_id 
    and u.email = :email

But I think that you must review you association. Since looks to me that SkillsetVO.user_id should be SkillsetVO.User (an association to a UserVO entity and same for CourseVO).

Upvotes: 1

Related Questions