Связать две таблицы в Hibernate
есть два класса, описывающие две таблицы соответственно:
Класс Service
@Entity
@Table(name = "services")
public class Service {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String srvcode;
@Column
private String srvname;
public Service() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSrvcode() {
return srvcode;
}
public void setSrvcode(String srvcode) {
this.srvcode = srvcode;
}
public String getSrvname() {
return srvname;
}
public void setSrvname(String srvname) {
this.srvname = srvname;
}
}
Класс OtherService
@Entity
@Table(name = "other_service")
public class OtherService {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String srvcode;
@Column
private String srvname;
@Column
private int type;
public OtherService() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSrvcode() {
return srvcode;
}
public void setSrvcode(String srvcode) {
this.srvcode = srvcode;
}
public String getSrvname() {
return srvname;
}
public void setSrvname(String srvname) {
this.srvname = srvname;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
Подскажите пожалуйста как связать эти классы (таблицы) по полям Service.srvcode = OtherService.srvcode, чтобы в итоге получить все поля из Service и поле OtherService.type?
UPDATE
Попробовала реализовать так:
Создала новый класс, который содержит поля из Service и OtherService
ServiceNew
public class ServiceNew {
private int id;
private String srvcode;
private String srvname;
private int type;
public ServiceNew() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSrvcode() {
return srvcode;
}
public void setSrvcode(String srvcode) {
this.srvcode = srvcode;
}
public String getSrvname() {
return srvname;
}
public void setSrvname(String srvname) {
this.srvname = srvname;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
Запрос:
TypedQuery<Object[]> query = entityManager.
createQuery("select srv.id, srv.srvname, srv.srvcode, osrv.type from Service srv join OtherService osrv on srv.srvcode = osrv.srvcode", Object[].class);
List<ServiceNew> list = new ArrayList<>();
for (Object[] results : query.getResultList()) {
ServiceNew sNew = new ServiceNew();
sNew.setId((int) results[0]);
sNew.setSrvname((String) results[1]);
sNew.setSrvcode((String) results[2]);
sNew.setType((int) results[3]);
list.add(sNew);
}
System.out.println(list);