Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7数据库访问 [英] Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7 Database access

查看:75
本文介绍了Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7数据库访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将我们的项目从2.5.6春季版,hibernate 3.3.2,jboss 4.2升级到 Spring 3..0.5 + hierbnate 3.6.6.final + jboss升级为7 .

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.

那里有很多问题,我决定开始编写一个简单的项目,并在jboss as7(使用Spring 3..0.5 + hierbnate 3.6.6.final)上对其进行开发.该项目非常简单:DAO类将访问MYSQL数据库.但是,当应用程序在它的entityManager尝试访问数据库时失败,并抛出引发异常:org.hibernate.MappingException:未知实体".

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".

下面是我的项目:

我的Web控制器类:

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";
    }
}

我的DAO接口:

package com.yan.testing.jpa.dao;

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

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

在我的DAO课中:

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);
    }
}

这是问题所在: "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

似乎休眠状态无法识别我的实体.由于我的实体是使用Jboss工具生成的,因此在我的实体类中找不到任何错误.

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.

下面是我的实体类:

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;
    }

}

下面是我的应用程序和jboss的7种配置:

Below is my application and jboss as 7 configuration:

applicationContext.xml(基本上定义了EntityManagerFactory bean)

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:

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>

我的jboss confi standalone.xml:

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.

谢谢

安德鲁

推荐答案

jpa-persistence.xml是什么样的?

您需要列出要使用EntityManager映射的类带有<class>元素.

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

这篇关于Spring 3..0.5 + hierbnate 3.6.6.final + jboss as 7数据库访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆