如何配置Spring,而不使用persistence.xml? [英] How to configure Spring without persistence.xml?

查看:1642
本文介绍了如何配置Spring,而不使用persistence.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置spring xml配置,而不必创建一个 persistence.xml 。但我不断得到以下异常,即使我包括数据库属性在 spring.xml

I'm trying to set up spring xml configuration without having to create a futher persistence.xml. But I'm constantly getting the following exception, even though I included the database properties in the spring.xml

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [C:\Users\me\workspace\app\src\main\webapp\WEB-INF\applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}

spring.xml:

spring.xml:

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
     <property name="jpaProperties">
         <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
         </props>
      </property>
    </bean>

我在这里缺少什么?

推荐答案

指定packagesToScan&

Specify the "packagesToScan" & "persistenceUnitName" properties in the entityManagerFactory bean definition.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />

        <property name="persistenceUnitName" value="myPersistenceUnit" />
        <property name="packagesToScan" >
            <list>
                <value>org.mypackage.*.model</value>
            </list>
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

请注意,这是Spring版本> 3.1

Note that this is for Spring version > 3.1

这篇关于如何配置Spring,而不使用persistence.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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