[英]JPA/Hibernate Selecting from two entities where two different columns
Assuming I have two Entities that are related eg AccountTypes and Accounts that looks something like this 假设我有两个相关的实体,例如AccountTypes和Accounts看起来像这样
//Accounts.java
@Entity
@Table(name = "accounts")
@XmlRootElement
public class Accounts implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "account_id")
private Long accountId;
@JoinColumn(name = "account_type_id", referencedColumnName = "account_type_id")
@ManyToOne(optional = false)
private AccountTypes accountTypeId;
@JoinColumn(name = "customer_id", referencedColumnName = "customer_id")
@ManyToOne(optional = false)
private CustomerInformation customerId;
//AccountTypes.java
@Entity
@Table(name = "account_types")
@XmlRootElement
public class AccountTypes implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "account_type_id")
private Integer accountTypeId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "account_type_name")
private String accountTypeName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "accountTypeId")
private Collection<CustomerAccounts> customerAccountsCollection;
I want to be able to 我希望能够
select from accounts where accounts.customerId=? and accountTypes.accountTypeName=?.
Any JPA compliant solution will work for me, I'd just rather not run two different queries. 任何符合JPA的解决方案都可以为我工作,而我宁愿不要运行两个不同的查询。
I am assuming CustomerInformation
has customerId
field in Long
type: 我假设CustomerInformation
在Long
类型中具有customerId
字段:
String hqlString = "select account from Accounts account where account.customerId.customerId=:customerId and account.accountTypeId.accountTypeName=:accountTypeName";
Query query = getSession().createQuery(hqlString);
query.setLong("customerId", customerId);
query.setString("accountTypeName", accountTypeName);
Accounts account = (Accounts) query.uniqueResult(); //if it's not unique then query.list()
I suggest you to rename your classes into Account
and AccountType
. 建议您将类重命名为Account
和AccountType
。 Also don't name an object with id like you did private CustomerInformation customerId;
也不要像使用private CustomerInformation customerId;
那样用id命名对象private CustomerInformation customerId;
You will see how screwed hql is written. 您将看到如何编写错误的hql。
You can use following query as per the your entity class you have created.. 您可以根据您创建的实体类使用以下查询。
select from Account acnt
where acnt.customerId=:customerId
and acnt.accountTypeId.accountTypeName = :accountTypeName
Thanks for your answers but this is what worked for me - 感谢您的回答,但这对我有用-
String hql_string = "from Accounts acnt where acnt.customerId.id=:custmerID and acnt.accountTypeId.accountTypeName=:typeName";
Query query = entityManager.createQuery(hql_string);
query.setParameter("custmerID", new Long(customer_id));
query.setParameter("typeName", "Account Type Name");
And thanks #Mustafa, would change the variables names. 并感谢#Mustafa,将更改变量名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.