2011-06-17 59 views
0

我想获得以下配置。休眠不检索与JBOSS数据

弹簧,休眠时,JBoss 5.1中,MySQL 5.14

调度-servlet.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:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring- mvc-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> 

<context:annotation-config /> 
<context:component-scan base-package="au.com.kiche" /> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

<bean 
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> 
<bean 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
<bean 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

<!-- Most controllers will use the ControllerClassNameHandlerMapping above, 
    but for the index controller we are using ParameterizableViewController, 
    so we must define an explicit mapping for it. --> 

<bean id="jspViewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 

<!-- the following bean description should be moved to applicationContext. I'm leaving this decision to Chief. --> 
<jee:jndi-lookup id="dataSource" jndi-name="java:/MyDS"/> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="configLocation"> 
     <value>classpath:hibernate.cfg.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
</bean> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

的hibernate.cfg.xml是:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 
            "http://hibernate.sourceforge.net/hibernate-configuration- 3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.connection.datasource">java:/MyDS</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 
    <property name="hibernate.show_sql">true</property> 

    <mapping class="au.com.kiche.model.User"/> 
    </session-factory> 
</hibernate-configuration> 

数据源文件“kiche-ds.xml”是:

<datasources> 
<local-tx-datasource> 
    <jndi-name>MyDS</jndi-name> 
    <connection-url>jdbc:mysql://localhost:3306/kiche</connection-url> 

    <driver-class>com.mysql.jdbc.Driver</driver-class> 
    <user-name>root</user-name> 
    <password>root5</password> 

    <min-pool-size>5</min-pool-size> 
    <max-pool-size>100</max-pool-size> 

    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> 

    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml --> 
    <metadata> 
     <type-mapping>mySQL</type-mapping> 
    </metadata> 
</local-tx-datasource> 
</datasources> 

的followis是我的类,它是试图从数据库中获取所有用户:

@Repository 
public class UserDAOImpl implements UserDAO{ 

    @Autowired 
    SessionFactory sessionFactory; 

    @Override 
    public List<User> getUserByLogin() { 
      Criteria criteria=sessionFactory.getCurrentSession().createCriteria(User.class); 
     List<User> users=criteria.list(); 
     System.out.println("**** The size of the user is: "+users.size()); 
      return users; 
    } 
    . 
    . 
    . 
} 

奇怪的是,我能够得到的数据,形成数据库,当我使用Tomcat(不一个与JBOSS)。但是当我尝试在JBOSS中运行这个应用程序时,没有数据被回收,没有错误或异常。

任何帮助将高度支持。

问候 Adofo

+0

我不知道Jboss,但我想你可以在某种管理界面上定义一个数据源,在那里你将得到一个JNDI名称,你应该在Hibernate配置中使用它,并且你可能会丢失你的ds定义。 – abalogh 2011-06-17 09:44:15

回答

1

,您应该使用资源映射的Java EE的约定。对于您的情况:

应用程序上下文:

<jee:jndi-lookup id="dataSource" 
       jndi-name="MyDS" 
       lookup-on-startup="false" 
       proxy-interface="javax.sql.DataSource" 
       resource-ref="true" 
     /> 

WEB-INF/web.xml文件:

<resource-ref> 
    <res-ref-name>MyDS</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

WEB-INF /的jboss-web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE jboss-web PUBLIC 
     "-//JBoss//DTD Web Application 4.2//EN" 
     "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd"> 
<jboss-web> 
    <resource-ref> 
     <res-ref-name>MyDS</res-ref-name> 
     <jndi-name>java:MyDS</jndi-name> 
    </resource-ref> 
</jboss-web> 

这将适用于JBoss和纯Tomcat。

+0

嗨,谢谢你的回应,我会在一天左右给它一个。真的很感激它。 – Harbir 2011-06-22 10:30:18