如何使用Hibernate检索类的成员对象? [英] How to retrieve a member object of a class using Hibernate?

查看:110
本文介绍了如何使用Hibernate检索类的成员对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我可以成功检索用户的地址字段,为此我需要使用Projection定义其所有字段。想象一下地址有100个字段,在这种情况下,我必须定义所有的字段。

我在想,如果我可以仅仅返回客户的地址对象而不定义Proposition中的所有字段?

我知道我可以检索地址的id并使用它来检索它的对象,但是我想知道是否有其他方法而不是它或定义它的所有字段。

Hibernate $ b

  ... .. 
Criteria cre = session.createCriteria(User.class,user)
.createAlias(user.address,addr);

cre.add(Restrictions.eq(user.id,ID));

ProjectionList pl = Projections.projectionList();
pl.add(Projections.property(addr.id)。as(id));
pl.add(Projections.property(addr.unit)。as(unit));
.......
cre.setProjection(pl);
地址=(地址)cre.list()。get(0);

我也使用了以下代码,但它会出错(无法解析属性:addr of:com.myProject.User)

  pl.add(Projections.property(addr)。as (地址)); 

Java

  @Entity 
public Class User {

@Id
@GeneratedValue
private long id;

@OneToOne
私人地址;

...
}


解决方案使用JPQL / HQL:

 选择一个来自用户您加入u.address a其中u.id =: userId 

Criteria API比JPQL更有限,并且无法选择除根之外的任何其他实体实体。如果查询不必动态组合,则不应使用它。当然,如果这个关联是双向的,你可以简单地使用

 从地址a中选择一个a.user.id =: userId 

或其同等标准:

  Criteria c = session.createCriteria(Address.class,a); 
c.createAlias(a.user,u);
c.add(Restrictions.eq(u.id,userId));


Using following code I can successfully retrieve address fields of a user, to do that I need to define all its fields using Projection. Imagine address has 100 fields, in this case I have to define all of them.

I am wondering if I can return just address object of customer without defining all its fields in Proposition?

I know I can retrieve id of address and use that to retrieve its object, but I am wondering if there is ano other method rather than this or defining all its fields.

Hibernate

            .....
            Criteria cre = session.createCriteria(User.class, "user")
                    .createAlias("user.address", "addr");

            cre.add(Restrictions.eq("user.id", ID));

            ProjectionList pl = Projections.projectionList();
            pl.add(Projections.property("addr.id").as("id"));
            pl.add(Projections.property("addr.unit").as("unit"));
            .......
            cre.setProjection(pl);
            Address address = (Address) cre.list().get(0);

I used the following as well but it runs into error (could not resolve property: addr of: com.myProject.User)

    pl.add(Projections.property("addr").as("address"));

Java

@Entity
public Class User {

     @Id
     @GeneratedValue
     private long id;

     @OneToOne
     private Address address;

     ...
}

解决方案

Use JPQL/HQL:

select a from User u join u.address a where u.id = :userId

The Criteria API is more limited than JPQL, and can't select any other entity than the root entity. It shouldn't be used if the query doesn't have to be dynamically composed. Of course, if the association is bidirectional, you can simply use

select a from Address a where a.user.id = :userId

or its equivalent Criteria:

Criteria c = session.createCriteria(Address.class, "a");
c.createAlias("a.user", "u");
c.add(Restrictions.eq("u.id", userId));

这篇关于如何使用Hibernate检索类的成员对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆