【问题标题】:Query attributes of extended class in HibernateHibernate中扩展类的查询属性
【发布时间】:2016-06-27 02:04:43
【问题描述】:

我有四个类,一个代表一个产品,另一个代表这个产品的一个属性,另外两个保存这个属性的值,因为它可以是字符串或整数,都有 getter 和 setter(这里省略)。

产品.class

@Entity
@Table(name = "product")
public class Product {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "product", cascade = CascadeType.MERGE, fetch = FetchType.EAGER, orphanRemoval = true)
    private Set<Attribute> attributes = new HashSet<>(0);

    [...]
}

属性类

@Entity
@Table(name = "attribute")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
public class Attribute {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "product_id")
    private Product product;

    @ManyToOne
    @JoinColumn(name = "custom_attribute_id")
    private CustomAttribute customAttribute;

    @Column(name = "type")
    private Integer type;

    [...]
}

AttributeString.class

@Entity
@Table(name = "attribute_string")
@DiscriminatorValue("1")
public class AttributeString extends Attribute {

    @Column(name = "value")
    private String value;

    [...]
}

AttributeInteger.class

@Entity
@Table(name = "attribute_integer")
@DiscriminatorValue("2")
public class AttributeInteger extends Attribute {

    @Column(name = "value")
    private Integer value;

    [...]
}

我想使用 Hibernate Criteria 创建一个投影查询来计算产品具有的不同属性和值的数量,如下所示:

// Projection query to list the summary of attribute
Session session = (Session) em.getEntityManagerFactory().createEntityManager()
            .getDelegate();
Criteria criteria = session.createCriteria(Product.class);
criteria.creatAlias("attributes", "attribute");
criteria.creatAlias("attribute.customAttribute", "customAttribute");
Projection projectionSummary = Projections.projectionList()
        .add(Projections.groupProperty("customAttribute.id"))
        .add(Projections.groupProperty("attribute.value"))
        .add(Projections.count("id"));
List<Object[]> items = criteria.setProjection(projectionSummary).list();

但这会引发错误org.hibernate.QueryException: could not resolve property: value of com.example.entity.Attribute

【问题讨论】:

    标签: java hibernate projection


    【解决方案1】:

    正如你的例外所说

    无法解析属性:com.example.entity.Attribute 的值

    Attribute.classgetter 和setter 方法有问题。它可能不遵循约定。

    如果你使用任何IDE like eclipse,那么尝试使用它来创建getter setter方法,这将帮助你更多地遵循约定。

    id, product, customAttribute and type 创建getter 和setter。

    id为例:

    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    

    更新:

    我认为attributes 将是attribute。请检查我是否错了。

    criteria.creatAlias("attributes", "attribute");
    

    使用以下代码

    criteria.creatAlias("attribute", "attribute");
    

    【讨论】:

    • 它有getter和setter,我省略了它们只是为了简化问题。
    • @Magritte 您使用的是哪个 IDE?
    • Eclipse Mars.2,应用服务器为JbossAS 7.1.1,JPA 2.0,Hibernate Core 4.0.1.Final
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2014-05-23
    • 1970-01-01
    • 2020-03-28
    相关资源
    最近更新 更多