加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Spring JPA休眠

发布时间:2020-12-15 01:22:41 所属栏目:大数据 来源:网络整理
导读:我是春天的新手.我正在尝试使用Spring 3.1.3和JPA 2.0设置简单的Web应用程序 我已将所有必需的库添加到WEB-INF / lib.启动过程中没有错误,但DaoImpl文件中的entityManager为空. 因此,这是我的配置: persistance.xml ?xml version="1.0" encoding="UTF-8"?pe

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

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类:

@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);
}
}

用户类别

@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;

????}

因此,当我尝试在UserController中调用addUser()时,在持久化过程中出现了NullPointerException.

最佳答案
我认为您缺少告诉Spring搜索注释的配置(在< beans>元素之间的任意位置添加以下内容:

<context:annotation-config />

也许您还需要添加以下代码,以指定DAO所在的软件包.但是我认为这不是必需的.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读