[英]Read data from table where primary key is composite using hibernate
I have these POJO classes in my project. 我的项目中有这些POJO类。
public class MerchantChainUser extends com.avanza.ni.common.dto.AbstractDTO
implements java.io.Serializable {
private long chainId;
private CompositePK compositePK;
public MerchantChainUser() {
}
public void setChainId(long chainId) {
this.chainId = chainId;
}
public long getChainId() {
return chainId;
}
public void setCompositePK(CompositePK compositePK) {
this.compositePK = compositePK;
}
public CompositePK getCompositePK() {
return compositePK;
}
}
AND
public class CompositePK implements Serializable {
private long merchantId;
private long userId;
public void setMerchantId(long merchantId) {
this.merchantId = merchantId;
}
public long getMerchantId() {
return merchantId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public long getUserId() {
return userId;
}
}
hbm.xml file for MerchantUserChain
is MerchantUserChain
hbm.xml文件是
<hibernate-mapping>
<class name="com.avanza.ni.portal.dto.MerchantChainUser" table="MERCHANT_CHAIN_USER">
<composite-id name="compositePK">
<key-property name="merchantId" type="long" column="MERCHANT_ID"></key-property>
<key-property name="userId" type="long" column="MERCHANT_USER_ID"></key-property>
</composite-id>
<property name="chainId" type="long">
<column name="MERCHANT_CHAIN_ID" length="38" />
</property>
</class>
Now what i wanted is i have to read data from the table using just MERCHANT_USER_ID
. 现在我想要的是仅使用
MERCHANT_USER_ID
从表中读取数据。 I am able to retreive whole data from the table but now i want to set a criteria as Only give me those row that MERCHANT_USER_ID is specific
. 我可以从表中检索整个数据,但是现在我想设置一个条件为
Only give me those row that MERCHANT_USER_ID is specific
。 I didn't know how to write data criteria. 我不知道如何编写数据标准。
the answer that i put the comment has been deleted, so i post it here :D 我发表评论的答案已被删除,所以我在这里发布:D
Criteria crit = session.createCriteria(MerchantChainUser.class)
.add(Restrictions.eq("compositePK.userId", userId));
or with hql 或与hql
session.createQuery("from MerchantChainUser where compositePK.userId = :userid").setParameter("userid",userid);
Can you try: 你能试一下吗:
Criteria crit = session.createCriteria(MerchantChainUser.class);
crit.add(Restrictions.eq("compositePK.merchantId", 42));
crit.add(Restrictions.eq("compositePK.userId", 43));
crit.setMaxResults(10);
List result = crit.list();
Vinit VINIT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.