Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA question and i18n problem

 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have the following two tables:

{code}

CREATE TABLE Sculpture
(
sculpture_ID integer NOT NULL PRIMARY KEY,
hauteur integer NOT NULL,
largeur integer NOT NULL,
annee year NOT NULL,
prix double,
nombreExemplaires integer NOT NULL,
actif bool DEFAULT false NOT NULL
);

CREATE TABLE Sculpture_i18n
(
sculpture_id integer NOT NULL ,
locale char(2) NOT NULL ,
titre varchar (50) NOT NULL,
description varchar(255),
FOREIGN KEY(sculpture_id) REFERENCES Sculpture (sculpture_ID)
);

{code}

I have generated the associated entity classes using netbeans.

in the Sculpture_i18n table, I have as many lines per sculpture as there are locales in the app. Say I have two locales: French and English. I'll have the following rows in the Sculpture_i18n:

1 "en" "woman" "a woman's bust"
1 "fr" "femme" "buste de femme"
2 "en" "dog" "a black dog"
2 "fr" "chien" "un chien noir"
etc...

I want to be able to retrieve a sculpture together with its localized information using jpa.

As of now my DAO looks like that and does not handle i18n:

{code} public Sculpture findBySculptureID(Integer sculptureId) {
return (Sculpture) entityManager.createNamedQuery("Sculpture.findBySculptureID").setParameter("sculptureID", sculptureId).getSingleResult();
}
{code}

Does anyone have any sugggestion?

Julien.

PS The pk of sculpturei18n is the composition of "sculptureid" and "locale"
[ July 10, 2008: Message edited by: Julien Martin ]
 
Charles Ohene
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Julien, do you found a solution inbetween, I am facing the same problem! Mine is even a bit more complex: Imagine I have an entity (Exposition) which holds many Skulptures and a Skulpture could be part of several expositions, thus an M:N relationship, and the Exposition entity also holds i18n texts for itself (e.g. genre description). I have no idea how to handle this,
any help is apreciated ;-)
Thanks, Charles.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest solution is to define a OneToMany from Sculpture to SculptureDescription. SculptureDescription will have a composite primary key of (sculpture_id, locale).
Your app can then get which descriptor matches its local.

If you know the local ahead of time, you could make a more complex solution, depending on your JPA provider. In EclipseLink you could define a DescriptorCustomizer that would define a OneToOneMapping in code based on a selectionCriteria Expression including the current local. You could potentially also define a multiple table descriptor as such in code, but this does get complicated, and the simpler solution is probably best.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic