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

标签 java hibernate spring-mvc

我有一个简单的 spring mvc 项目,用于将用户添加到数据库中。 我使用 hibernate 4.1.9、spring 框架 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();
    }
}

用户服务

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>

用户 Controller

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_

如何解决这个问题?

最佳答案

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

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

关于java - 如何将实体保存到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14877897/

相关文章:

hibernate - 在通用 Java 编程中使用 Quarkus Panache 实体时出错

java - 无法统计应用程序。将 SpringBoot 从 2.0.6.RELEASE 更新到 2.1.0.RELEASE 后

java - 使用 Spring 时为 Controller 使用不同的包

java - JsonMappingException : lazily initialize a collection without using @Jsonignore的解决方法

mysql - 如何在Mysql中使用用户定义的@variable作为线程安全

java - 为什么 JPA 实体在 rowKey 属性为主键时抛出异常?

java - Spring 正常关闭 - 请求方法不支持

java - EclipseLink - 带条件查询的子查询 (JPA)

java - 从 Spring 2.5 迁移到 Spring 3.0.5

java - 使用 java 代码从 GIT url 检索分支