Reputation: 785
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
Upvotes: 0
Views: 3051
Reputation: 31
@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.
Upvotes: 3
Reputation: 2621
Also post the whole class. The error is basically saying that jpa cannot find your class.
Upvotes: 2
Reputation: 140051
What does jpa-persistence.xml
look like?
You need to list the classes you want to map with the EntityManager with a <class>
element.
Upvotes: 1