Interesting. Here is what i did
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;
}
}
Now our array of AbstractChild (are you sure you defined your AbstractChild (here it plays the role of the Book class) as abstract ??? )
package br.com.ar.model.domain;
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="BILLING_DETAILS_TYPE",
discriminatorType=DiscriminatorType.STRING
)
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 (root of the class path)
<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>
</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>
Now let test
WITH
@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();
}
And without
@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();
}
Both work great!