简体   繁体   English

从表中读取数据,其中使用休眠模式将主键合成

[英]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.

相关问题 如何从具有多个(复合)主键的表中检索数据。 在休眠状态 - how to retrive data from the table that have multiple(composite) primary key. In Hibernate 使用@Embedded和@Id的Hibernate复合主键 - Hibernate composite primary key using @Embedded and @Id 主键也是外键的表上的复合键? - Composite key on table where primary key is also foreign key? 在具有复合主键的表上使用Spring JpaRepository(具有Hibernate 4作为提供程序)会抛出IdentifierGenerationException - Using Spring JpaRepository, with Hibernate 4 as the provider, on a table with composite primary key throws an IdentifierGenerationException 休眠中的复合主键问题 - Composite primary key issue in hibernate 复合主键的休眠问题 - hibernate issues with composite primary key 如何使用复合主键为下表结构编写休眠映射 - how to write hibernate mapping for below table structure with composite primary key hibernate&jpa:具有复合主键的表:自动递增问题 - hibernate & jpa : table with composite primary key : auto increment problem 如何在hibernate中使用作为另一个表的复合主键的外键的一部分作为主键? - How to use part of foreign key which is composite primary key of another table as a primary key in hibernate? 如何从非主键表的休眠列表中选择数据,其中所有列的where子句数据都相同 - how to select data from hibernate list on non primary key table in which where clause data is same for all the column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM