Queqlues précisions sur mon mapping :
Catalogue :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.cylande.service.test.Catalogue" table="TCCTLP00">
<id name="uid" column="CATNAM" type="java.lang.String">
<generator class="assigned" />
</id>
<many-to-one name="libelle" column="CTLBID" class="com.cylande.service.test.Libelle" unique="true"/>
</class>
</hibernate-mapping>
Libelle :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.cylande.service.test.Libelle" table="TCLIBP00">
<composite-id>
<key-property name="uid" type="long" column="LIBID"/>
<key-property name="codeIso" type="string" column="ISOLG"/>
</composite-id>
<property name="lib" column="LIBEL"/>
</class>
</hibernate-mapping>
Classe Catalogue :
/*
* Créé le 17 févr. 2006
*/
package com.cylande.service.test;
import java.io.Serializable;
/**
* @author sberthod
*/
public class Catalogue implements Serializable{
private String uid=null;
private Libelle libelle =null;
public String getLib() {
if (libelle!=null)
return libelle.getLib();
else
return "libelle inconnu";
}
public String getUid() {
return uid;
}
public void setUid(String string) {
uid = string;
}
public Libelle getLibelle() {
return libelle;
}
public void setLibelle(Libelle libelle) {
this.libelle = libelle;
}
}
Classe Libelle :
/*
* Créé le 17 févr. 2006
*/
package com.cylande.service.test;
/**
* @author sberthod
*/
import java.io.Serializable;
public class Libelle implements Serializable{
private Integer uid;
private String lib;
private String codeIso;
public Libelle(){
uid = new Integer(0);
lib ="";
codeIso = "";
}
public String getCodeIso() {
return codeIso;
}
public String getLib() {
return lib;
}
public Integer getUid() {
return uid;
}
public void setCodeIso(String string) {
codeIso = string;
}
public void setLib(String string) {
lib = string;
}
public void setUid(Integer integer) {
uid = integer;
}
public boolean equals(Object arg0) {
return super.equals(arg0);
}
public int hashCode() {
return super.hashCode();
}
}
Programme Test :
public class Multilinguisme {
/**
* Méthode de test
*
* @param args
*/
public static void main(String[] args) {
Catalogue cat = null;
try {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Criteria cr = session.createCriteria(Catalogue.class).add(Expression.eq("uid", "RSF")).createCriteria("libelle").add(Expression.eq("codeIso", "FR"));
List lst = cr.list();
if (lst != null) {
cat = (Catalogue) lst.get(0);
}
tx.commit();
session.close();
if (cat != null) {
System.err.println("CAT : " + cat);
System.err.println(cat.getLib());
} else {
System.err.println("GRUMPPPH pas trouvé !!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Message d'erreur :
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.cylande.mapping.HibernateUtil.<clinit>(HibernateUtil.java:17)
at com.cylande.service.test.Multilinguisme.main(Multilinguisme.java:32)
Caused by: org.hibernate.MappingException: Foreign key (FK653C61A4359CFE5B:TCCTLP00 [CTLBID])) must have same number of columns as the referenced primary key (TCLIBP00 [LIBID,ISOLG])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1122)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1061)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1145)
at com.cylande.mapping.HibernateUtil.<clinit>(HibernateUtil.java:14)
... 1 more
strusme wrote:
J'ai le même problème que toi.
Comment géré le mutlilinguisme en base avec hibernate ?
Par exemple comment mapper mon objet catalogue pour obtenir le libellé de ce dernier dans la langue souhaitée ?
-- CATALOGUES (Table)
CREATE TABLE TCCTLP00
(
CATNAM VARCHAR2(10) not null,
CTLBID number(38) not null,
constraint TCCTNP00 primary key (CATNAM)
using index tablespace INDEX_TBS
);
comment on table TCCTLP00 is 'declaration des catalogue';
comment on column TCCTLP00.CATNAM is 'nom d un catalogue';
comment on column TCCTLP00.CTLBID is 'id du libelle long du catalogue';
-- LIBELLE (Table)
CREATE TABLE TCLIBP00
(
LIBID number(38) not null,
ISOLG varchar2(2) not null,
LIBEL varchar2(255) not null,
constraint TCLIBP00 primary key (LIBID, ISOLG)
using index tablespace INDEX_TBS
);
comment on table TCLIBP00 is 'table des libelles internationalises';
comment on column TCLIBP00.LIBID is 'id libelle';
comment on column TCLIBP00.ISOLG is 'code ISO de la langue du libelle';
comment on column TCLIBP00.LIBEL is 'texte du libelle ';