3

现有类之一(A 类)同时具有@Entity@MappedSuperclass注释,而子类(B 类扩展 A)具有@Entity.

虽然这不对,但它在 hibernate-entitymanager 3.3 中运行良好。两个实体都有自己的表,B 类的表具有与 A 类相同的列,外加一个对应于其自身属性的附加列。

我正在尝试修复这个存在多年的错误,以便我可以迁移到 3.6,因为它不允许两个注释。(它抛出org.hibernate.AnnotationException: An entity cannot be annotated with both @Entity and @MappedSuperclass:

我试图替换@MappedSuperclass为,@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)但现在在加载另一个类时出现运行时错误,该类的一个属性是一组 B。

org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:78) - Unknown column 'A.createdDateTime' in 'order clause'
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.acmy.AnotherClassC.classBset#975905b5-d59c-4e53-98dd-30cf39b0c831]

@Entity修复同时使用and@MappedSuperclass使用@Entityand的 A 类的平滑方法是@Inheritance什么?我需要任何 SQL 来修复现有数据吗?

4

1 回答 1

3

您可以删除@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>
于 2014-04-15T18:27:26.047 回答