0

ユーザーをデータベースに追加するための単純なSpring MVCプロジェクトがあります。私は hibernate 4.1.9、Spring Framework 3.2.0、および PostgreSQL 9.2 を使用しています。

以下の私のコードを参照してください

ユーザービーン

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();

    }        
}

ユーザーDAO

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

ユーザーサービス

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

root-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>

ユーザーコントローラー

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_

この問題を解決するにはどうすればよいですか?

4

2 に答える 2

3

セッションを開いてオブジェクトを保存したら、セッションをフラッシュしてから閉じます。

Session session = sessionFactory.openSession();
session.save(user);
session.flush();
session.close();
于 2013-02-14T15:15:55.870 に答える
1

の利点を得るHibernate Transaction

1つでtransaction複数の操作を実行でき、commit transactionすべての操作を正常に完了した後に1回実行できます。ここでの利点はrollback、で1つの操作が失敗した場合に、以前のすべての操作を実行できることです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();
    }
于 2013-02-15T09:23:27.887 に答える