I am learning Hibernate and I am stuck with annotations. I have a POJO Item:
@Entity
public class Item implements Serializable {
@Id
@GeneratedValue
@Column
protected Integer id;
@Column
protected State state;
@ElementCollection
protected Map<Locale, ItemBody> localization = new EnumMap<Locale, T>(Locale.class);
...
}
Locale is an enum of four items:
public enum Locale {
cs, en, de, fr, es
}
ItemBody:
public class ItemBody implements Serializable {
@Column(length = 256)
protected String name;
@Column(columnDefinition = "text")
protected String description;
@Column(columnDefinition = "text")
protected String excerpt;
...
}
I use it as a key to attach translations of texts for Item.
Ideally, Hibernate would generate two classes:
Item (id, state)
ItemBody (item_id, localization_KEY, name, description, excerpt)
where in item_id and localization_KEY form together primary key.
What happens is that Hibernate generates:
Item (id, state)
ItemBody (item_id, localization, localization_KEY)
where localization is of type Bytea and is a serialization of all rows in ItemBody, instead of listing all the rows. I will appreciate any suggestion for what I am doing wrong.
Thanks!
You must annotate ItemBody with
@Embeddable. Otherwise, Hibernate treats it as a serializable object.