Clé composite : souci mapping
Hello,
Je débute sur Hibernate, et le projet a un schéma de base complexe :roll:
Env.
Base Mysql
Développement sous Eclipse avec Spring, Hibernate
J'ai l'erreur suivante : FATAL (ThesaurusTreeDaoImpl.java:64) Erreur lors du chargement du site 1
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: ThesaurusTree is not mapped [SELECT tt FROM ThesaurusTree tt WHERE tt.id.site.id = :siteId]
Auriez vous une petite idée de la cause ?? Merci par avance.
1°) Définition du modèle
Classe pour la clé composite
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
package com.leguide.thesaurus.tree.model;
...
@Embeddable
public class ThesaurusTreePK implements Serializable{
private static final long serialVersionUID = 179022443765548126L;
private Integer idThesaurus;
private Site site;
@Column(name = "id_thesaurus", nullable = false)
public Integer getIdThesaurus() {
return idThesaurus;
}
public void setIdThesaurus(Integer idThesaurus) {
this.idThesaurus = idThesaurus;
}
@ManyToOne(targetEntity=Site.class)
@JoinColumn(name = "id_site")
public Site getSite() {
return site;
}
public void setSite(Site site) {
this.site = site;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if ( !(obj instanceof ThesaurusTreePK) ) return false;
final ThesaurusTreePK other = (ThesaurusTreePK) obj;
if ( !other.getIdThesaurus().equals( getIdThesaurus() ) ) return false;
if ( !other.getSite().equals( getSite() ) ) return false;
return true;
}
@Override
public int hashCode() {
return super.hashCode();
}
} |
Classe permettant le mapping de la table
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
package com.leguide.thesaurus.tree.model;
import java.io.Serializable;
...
@Entity
@Table(name = "lgintegration.tb_thesaurus_tree")
public class ThesaurusTree implements Serializable{
private static final long serialVersionUID = 2839355101257449961L;
@EmbeddedId
private ThesaurusTreePK id;
private Short left_interval;
private Short right_interval;
...
@Id
@AttributeOverrides( {
@AttributeOverride(name="idThesaurus",
column=@Column(name="id_thesaurus") ),
@AttributeOverride(name="site",
column=@Column(name="id_site"))
})
public ThesaurusTreePK getThesaurusTreePK() {
return this.id;
}
public void setThesaurusTreePK(ThesaurusTreePK thesaurusTreePK) {
this.id = thesaurusTreePK;
}
@Column(name = "tht_left_interval")
public Short getLeft_interval() {
return left_interval;
}
public void setLeft_interval(Short leftInterval) {
left_interval = leftInterval;
}
@Column(name = "tht_right_interval")
public Short getRight_interval() {
return right_interval;
}
public void setRight_interval(Short rightInterval) {
right_interval = rightInterval;
}
...
} |
Classe "Site" utilisé dans la classe de définition de la clé composite
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
package com.leguide.thesaurus.tree.model;
...
@Entity
@Table(name = "lgintegration.tb_site")
public class Site implements Serializable{
private static final long serialVersionUID = 6685004289144296548L;
private Integer id;
private String name;
private String url;
private String locale;
private Boolean partner;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_site", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "tsi_name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "tsi_url")
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Column(name = "tsi_locale")
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
@Column(name = "tsi_is_partner")
public Boolean getPartner() {
return partner;
}
public void setPartner(Boolean partner) {
this.partner = partner;
}
} |
2°) Utilisation
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package com.leguide.thesaurus.tree.dao.impl;
...
@Repository("thesaurusTreeDao")
public class ThesaurusTreeDaoImpl implements ThesaurusTreeDao {
private static final Log logger = LogFactory.getLog(TreeDaoImpl.class);
@PersistenceContext(type=PersistenceContextType.EXTENDED,unitName="tree")
private EntityManager entityManager;
@SuppressWarnings("unchecked")
public List<ThesaurusTree> getThesaurusTree(Integer siteId) throws ThesaurusTreeException{
try {
Query treeQuery = entityManager.createQuery("SELECT tt FROM ThesaurusTree tt WHERE tt.id.site.id = :siteId");
treeQuery.setParameter("siteId", siteId);
List<ThesaurusTree> treeList = treeQuery.getResultList();
if ( treeList.size() == 0 ) {
logger.warn("Le site "+siteId+" n'existe pas.");
throw new ThesaurusTreeException();
}
return treeList;
} catch (Throwable e) {
logger.fatal("Erreur lors du chargement du site "+siteId+" ",e);
throw new ThesaurusTreeException(e);
}
}
} |
3°) Service exposé
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
package com.leguide.thesaurus.tree.services.impl;
@Service("thesaurusTreeServices")
public class ThesaurusTreeServiceImpl implements ThesaurusTreeServices{
@Autowired
protected ThesaurusTreeDao thesaurusTreeDao;
public List<Site> getSiteList() throws ThesaurusTreeException {
return this.thesaurusTreeDao.getSiteList();
}
public List<ThesaurusTree> getThesaurusTree(Integer siteId) throws ThesaurusTreeException{
return this.thesaurusTreeDao.getThesaurusTree(siteId);
}
} |
4°) Fichier de test
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
package com.leguide.thesaurus.tree.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
..
import com.leguide.thesaurus.tree.services.ThesaurusTreeServices;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "/leguideThesaurusTreeContext.xml")
public class ThesaurusTreeDaoTest {
@Autowired
public ThesaurusTreeServices thesaurusTreeServices;
@Test
public void getSiteList(){
int nb = 0;
int nb2 = 0;
Site elem;
Thesaurus elTh;
try
{
/*
List<Site> laListe = thesaurusTreeServices.getSiteList();
nb = laListe.size();
System.out.println("NB site(s) : "+ nb + "\n");
Iterator i = laListe.iterator();
while(i.hasNext()){
elem = (Site)i.next();
System.out.println( elem.getId()+ " ->" + elem.getName() + "|" + elem.getUrl()+ "|" + elem.getPartner());
}
*/
List<ThesaurusTree> thesTreeListe = thesaurusTreeServices.getThesaurusTree(1);
nb2 = thesTreeListe.size();
System.out.println("NB element(s) : "+ nb2 + "\n");
Iterator j = thesTreeListe.iterator();
while(j.hasNext()){
elTh = (Thesaurus)j.next();
System.out.println( elTh.getId()+ " ->" + elTh.getComment() + "(" + elTh.getCreatDate()+ "," + elTh.getUpdDate()+ ")");
}
}
catch (Throwable e) {}
}
} |
Annexe
Fichier persistence_tree.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="tree" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="xxx" />
<property name="hibernate.connection.password" value="yyy" />
<property name="hibernate.connection.url" value="jdbc:mysql://host/schema" />
<property name="hibernate.dialect" value="org.hibernate.dialect.InformixDialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.showsql" value="true"/>
<property name="current_session_context_class" value="thread"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
</properties>
</persistence-unit>
</persistence> |
Fichier de conf.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.leguide.thesaurus" />
<tx:annotation-driven transaction-manager="transactionManagerTree"/>
<bean id="entityManagerFactoryTree" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="tree"/>
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="generateDdl" value="false" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<entry key="hibernate.cache.use_query_cache" value="false"/>
<entry key="hibernate.cache.use_second_level_cache" value="false"/>
<entry key="hibernate.archive.autodetection" value="class, hbm" />
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</map>
</property>
</bean>
<bean id="transactionManagerTree" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryTree"/>
</bean>
</beans> |
[RESOLU] Clé composite : souci mapping
Arff, il suffisait que je rajoute dans persistence_tree.xml
<class>com.leguide.thesaurus.tree.model.Site</class>
<class>com.leguide.thesaurus.tree.model.Thesaurus</class>
<class>com.leguide.thesaurus.tree.model.ThesaurusTree</class>
Dans JUnit Test, il ne prenait pas en compte les classes ci-dessus.
:cry: