我有一个使用 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();
}
}