簡體   English   中英

使用Hibernate來保存具有超類類型的一對多列表

[英]Using Hibernate to persist a one-to-many list with a superclass type

有人在Hibernate論壇上問了這個問題,我在這里鏈接到它,因為我有同樣的問題。 似乎沒有人在那里提供任何幫助,所以我希望這可能更有用。

這里是:

關於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類的角色)作為抽象 ???

package br.com.ar.model.domain;

@Entity
/**
  * I am using InheritanceType.SINGLE_TABLE instead of TABLE_PER_CLASS
  * Because MySQL (DB used) DOES NOT SUPPORT identity generation strategy
  * Otherwise, i will get some Exception
  */
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="BILLING_DETAILS_TYPE",
    discriminatorType=DiscriminatorType.STRING
)
/**
  * ARE YOU SURE your Book class HAS BEEN MARKED AS abstract
  */ 
public abstract class AbstractChild implements Serializable {

    private Integer id;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

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

}

mapping.hbm.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();
}

兩者都很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM