Search code examples
javasqlhibernatehql

How to get combine Column name using HQL


I have two following Java entities

    @Entity(table="user_profiles")
class Profile{
@Column(name="id")
@Id
@GeneratedValue
private Integer id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

}

And

@Entity
@Table(name="threads")
class Thread {

@Column(name="thread_id")
@Id
@GeneratedValue
private Integer threadId;

@Column(name="to_profile_name")
private String toProfileFullName;

@Column(name="from_profile_name")
private String fromProfileFullName;

@Column(name="to_profile_id")
private Integer toProfileId;

@Column(name="from_profile_id")
private Integer fromProfileId;

}

I need to populate toProfileFullName and fromProfileFullName with firstName+lastName of Profile class, fromProfileId and toProfileId are foreign keys of Profile class, is there any way using HQL? or with Hibernate relationship?


Solution

  • You need to create mapping between Profile and Thread. Look for OneToOne, OneToMany, ManyToOne and ManyToMany, depending on your data model.

    Then I would create transient getter in Thread that would simply return getProfile().getFirstName() + getProfile.getLastName().