How to write named native subqueries in JPA?
I was given a task to write a named native subquery like
select c.comp_name from company c where c.cid = (select cid from com_usr_rel where uid=1100)
The entities are
company
@Entity
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="company_id")
private String companyId;
@Column(name="apartment_no")
private BigDecimal apartmentNo;
private String city;
@Column(name="company_name")
private String companyName;
//bi-directional many-to-one association to CompanyUser
@OneToMany(mappedBy="company")
private List<CompanyUser> companyUsers;
public Company() {
}
public String getCompanyId() {
return this.companyId;
}
public void setCompanyId(String companyId) {
this.companyId = companyId;
}
public BigDecimal getApartmentNo() {
return this.apartmentNo;
}
public void setApartmentNo(BigDecimal apartmentNo) {
this.apartmentNo = apartmentNo;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getCompanyName() {
return this.companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public List<CompanyUser> getCompanyUsers() {
return this.companyUsers;
}
public void setCompanyUsers(List<CompanyUser> companyUsers) {
this.companyUsers = companyUsers;
}}
com_usr_rel
@Entity
@Table(name="com_usr_rel")
public class CompanyUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="com_usr_rel_id")
private String comUserId;
//bi-directional many-to-one association to Company
@ManyToOne
@JoinColumn(name="company_id")
private Company company;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="user_id")
private User user;
public CompanyUser() {
}
public String getCom_usr_relId() {
return this.comUserId;
}
public void setCom_usr_relId(String com_usr_relId) {
this.comUserId = comUserId;
}
public Company getCompany() {
return this.company;
}
public void setCompany(Company company) {
this.company = company;
}
public User getUser()开发者_如何学Go {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
@Update OOPS.
The Named Native query does not execute. It gives me an exception saying that Native Scalar queries are not supported.
org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
@Update
Solved it by using relationship in the entities.
It appears that this doesn't work on Hibernate
精彩评论