Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate.MappingException persistent class not known

I have this exception :

org.hibernate.MappingException: persistent class not known: java.lang.Long

I'm using Hibernate 5.1.0.Final and spring-orm 4.2.4.RELEASE

My spring configuration file (spring.xml) :

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/ecollection" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>
<bean id="hibernateAnnotatedSessionFactory" 
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />

    <property name="annotatedClasses">
        <list>
            <value>com.ecollection.model.Adresse</value>
            <value>com.ecollection.model.Utilisateur</value>
        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>

</bean>
<bean id="utilisateurDao" class="com.ecollection.dao.UtilisateurDaoImpl">
    <property name="sessionFactory" ref="hibernateAnnotatedSessionFactory" />
</bean>
<bean id="adresseDao" class="com.ecollection.dao.AdresseDaoImpl">
    <property name="sessionFactory" ref="hibernateAnnotatedSessionFactory" />
</bean>

And I have two MySQL tables, User and Address.

User = Id, Name and Firstname
Address = Id, Street, City and UserId

for UserId in my Address entitybean, I'm doing this :

@OneToOne
@JoinColumn(name="id")
public Long getIdUtilisateur() {
    return idUtilisateur;
}
like image 839
Paladice Avatar asked Feb 24 '16 22:02

Paladice


People also ask

Why is my Hibernate mapping not working?

Missing or Invalid @Entity Annotation The most common cause for the mapping exception is simply an entity class missing the @Entity annotation: Another possibility is that it may have the wrong type of @Entity annotation: import org.hibernate.annotations.Entity; @Entity public class Foo implements Serializable { ...

What is mappingexception with spring hibernate?

MappingException With Spring The configuration of Hibernate in Spring involves bootstrapping the SessionFactory from annotation scanning, via a LocalSessionFactoryBean: This simple configuration of the Session Factory Bean is missing a key ingredient, and a test trying to use the SessionFactory will fail:

What is mappingexception in Java?

An exception that occurs while reading mapping sources (xml/annotations),usually as a result of something screwy in the O-R mappings. Constructs a MappingException using the given information. Fake signature of an existing Java class. Walk the nodes of the tree left-to-right or right-to-left.

How to configure hibernate in spring with SessionFactory?

The configuration of Hibernate in Spring involves bootstrapping the SessionFactory from annotation scanning, via a LocalSessionFactoryBean: This simple configuration of the Session Factory Bean is missing a key ingredient, and a test trying to use the SessionFactory will fail: ...


1 Answers

You can not use a non entity as a target for OneToOne or JoinColumn, as you did here:

@OneToOne
@JoinColumn(name="id")
public Long getIdUtilisateur() {
    return idUtilisateur;
}

Instead you can use this approach for mapping Utilisateur and Address relationship:

@OneToOne
@JoinColumn(name="id")
public Utilisateur getUtilisateur() {
    return utilisateur;
}

Of course, Utilisateur should be an entity:

@Entity
public class Utilisateur { ... }
like image 67
Ali Dehghani Avatar answered Oct 21 '22 11:10

Ali Dehghani