如何在 JPA 中编写命名本机子查询?

发布于 2024-11-25 23:56:47 字数 2599 浏览 2 评论 0原文

我的任务是编写一个命名的本机子查询,例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-12-02 23:56:47

It appears that this doesn't work on Hibernate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文