Domanda

A partire da ora ho una domanda di primavera lavoro con persistenza. Comunque ora voglio usare Hibernate con APP a fare tutte le mie attività di database. Io voglio fare questo utilizzando un EntityManager.

Ho letto molti documenti e tutorial su questo argomento, ho sempre confuso se ho bisogno di un file persistence.xml o meno. Anche Sono stato sempre confuso su come impostare il mio file applicationContext.xml pure.

Qualcuno sa di un buon sito da guardare per imparare primavera + Hibernate JPA + + utilizzando EntityManager?

È stato utile?

Soluzione

Ho appena trascorso l'ultimo paio di settimane cercando di creare lo stesso tipo di progetto.

Avete bisogno di un file persistence.xml, e appartiene a META-INF

Ecco un esempio del mio file fagioli di primavera per la persistenza:

<beans  xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="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">

<context:property-placeholder location="/WEB-INF/config.properties" />

    <tx:annotation-driven />

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

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

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

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="whatisayis" />
    <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="leDAO" class="com.noisyair.whatisayis.dao.jpa.JpaLearningEntryDAO">
    <property name="jpaTemplate" ref="jpaTemplate" />
</bean> 
<bean id="sampleDAO" class="com.noisyair.whatisayis.dao.jpa.JpaSampleDAO">
    <property name="jpaTemplate" ref="jpaTemplate" />
</bean>
    <bean id="tagDAO" class="com.noisyair.whatisayis.dao.jpa.JpaTagDAO">
    <property name="jpaTemplate" ref="jpaTemplate" />
</bean>
</beans>

Inoltre, sto usando Maven per tirare in spring3 e ibernazione dipendenze ho bisogno.

modifica: per una risorsa di apprendimento mi raccomando "Primavera Ricette Un approccio Problem-Solution" di Gary Mac http://www.apress.com/book/view/9781590599792 . Questo è uno dei migliori libri tecnici che abbia mai letto, e sarà sicuramente aiuterà a ottenere installato e funzionante con la primavera / JPA / Hibernate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top