如何将实体保存到数据库中?

时间:2013-02-14 15:11:03

标签: java hibernate spring-mvc

我有一个简单的spring mvc项目,用于将用户添加到数据库中。 我使用hibernate 4.1.9,spring framework 3.2.0和PostgreSQL 9.2。

请参阅下面的代码

用户bean

package org.vdzundza.beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq")
    @SequenceGenerator(name = "user_seq", sequenceName = "user_seq")
    @Column(name = "id")
    private Long id;

    @Column(name = "network")
    private String network;

    @Column(name = "photo")
    private String photo;

    @Column(name = "identity")
    private String identity;

    @Column(name = "firstname")
    private String firstName;

    @Column(name = "lastname")
    private String lastName;

    @Column(name = "hash")
    private String hash;

    @Column(name = "isonline")
    private Boolean isOnline;

    public User() {
    }
    //getters and setters

    @Override
    public String toString(){
        StringBuilder out = new StringBuilder();
        out.append("[");
        out.append("name: " + firstName + " " + lastName);
        out.append("\tnetwork: " + network);
        out.append("\t Is online: " + isOnline);
        return out.toString();

    }        
}

UserDAO的

package org.vdzundza.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.vdzundza.beans.User;

@Repository
public class UserDAOImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addUser(User user) {
        sessionFactory.openSession().save(user);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> listUser() {
        return sessionFactory.openSession().createCriteria(User.class).list();
    }
}

UserService

package org.vdzundza.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.vdzundza.beans.User;
import org.vdzundza.dao.UserDao;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDAO;

    @Transactional
    public void addUser(User user) {
        userDAO.addUser(user);
    }

    @Transactional
    public List<User> listUser() {
        return userDAO.listUser();
    }
}

根context.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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
    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-3.1.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
    <context:annotation-config />
    <context:component-scan base-package="org.vdzundza.dao" />
    <context:component-scan base-package="org.vdzundza.service" />

    <bean id="propertiesConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        lazy-init="false">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="packagesToScan" value="org.vdzundza.beans" />
    </bean>

    <bean id="transactionalManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

UserController中

package org.vdzundza.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.vdzundza.beans.User;
import org.vdzundza.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(){
        return "redirect:/user/index";
    }

    @RequestMapping("/index")
    public String listUsers(Map<String, Object> map){
        map.put("user", new User());
        map.put("userList", userService.listUser());
        return "user";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addUser(@ModelAttribute("user") User user, BindingResult result){
        userService.addUser(user);
        return "redirect:/user/index";
    }
}

user.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>user test spring and hibernate</title>
</head>
<body>
    <a href="${pageContext.request.contextPath}/">Go back</a>
    <br />

    <form:form method="post"
        action="${pageContext.request.contextPath}/user/add"
        commandName="user">
        <table>
            <tr>
                <td><form:label path="firstName">First Name</form:label></td>
                <td><form:input path="firstName" /></td>
            </tr>

            <tr>
                <td><form:label path="lastName">Last Name</form:label></td>
                <td><form:input path="lastName" /></td>
            </tr>

            <tr>
                <td><form:label path="network">network</form:label></td>
                <td><form:input path="network" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="add user" /></td>
            </tr>

        </table>
    </form:form>


    <c:if test="${empty userList}">
        <h2>user list is empty</h2>
    </c:if>

    <c:if test="${!empty userList}">
        <h2>user list</h2>
        <table>
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Network</th>
            </tr>

            <c:forEach items="${userList}" var="user">
                <tr>
                    <td>${user.firstName}</td>
                    <td>${user.lastName}</td>
                    <td>${user.network}</td>
                </tr>
            </c:forEach>
        </table>
    </c:if>
</body>
</html>

新用户未保存在数据库中。 当我点击“添加用户”时,我在控制台中看到了这一点:

Hibernate: select nextval ('user_seq')
Hibernate: select this_.id as id0_0_, this_.firstname as firstname0_0_, this_.hash as hash0_0_, this_.identity as identity0_0_, this_.isonline as isonline0_0_, this_.lastname as lastname0_0_, this_.network as network0_0_, this_.photo as photo0_0_ from users this_

如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

打开会话并保存对象后...刷新会话,然后关闭它。

Session session = sessionFactory.openSession();
session.save(user);
session.flush();
session.close();

答案 1 :(得分:2)

获得Hibernate Transaction的优势:

在一个transaction内,您可以执行多项操作,并且可以在成功完成所有操作后commit transaction执行一次操作。这里的优点是,如果rollback中的一个操作失败,您可以operation batch以前的所有操作。

  Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();


        session.save(user);


        tx.commit(); // Flush happens automatically
    }
    catch (RuntimeException e) {
        tx.rollback();
        throw e; // or display error message
    }
    finally {
        session.close();
    }