Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Hibernate持久化具有超类类型的一对多列表_Java_Database_Hibernate_Persistence_Polymorphism - Fatal编程技术网

Java 使用Hibernate持久化具有超类类型的一对多列表

Java 使用Hibernate持久化具有超类类型的一对多列表,java,database,hibernate,persistence,polymorphism,Java,Database,Hibernate,Persistence,Polymorphism,有人在Hibernate论坛上问了这个问题,我在这里链接到它,因为我有同样的问题。似乎没有人能帮上什么忙,所以我希望这会更有用 这是: 谢谢。很有趣。这就是我所做的 package br.com.ar.model.domain.Parent; @Entity public class Parent implements Serializable { private Integer id; private AbstractChild[] childArray; @

有人在Hibernate论坛上问了这个问题,我在这里链接到它,因为我有同样的问题。似乎没有人能帮上什么忙,所以我希望这会更有用

这是:


谢谢。

很有趣。这就是我所做的

package br.com.ar.model.domain.Parent;

@Entity
public class Parent implements Serializable {

    private Integer id;

    private AbstractChild[] childArray;

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @OneToMany
    @IndexColumn(name="CHILD_INDEX", nullable=false)
    @JoinColumn(name="PARENT_ID", nullable=false)
    @Cascade(CascadeType.SAVE_UPDATE)
    public AbstractChild[] getChildArray() {
        return childArray;
    }

    public void setChildArray(AbstractChild[] childArray) {
        this.childArray = childArray;
    }

}
现在,我们的AbstractChild数组(您确定您已经将AbstractChild(在这里,它扮演Book类的角色)定义为AbstractChild吗?

mapping.hbm.xml(类路径的根)

和无

@Test
public void doWithoutAnnotations() {

    Configuration configuration = new Configuration().configure();

    SessionFactory sessionFactory = configuration.buildSessionFactory();

    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Parent parent = new Parent();
    parent.setChildArray(new AbstractChild[] {new Child("AAA"), new Child("BBB")});

    session.save(parent);

    session.getTransaction().commit();
    session.close();
}

两者都很好

您尝试了什么?使用注释或XML映射?我正在使用XML映射,就像这家伙在他的问题中所做的那样。你是对的。问题在于,如果您使用的是表对具体类的映射策略,那么就不能有一个超类列表。每个层次结构的此表适用于此问题。谢谢
<hibernate-mapping>
  <class name="br.com.ar.model.domain.AbstractChild" abstract="true">
    <id column="id" name="id" type="integer">
      <generator class="native"/>
    </id>
    <discriminator column="BILLING_DETAILS_TYPE" type="string"/>
    <subclass name="br.com.ar.model.domain.Child" discriminator-value="CC">
        <property name="someProperty" type="string"/>
    </subclass>
    <!--I CAN NOT USE union-subclass because MySQL does not support DB identity generation strategy-->
    <!--union-subclass name="br.com.ar.model.domain.Child">
        <property name="someProperty" type="string"/>
    </union-subclass-->
  </class>
  <class name="br.com.ar.model.domain.Parent">
    <id column="id" name="id" type="integer">
      <generator class="native"/>
    </id>
    <array name="childArray" cascade="all">
        <key column="PARENT_ID"/>
        <index column="SORT_ORDER"/>
        <one-to-many class="br.com.ar.model.domain.AbstractChild"/>
    </array>
  </class>
</hibernate-mapping>
@Test
public void doWithAnnotations() {

    AnnotationConfiguration configuration = new AnnotationConfiguration();

    SessionFactory sessionFactory = configuration

        .addAnnotatedClass(Parent.class)
        .addAnnotatedClass(Child.class)
        .addAnnotatedClass(AbstractChild.class)
        .setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver")
        .setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/ar")
        .setProperty(Environment.USER, "root")
        .setProperty(Environment.PASS, "root")
        .setProperty(Environment.SHOW_SQL, "true")
        .setProperty(Environment.FORMAT_SQL, "true")
        .setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect")
        .setProperty(Environment.HBM2DDL_AUTO, "create-drop").buildSessionFactory();

    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Parent parent = new Parent();
    parent.setChildArray(new AbstractChild[] {new Child("AAA"), new Child("BBB")});

    session.save(parent);

    session.getTransaction().commit();
    session.close();
}
@Test
public void doWithoutAnnotations() {

    Configuration configuration = new Configuration().configure();

    SessionFactory sessionFactory = configuration.buildSessionFactory();

    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Parent parent = new Parent();
    parent.setChildArray(new AbstractChild[] {new Child("AAA"), new Child("BBB")});

    session.save(parent);

    session.getTransaction().commit();
    session.close();
}