Spring + JPA + Hibernate [英] Spring + JPA + Hibernate

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

问题描述

我在春天是新手。我试图用Spring 3.1.3和JPA 2.0
设置简单的Web应用程序我已经将所有必需的库添加到WEB-INF / lib。启动期间没有错误,但DaoImpl文件中的entityManager为空。
因此,这是我的配置:

persistance.xml

 <?xml version =1.0encoding =UTF-8?> 
< persistence version =2.0
xmlns =http://java.sun.com/xml/ns/persistencexmlns: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 =fcmsServertransaction-type =RESOURCE_LOCAL>
< / persistence-unit>
< /余辉>

fcms-servlet.xml

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :tx =http://www.springframework.org/schema/tx
xsi:schemaLocation =http://www.springframework.org/schema/beans
http:// www。 springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring- context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd\">

<! - fcmsServer DispatcherServlet的应用程序上下文定义 - >

< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =fcmsServer/>
< property name =dataSourceref =dataSource/>
< property name =jpaDialect>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaDialect/>
< / property>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =showSqlvalue =true/>
< property name =generateDdlvalue =true/>
< property name =databasePlatformvalue =org.hibernate.dialect.H2Dialect/>
< / bean>
< / property>
< property name =persistenceUnitManager>
< bean
class =org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager>
< property name =defaultDataSourceref =dataSource/>
< / bean>
< / property>
< / bean>

< bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSource
destroy-method =close>
< property name =driverClassNamevalue =org.h2.Driver/>
< property name =urlvalue =jdbc:h2:tcp:// localhost /〜/ fcms/>
< property name =usernamevalue =sa/>
< property name =passwordvalue =sa/>
< / bean>

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

< bean id =persistenceAnnotation
class =org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor/>


< bean id =userDaoclass =fcms.data.user.UserDAOImpl>
< / bean>

< bean name =/ user.htmclass =fcms.controller.UserController>
< / bean>

< bean id =viewResolver
class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =viewClass
value =org.springframework.web.servlet.view.JstlView>< / property>
< property name =prefixvalue =/ WEB-INF / jsp />< / property>
< property name =suffixvalue =。jsp>< / property>
< / bean>



DaoImpl class:

  @Repository 
public class UserDAOImpl implements UserDAO {
@PersistenceContext(unitName =fcmsServer)
私人EntityManager entityManager;

public void setEntityManager(EntityManager entityManager){
this.entityManager = entityManager;
}

@Override
public User getUserById(long id){
return entityManager.find(User.class,id);
}

@Override
public void addUser(User u){
entityManager.persist(u);


$ / code $ / pre
$ b $用户等级

  @Entity 
@Table(name =User)
public class User实现Serializable {


@Id @GeneratedValue
私人长ID;
@Column(name =lastName,nullable = false)
private String lastName;
@Column(name =firstName,nullable = false)
private String firstName;
@Column(name =birthDate,nullable = true)
私人日期birthDate;
private static final long serialVersionUID = 1L;




}



当我尝试在UserController中调用addUser()时,在持久化期间我有NullPointerException。

解决方案

我认为你错过了配置以告诉spring搜索注释(在< beans> 元素:

 < context:annotation-config /> ; 

也许您还需要添加以下指定包含DAO的包,但是我

 < context:component-scan base-package =your.package/> ; 


I am newbie in Spring. I am trying to setup simple web application with Spring 3.1.3 and JPA 2.0 I have added all necessary libs to the WEB-INF/lib. There is no errors during the startup but entityManager in my DaoImpl file is null. So, this is my configuration:

persistance.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="fcmsServer" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>

fcms-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- the application context definition for the fcmsServer DispatcherServlet -->

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="fcmsServer" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
        </bean>
    </property>
    <property name="persistenceUnitManager">
        <bean
            class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
            <property name="defaultDataSource" ref="dataSource" />
        </bean>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:tcp://localhost/~/fcms" />
    <property name="username" value="sa" />
    <property name="password" value="sa" />
</bean>

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

<bean id="persistenceAnnotation"
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


<bean id="userDao" class="fcms.data.user.UserDAOImpl">
</bean>

<bean name="/user.htm" class="fcms.controller.UserController">
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView"></property>
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

DaoImpl class:

@Repository
public class UserDAOImpl implements UserDAO {
@PersistenceContext(unitName = "fcmsServer")
private EntityManager entityManager;

public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

@Override
public User getUserById(long id) {
    return entityManager.find(User.class,id);
}

@Override
public void addUser(User u) {
    entityManager.persist(u);
}
}

User class

@Entity
@Table(name = "User")
public class User implements Serializable {


@Id  @GeneratedValue
private long id;
@Column(name = "lastName", nullable = false)
private String lastName;
@Column(name = "firstName", nullable = false)
private String firstName;
@Column(name = "birthDate", nullable = true)
private Date birthDate;
private static final long serialVersionUID = 1L;

}

So when I try to call addUser() in UserController, I have NullPointerException during persist.

解决方案

I think you're missing the configuration to tell spring to search for the annotations (add the following anywhere between the <beans> element:

<context:annotation-config />

And maybe you also need to add the following specifying the package where you have the DAO. But I don't think this is required.

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

这篇关于Spring + JPA + Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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