如何在 JPA 中编写命名本机子查询?
我的任务是编写一个命名的本机子查询,例如
select c.comp_name from company c where c.cid = (select cid from com_usr_rel where uid=1100)
实体是
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() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
@Update 哎呀。
命名本机查询不执行。它给了我一个例外,说不支持本机标量查询。
org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
@Update
通过使用实体中的关系解决了这个问题。
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() {
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来这个 在 Hibernate 上不起作用
It appears that this doesn't work on Hibernate