0

我有一个使用 DiscriminatorColumn 的抽象实体,并由各种实体子类化。现在,当我在抽象类中查询命名查询时,它会抛出一个错误,说“org.hibernate.InstantiationException 无法实例化抽象类或接口”。经过分析,我发现鉴别器列未附加在生成的本机 SQL 本身中。以下是实体映射的简化版本:

  package com.qz.test;

import javax.persistence.*;

/**
 * User: r4rao
 * Date: 7/18/11
 * Time: 5:36 PM
 */
@Entity
@Table(schema = "ems", name = "nlt_content_profile")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "content_profile_type", discriminatorType = DiscriminatorType.STRING)
@NamedQueries({
        @NamedQuery(name = "A.getProfilesForDelivery", query = "select pcp from A pcp where pcp.serviceId = :service_id ")
//      @NamedQuery(name = "A.getProfilesByIdsForDelivery", query = "select pcp,s from A pcp,Subscription s LEFT JOIN FETCH s.client c LEFT JOIN FETCH c.operator where pcp.id in (:cp_ids) and pcp.service.id = :service_id and s.contentProfile.id = pcp.id and s.status in (:statuses) and  (:use_timestamp = false or (:use_timestamp = true and s.beginningTime < :timestamp)))"),
//      @NamedQuery(name = "A.removeUnusedContentProfiles", query = "DELETE FROM A pcp WHERE NOT EXISTS(SELECT 1 FROM Subscription sub WHERE sub.contentProfile.id = pcp.id)"),
//      @NamedQuery(name = "A.getProfileIdsForDelivery", query = "select pcp.id from A pcp where pcp.service.id = :service_id and exists(select 1 from Subscription s where s.contentProfile.id = pcp.id and s.status in (:statuses))")
})
public abstract class A{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "ContentProfileSeq")
    @SequenceGenerator(name = "ContentProfileSeq", sequenceName = "ems.nlt_content_profile_seq")
    private Long id;

    public Long getId() {
        return id;
    }

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


    @Column(name = "service_id")
    private Long serviceId;

    public Long getServiceId() {
        return serviceId;
    }

    public void setServiceId(Long serviceId) {
        this.serviceId = serviceId;
    }
}

package com.qz.test;

import javax.persistence.*;

/**
 * User: r4rao
 * Date: 7/18/11
 * Time: 5:36 PM
 */
@Entity
public class B extends A {
    @Column(name = "subscription_parameter")
    private String param;

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("B");
        sb.append("{param='").append(param).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
4

2 回答 2

1

我发现这是由于配置错误。在persistence.xml 中仅配置了抽象类,而缺少任何具体子类的配置。在为具体类添加配置时,此错误已解决。

令人惊讶的是,它没有在生成的查询中使用鉴别器列附加过滤器子句,而不是引发适当的错误。

于 2011-09-05T08:58:59.797 回答
0

您的命名查询从中选择A意味着“给我所有A符合给定条件的实例及其子类”。Hibernate 没有理由过滤鉴别器列,因为无论如何您都在寻找它的所有可能值。

有问题的错误很可能是由基础数据库表中的不正确数据引起的 - 例如,您的记录具有指向“A”的鉴别器列值。Hibernate 尝试实例化A类来保存它们并且不能(因为它是抽象的),因此它会引发错误。

于 2011-08-04T17:40:21.313 回答