如何使用Spring 3配置JPA? [英] How to get JPA configured with Spring 3?

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

问题描述

我一直在阅读spring的文档,但我必须说它有点令人困惑,给出了有关如何配置JPA的几种不同选项。



什么是最好的方法,以及如何获得JPA 2(与休眠)配置春天3?如果你有一个pom,web.xml,applicationContext.xml和任何所需的java代码的例子,请你也可以发布它。

谢谢!

解决方案

我使用EclipseLink,但配置必须非常相似。这里你有最重要的部分。

pom.xml:

  <依赖性> 
< groupId> org.springframework< / groupId>
< artifactId> spring-orm< / artifactId>
< version> $ {org.springframework-version}< / version>
< /依赖关系>
< dependency>
< groupId> org.eclipse.persistence< / groupId>
< artifactId> eclipselink< / artifactId>
< version> 2.0.1< / version>
< /依赖关系>
< dependency>
< groupId> javax.persistence< / groupId>
< artifactId> javax.persistence< / artifactId>
< version> 2.0.0< / version>
< /依赖关系>

persistence.xml:

 <?xml version =1.0encoding =UTF-8?> 
< persistence xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns /持久性
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
version =2.0xmlns =http://java.sun.com/xml/ NS /持久性>

< persistence-unit name =persistenceUnittransaction-type =RESOURCE_LOCAL/>

< /持久性>

applicationContext-dao.xml:

 < bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean> 
< property name =persistenceXmlLocationvalue =classpath:persistence.xml/>
< property name =persistenceUnitNamevalue =persistenceUnit/>
< property name =dataSourceref =dataSource/>
< property name =jpaPropertyMap>
<道具>
< prop key =eclipselink.weaving> false< / prop>
< /道具>
< / property>
< / bean>

< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>

User.java:

  @Entity 
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;

私人字符串名称;

// Getters and setters

}

UserDao.java:

  @Repository 
public class JpaUserDao implements UserDao {

@ PersistenceContext
private EntityManager em;

@Override
public Item get(Integer id){
return em.find(User.class,id);


$ / code $ / pre
$ b $ UserService.java:

  @Service 
public class UserServiceImpl implements UserService {
$ b $ @Autowired
private UserDao userDao;

@Transactional
@Override
public User getUser(Integer id){
return userDao.get(id);
}

}

希望它有帮助。


I have been reading spring's documentation, but I must say it is a bit confusing, giving several different option on how to configure JPA.

What is the best way, and how, to get JPA 2 (with hibernate) configured with spring 3? If you have an example of pom, web.xml, applicationContext.xml, and any needed java code, could you please kindly post it as well.

thanks!

解决方案

I use EclipseLink, but configuration must be very similar. Here you have most important parts.

pom.xml:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.0.1</version> 
    </dependency>
    <dependency> 
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
    </dependency>

persistence.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    <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"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL" />

</persistence>

applicationContext-dao.xml:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
    <property name="persistenceUnitName" value="persistenceUnit" /> 
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaPropertyMap">
        <props>
            <prop key="eclipselink.weaving">false</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

User.java:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;

    private String name;

    // Getters and setters

}

UserDao.java:

@Repository
public class JpaUserDao implements UserDao {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Item get(Integer id) {
        return em.find(User.class, id);
    }
}

UserService.java:

@Service 
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    @Override
    public User getUser(Integer id) {
        return userDao.get(id);
    }

}

Hope it helps.

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

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