我正在研究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)