Reputation: 75
I know, that there is a way to map Map via XML:
<set name="images"
table="ITEM_IMAGE"
order-by="IMAGENAME asc">
<key column="ITEM_ID"/>
<composite-element class="Image">
<property name="name" column="IMAGENAME" not-null="true"/>
<property name="filename" column="FILENAME" not-null="true"/>
<property name="sizeX" column="SIZEX" not-null="true"/>
<property name="sizeY" column="SIZEY" not-null="true"/>
</composite-element>
</set>
Is there way to do the same thing via annotations mapping, and how to persist it into separate table?
Thatks a lot!
Upvotes: 2
Views: 89
Reputation: 6574
For composite values or basic types it should be almost the same, first you need to tell hibernate that we are mapping to a collection then specify the name of the table which should hold your map and the join column with your current entity , then you specify the key.
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Item_ID")
private Long itemId;
@ElementCollection
@CollectionTable(name="ITEM_IMAGE", joinColumns=@JoinColumn(name="ITEM_ID"))
@MapKeyColumn(name="ITEM_ID")
private Map<String, Image> contacts = new HashMap<String, Image>();
}
@Embeddable
public class Image {
//Specify your composite attributes with the column name for each
}
hope I did not miss anything
Upvotes: 1