Java 如何在成员对象的特定字段上应用投影?

Java 如何在成员对象的特定字段上应用投影?,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,我有一个类,它有一个成员对象列表,我只需要检索成员对象的几个字段,我怎么做?当我执行生成的查询时,它只显示那些同时具有所有者和用户的记录,那些没有所有者和用户的记录将不会包含在查询结果中 @Entity public class Category implements Serializable { @Id @GeneratedValue private long id; @OneToOne private List<Product> prod

我有一个类,它有一个成员对象列表,我只需要检索成员对象的几个字段,我怎么做?当我执行生成的查询时,它只显示那些同时具有所有者和用户的记录,那些没有所有者和用户的记录将不会包含在查询结果中

@Entity
public class Category implements Serializable {

    @Id
    @GeneratedValue
    private long id;
    @OneToOne
    private List<Product> products = new ArrayList()

    ...
}

@Entity
public class Product {

    @Id
    @GeneratedValue
    private long id;
    private float price;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date regDate;
    @OneToOne
    private Person owner;
    @OneToOne
    private Person user;


    ...
}
结果应如下所示

public class CategoryResult {


    private long cid;
    private List<ProductResults> products = new ArrayList()

    ...
}

public class ProductResults {


    private long pid;
    private float price;
    private Date regDate;
    private String owFname;
    private String owLname;
    private String usFname;
    private String usLname;
    ...
}
public类CategoryResult{
私人长cid;
私有列表产品=新的ArrayList()
...
}
公共类产品结果{
私人长pid;
私人浮动价格;
非公开日期;
私有字符串owFname;
私有字符串名称;
私有字符串usFname;
私有字符串usLname;
...
}

您应该尝试将代码中的
别名替换为ResultTransformer

public class CategoryResultTransformer extends AliasToBeanResultTransformer {
    public static final String CID_ALIAS = "cid";
    private int cidIndex = -1;

    public CategoryResultTransformer() {
        super(ProductResults.class);
    }

    @Override
    public Object transformTuple(Object[] tuple, String[] aliases) {
        if (cidIndex < 0) {
            for (int i = 0; i < aliases.length; ++i) {
                if (CID_ALIAS.equals(aliases[i])) {
                    cidIndex = i;
                    break;
                }
            }
        }

        return new Object[] { tuple[cidIndex], super.transformTuple(tuple, aliases) };
    }

    @Override
    public List transformList(List list) {
        List<CategoryResult> res = new ArrayList<CategoryResult>();
        Map<Long, CategoryResult> map = new HashMap<Long, CategoryResult>();

        for (Object[] row : (List<Object[]>)list) {
            long cid = ((Number)row[0]).longValue();
            CategoryResult cat = map.get(cid);

            if (cat == null) {
                cat = new CategoryResult();
                cat.setCid(cid);
                res.add(cat);
                map.put(cid, cat);
            }

            cat.getProducts().add((ProductResults)row[1]);
        }

        return res;
    }
}
不是吗

public class CategoryResultTransformer extends AliasToBeanResultTransformer {
    public static final String CID_ALIAS = "cid";
    private int cidIndex = -1;

    public CategoryResultTransformer() {
        super(ProductResults.class);
    }

    @Override
    public Object transformTuple(Object[] tuple, String[] aliases) {
        if (cidIndex < 0) {
            for (int i = 0; i < aliases.length; ++i) {
                if (CID_ALIAS.equals(aliases[i])) {
                    cidIndex = i;
                    break;
                }
            }
        }

        return new Object[] { tuple[cidIndex], super.transformTuple(tuple, aliases) };
    }

    @Override
    public List transformList(List list) {
        List<CategoryResult> res = new ArrayList<CategoryResult>();
        Map<Long, CategoryResult> map = new HashMap<Long, CategoryResult>();

        for (Object[] row : (List<Object[]>)list) {
            long cid = ((Number)row[0]).longValue();
            CategoryResult cat = map.get(cid);

            if (cat == null) {
                cat = new CategoryResult();
                cat.setCid(cid);
                res.add(cat);
                map.put(cid, cat);
            }

            cat.getProducts().add((ProductResults)row[1]);
        }

        return res;
    }
}
@OneToOne
private List<Product> products = new ArrayList()