-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Help with i18n Component Mapping
PostPosted: Wed Jul 09, 2008 10:16 am 
Newbie

Joined: Wed Jul 09, 2008 5:44 am
Posts: 1
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?


Last edited by Dimitris Menounos on Wed Jul 09, 2008 7:08 pm, edited 4 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 5:06 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This is absolutely possible. You don't absolutely need to map the association.

Just have a getter method that can provide the query the data it needs. Then, simply return the results of the HQL query. It will be very slick, and very efficient.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.