如何使用entitymanager修复persistence.xml和applicationContext.xml?

时间:2017-04-04 14:36:23

标签: java xml spring hibernate jpa

我使用hibernate,我想要基本的crud,但我没有配置persistence.xml和applicationContext.xml配置文件。我想使用EntityManager而不是Session。

这是我的applicationContext.xml

<context:annotation-config />

    <context:component-scan base-package="com.toyotaproject" />

    <tx:annotation-driven />
    <bean class = "org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

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



     <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <context:component-scan base-package="com.toyotaproject.domain" />
            <property name="persistenceXmlLocation" value="SpringJPA_PU"></property>
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter" ref="jpaAdapter" />
            <property name="loadTimeWeaver">
                <bean
                        class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
            </property>
        </bean>

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

        <bean id="jpaAdapter"
              class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
        </bean>

    </beans>

    this is my persistence.xml.

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
             version="1.0">

    <persistence-unit name="SpringJPA_PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>com.toyotaproject.domain.Customer </class>
        <class>com.toyotaproject.domain.Trip </class>
        <class>com.toyotaproject.domain.Reservation </class>
        <properties>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/toyotadb" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="12345" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.id.new_generator_mappings" value="true"/>

            <!-- If you are using Hibernate's proprietary API, you'll need the hibernate.cfg.xml.
             If you are using JPA i.e. Hibernate EntityManager, you'll need the persistence.xml. -->
            <!-- SQL -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name ="hibernate.format_sql" value="true"/>
            <property name ="hibernate.show_sql" value="true"/>
            <!-- C3P0 -->
            <property name="hibernate.c3p0.acquire_increment" value="2"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.timeout" value="180"/>
            <property name="hibernate.c3p0.idle_test_period" value="100"/>
            <property name="hbm2ddl.auto" value="update"/>

            <!-- property name="eclipselink.logging.level" value="OFF" /-->

        </properties>

    </persistence-unit>


</persistence>

我在哪里做错了?

0 个答案:

没有答案