0

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?

Ali
  • 43
  • 1
  • 18

1 Answers1

1

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().

Vlastimil Ovčáčík
  • 2,799
  • 27
  • 29
  • For the HQL solution and more see http://stackoverflow.com/questions/17910428/can-we-concatenate-two-properties-in-hibernate-hql-query I kinda like the `@Formula` way, but the transient getter I propose in my answer is by far the simplest (but not available in HQL). – Vlastimil Ovčáčík Feb 15 '16 at 15:17