2013-05-21 35 views
0

我使用JPA 2.0,Hibernate 3的註釋版本,問題是,我在persistence.xml中&我困惑的DispatcherServletPersistance.xml VS DispatcherServlet的

我的persistence.xml文件包含:

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

<persistence version="2.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_2_0.xsd"> 

    <persistence-unit name="Hello" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <class>models.student</class> 

     <properties> 
      <property name="hibernate.show_sql" value="true" /> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/test"/> 
      <property name="javax.persistence.jdbc.user" value="root" /> 
      <property name="javax.persistence.jdbc.password" value="admin" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> 
     </properties> 

    </persistence-unit> 

</persistence> 

在我的分發程序Servlet文件我有以下豆類:

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/test" /> 
    <property name="username" value="root" /> 
    <property name="password" value="admin" /> 
</bean> 


<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="Hello" /> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
      <property name="showSql" value="true" /> 
      <property name="generateDdl" value="true" /> 
     </bean> 
    </property> 
</bean> 



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

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

的問題是:爲什麼我們需要把所有屬性(DriverClassName,網址,用戶名,通字)在Persistence.xml和DispatcherServlet中的DataSource bean中?

我在學習,這讓我很困惑,請幫忙。

回答

0

其實你在這裏有雙重配置。

您需要persistence.xml或bean定義。

看一看here

相關問題