I'm trying to map three table columns in three primitive arrays. But whatever I try, I can't get this to work. Here's the code:
class Parent {
private Component component;
}
class Component {
private Parent parent;
private int[] valuesOne;
private int[] valuesTwo;
private double[] valuesThree;
}
Hibernate mapping:
<class name="com.package.Parent" table="parent">
<id name="id" column="id" access="field" type="int">
<generator class="increment"/>
</id>
...
<component name="component" access="field">
<many-to-one name="parent" class="com.package.Parent" insert="false" update="false" fetch="join" column="id" access="field"/>
<primitive-array name="valuesOne" table="component" access="field">
<key column="parent_id"/>
<index column="index"/>
<element column="value_one" type="int"/>
</primitive-array>
<primitive-array name="valuesTwo" table="component" access="field">
<key column="parent_id"/>
<index column="index"/>
<element column="value_two" type="int"/>
</primitive-array>
<primitive-array name="valuesThree" table="component" access="field">
<key column="parent_id"/>
<index column="index"/>
<element column="value_three" type="double"/>
</primitive-array>
</component>
</class>
Database table:
CREATE TABLE parent (
id INTEGER NOT NULL AUTO_INCREMENT,
...
)
CREATE TABLE component (
id INTEGER NOT NULL AUTO_INCREMENT,
parent_id INTEGER NOT NULL,
index INTEGER NOT NULL,
value_one INTEGER NOT NULL,
value_two INTEGER NOT NULL,
value_three DECIMAL NOT NULL
)
Now this half works. Hibernate will create for each value to store a separate query, like this:
Hibernate:
insert
into
component
(parent_id, index, value_one)
values
(?, ?, ?)
What I actually want is:
Hibernate:
insert
into
component
(parent_id, index, value_one, value_two, value_three)
values
(?, ?, ?, ?, ?)
How can I achieve this through Hibernate mapping?
Apparently I was overthinking this, I just needed to create one array with multiple fields.
class Component {
private Parent parent;
private ComponentEntry[] entries;
}
class ComponentEntry {
private int id;
private Component parent;
private int valueOne;
private int valueTwo;
private double valueThree;
}
Then just map the array.
<array name="entries" table="component" cascade="all-delete-orphan" access="field">
<key column="parent_id" not-null="true"/>
<index column="index" type="int"/>
<one-to-many class="ComponentEntry"/>
</array>