0

I'm upgrading our projects from spring 2.5.6, hibernate 3.3.2, jboss 4.2 to Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7.

Lots of issues there and I decided to write a simple project and delopy it on jboss as7(with Spring 3..0.5 + hierbnate 3.6.6.final) at the begining. The project is very simple: A DAO class will access the MYSQL database. But the application fails when it's entityManager try to access the DB, throwing "threw exception: org.hibernate.MappingException: Unknown entity".

Below is my project:

My web controller class:

package com.yan.testing.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yan.testing.jpa.dao.IYanDao;
import com.yan.testing.jpa.entity.Yan;

@Controller
public class WebController {

    @Autowired
    IYanDao yanDao;

    @RequestMapping("sayHello.do")
    public String SayHello(){
        Yan yan = yanDao.findById(1L);
        System.out.println(yan.getName());
        return "sayHello";
    }
}

My DAO inferface:

package com.yan.testing.jpa.dao;

import com.yan.testing.jpa.entity.Yan;

public interface IYanDao {
    Yan findById(Long id);
}

And In my DAO class :

package com.yan.testing.jpa.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.yan.testing.jpa.dao.IYanDao;
import com.yan.testing.jpa.entity.Yan;

@Repository(value="yanDao")
public class YanDao implements IYanDao{

    private EntityManager entityManager;
    @PersistenceContext 
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public Yan findById(Long id){
        return entityManager.find(Yan.class, id);
    }
}

Here comes the problem: "entityManager.find(Yan.class, id);" throw exception:

Servlet.service() for servlet spring threw exception: org.hibernate.MappingException: Unknown entity: com.yan.testing.jpa.entity.Yan

It seems like hibernate don't recognize my entity. As my entity is generated using Jboss tools, I can't find any error in my entity class.

Below is my entity class:

package com.yan.testing.jpa.entity;

// default package
// Generated Jul 27, 2011 4:15:52 PM by Hibernate Tools 3.4.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Yan generated by hbm2java
 */

@Entity
@Table(name = "yan", catalog = "fuhu_app_submission")
public class Yan implements java.io.Serializable {

    private static final long serialVersionUID = -6812001362936479032L;
    private Integer objId;
    private String name;

    public Yan() {
    }

    public Yan(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "obj_id", unique = true, nullable = false)
    public Integer getObjId() {
        return this.objId;
    }

    public void setObjId(Integer objId) {
        this.objId = objId;
    }

    @Column(name = "name", length = 45)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Below is my application and jboss as 7 configuration:

applicationContext.xml(which basically define a entityManagerFactory bean)

<context:component-scan base-package="com.yan.testing"/> 
<context:annotation-config/>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:jboss/datasources/MySqlDS" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml"/>   
    <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
            </bean>
    </property>
</bean>    
<tx:annotation-driven />

jpa-persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="app_sub_jpa">
        <description>Hibernate for JPA</description>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

My jboss confi standalone.xml:

<subsystem xmlns="urn:jboss:domain:datasources:1.0">
            <datasources>
                <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" jta="true" use-java-context="true" use-ccm="true">
                    <connection-url>
                        jdbc:mysql://127.0.0.1:3306/my_schema
                    </connection-url>
                    <driver>
                        com.mysql
                    </driver>
                    <transaction-isolation>
                        TRANSACTION_READ_COMMITTED
                    </transaction-isolation>
                    <security>
                        <user-name>
                            root
                        </user-name>
                        <password>
                            root
                        </password>
                    </security>
                    <statement>
                        <prepared-statement-cache-size>
                            32
                        </prepared-statement-cache-size>
                    </statement>
                </datasource>
                <drivers>
                    <driver name="com.mysql" module="com.mysql">
                        <xa-datasource-class>
                            com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
                        </xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>
        </subsystem>

Any help or hints are appreciated.

Thank you

Andrew

yzandrew
  • 785
  • 1
  • 11
  • 25
  • I add more details here, so that the original post won't be too long to read. 1. Jboss AS 7 have hibernate 4 as its jpa module. I have add "jboss-deployment-structure.xml" in my project to exclude hibernate 4, as I am using hibernate 3.6.6.final – yzandrew Jul 28 '11 at 01:15
  • 2. I have edit the datasource in standalone.xml and bind it to my EntityManagerFactory by jndi lookup. 3. I can see that the entityManager is initialized when I am debugging. – yzandrew Jul 28 '11 at 01:19

3 Answers3

3

@yzandrew: It requires a property 'packagesToScan' for entity manager factory bean if you don't want to add each entity class in persistence.xml.

<property name="packagesToScan" value="path/to/package"/>

Actually you don't even need persistence.xml but it depends if you want to go that route.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Parvez
  • 31
  • 1
2

Also post the whole class. The error is basically saying that jpa cannot find your class.

chrislovecnm
  • 2,549
  • 3
  • 20
  • 36
  • I have 4 class. web controller(which call the dao class method), DAO interface, DAO impl, and entity. I will post them all int the original post – yzandrew Jul 28 '11 at 01:27
1

What does jpa-persistence.xml look like?

You need to list the classes you want to map with the EntityManager with a <class> element.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • The jpa-persistence.xml file doesn't do much thing but just specify the "hibernate.dialect" property. – yzandrew Jul 28 '11 at 01:21
  • But I didnot have element in my previous project and the project works fine. – yzandrew Jul 28 '11 at 01:24
  • cool. It seems like I have to add `com.yan.testing.jpa.entity.Yan` into my jpa-persistence.xml. But it's strange that In my previous project, it's not necessary. – yzandrew Jul 28 '11 at 01:48
  • I can't say there are the same configuration, as there are in different env. But basically, yes. They are basically the same. I do list `` element in my old project in the very beginning. and then they are removed as annotating the entity class with `@entity` will let JPA know's it's an entity. – yzandrew Jul 28 '11 at 01:50
  • The presence of `@Entity` in a class file on it's own does not let JPA know about the entity. The EntityManager (and/or Hibernate) still needs to be told which classes you are interested in mapping. – matt b Jul 28 '11 at 01:56
  • So strange. Another project using jpa 1.1 with hibernate dont need `` in persistence.xml can work. I guess it's the difference between jpa 2.0 and jpa 1.1? or it's the difference between spring 3.0.5 and spring 2.5.6(or org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter)? – yzandrew Jul 28 '11 at 02:22