Hi I am using Hibernate 3.2.5 and I need some help with the following entity <-> component mapping case:
1.
Node - the entity
2.
NodeInfo - the component that holds information about a Node in multiple languages
Code:
create table Node (
id int not null auto_increment primary key,
....
);
create table NodeInfo (
nodeId int not null,
locale varchar(255) not null,
title varchar(255) not null,
description varchar(255)
);
alter table NodeInfo add foreign key (nodeId) references Node(id);
The way I have found to map this so far is to add a
Map of NodeInfos into Node like this:
Code:
public class Node {
private Integer id;
private Map<Locale, NodeInfo> infos;
...
}
public class NodeInfo {
private String title;
private String description;
...
}
<hibernate-mapping>
<class name="Node">
<map table="NodeInfo" name="infos">
<key not-null="true" column="nodeId"/>
<map-key type="locale" column="locale"/>
<composite-element class="NodeInfo">
<property name="title"/>
<property name="description"/>
</composite-element>
</map>
</class>
</hibernate-mapping>
This works but, because components are treated as values, it means that all node instances have to always drag along all their related node-infos.
For example to access a single node-info via node.getInfos().get(locale) the whole map will be have to be loaded. Moreover if I load a node -> send it to the client without having the node-infos initialized -> then later get it back and merge() it's node-infos will be wiped.
I don't want this. What I really want is to somehow make the relationship inverse. If possible to make the Node class completely
unaware of the NodeInfos but to be able to retrieve them with a query like:
"from NodeInfo where nodeId = :id and locale = :locale"
Is that possible?