双抽象类继承并且无法创建sessionFactory(hibernate)

时间:2012-12-31 00:05:26

标签: java hibernate inheritance sessionfactory

我正在研究Hibernate,我需要两个抽象类。第一个,Product是主要的一个,名为product的数据库上应该没有表。第二个抽象类是ComPart(继承自Product),并且一些计算机部分(如gpu和cpu)继承自此类。应该只有一个名为com_part的表,并且CPU和GPU都应该在此表中(每个层次结构的表)。

@MappedSuperclass
public abstract class Product{

    private long pID;
    protected String manufacturer;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "PID", unique = true, nullable = false)
    public int getPID() {
        return pID;
    }

    @Column(name = "Manufacturer")
    public String getManufacturer() {
        return manufacturer;
    }

}


// computer part abstract class inherited from Product
@Entity
@Table(name = "com_part")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="disc",
    discriminatorType=DiscriminatorType.STRING
)
@AttributeOverrides({
    @AttributeOverride(name="manufacturer", column=@Column(name="Manufacturer"))
})
public abstract class ComPart extends Product {

    private String platform;

    @Column(name = "PLATFORM", length = 10)
    public String getPlatform() {
        return platform;
    }
}


// Various computer hardwares inherited from ComPart
@Entity
@Table(name="com_part")
@DiscriminatorValue("CPU")
public class Processor extends comPart {
    private String socketType;
    private String chipset;

    //getters setters...
}

@Entity
@Table(name="com_part")
@DiscriminatorValue("GPU")
public class GraphicsCard extends comPart {
    private double memory;

    //getters setters...
}

我如何映射所有这些功能?上面的代码甚至无法创建sessionfactory?!它会产生这些错误:

Failed to create sessionFactory object.java.lang.NullPointerException
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.bts.core.hb.HibernateUtil.<clinit>(HibernateUtil.java:35)

Caused by: java.lang.NullPointerException
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1424)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1351)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1733)
    at com.bts.core.hb.HibernateUtil.<clinit>(HibernateUtil.java:32)

0 个答案:

没有答案