您可以删除@MappedSuperClass
注释 - 但保留@Entity
和@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
。
你不能使用@GeneratedValue(strategy = GenerationType.AUTO)
但即@GeneratedValue(strategy = GenerationType.TABLE)
更新
一个例子 - 对我有用
package-info
在类中注册生成器
包信息.java
@GenericGenerator(name = "system-uuid", strategy = "uuid")
package db;
import org.hibernate.annotations.GenericGenerator;
你的 A 类ClassA.java
package db;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class ClassA implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "system-uuid")
private String id;
private String value;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ClassA other = (ClassA) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
@Override
public String toString() {
return "db.ClassA[ id=" + id.toString() + " ]";
}
}
以及继承的Class B ClassB.java
package db;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("B")
public class ClassB extends ClassA {
private static final long serialVersionUID = 1L;
@Override
public String toString() {
return "db.ClassB[ id=" + getId() + " ]";
}
}
最后但并非最不重要的一个主要测试类
测试数据库.java
package db;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class TestDb {
EntityManager em;
public static void main(String[] args) {
new TestDb().start();
}
public void start() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("dbtest");
em = emf.createEntityManager();
em.getTransaction().begin();
ClassA a = new ClassA();
a.setValue("A");
em.persist(a);
a = new ClassA();
a.setValue("B");
em.persist(a);
ClassB b = new ClassB();
b.setValue("C");
em.persist(b);
em.getTransaction().commit();
em.getTransaction().begin();
List list = em.createQuery("from " + ClassA.class.getSimpleName() + " c order by c.value").getResultList();
System.out.println("List " + list);
em.getTransaction().commit();
}
}
persistence.xml看起来像这样。我将 h2database 与im-memory
数据库一起使用
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="dbtest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>db.ClassA</class>
<class>db.ClassB</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<!--property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/-->
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>