Apr 20

The mapping configuration can vary a little depending on the exact relationship involved but the following example should give you an isea what is required. In this example there is a one-to-many relationship to the objects in the array.

The Parent class has array of Child instances as a property.

public class Parent {
	private Integer id;
	private Child[] array;

	public Integer getId() {
		return id;
	}

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

	public Child[] getArray() {
		return array;
	}

	public void setArray(Child[] array) {
		this.array = array;
	}
}

The Child class in this example is just a simple bean.

public class Child {
	private Integer id;

	public Integer getId() {
		return id;
	}

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

The required mapping is shown in the following xml snippet.


<class name="Parent">
		
	<id name="id">
		<generator class="increment"/>
	</id>

	<array name="array" cascade="all" fetch="join"  >
		<key column="aid"/>
		<list-index column="id"/>
		<one-to-many class="Child"/>
	</array>
</class>

<class name="Child" lazy="true">
	<id name="id">
		<generator class="increment"/>
	</id>
</class>

If I get some time I’ll try and also post how to specify the mapping using hibernate annotations.

written by objects \\ tags: , ,


One Response to “How to map an array using hibernate?”

  1. Alessandro Says:

    Hi.. do you an implementations of array with annotations? thanks

Leave a Reply

You must be logged in to post a comment.