Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9289401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:10:09+00:00 2026-06-18T20:10:09+00:00

I have a simple spring mvc project for adding users into database. I use

  • 0

I have a simple spring mvc project for adding users into database.
I use hibernate 4.1.9, spring framework 3.2.0 and PostgreSQL 9.2.

See my code below

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

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>

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>

New user isn’t saved in database.
When I click “add user” I see this in console:

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_

How to fix this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:10:11+00:00Added an answer on June 18, 2026 at 8:10 pm

    After you open the session and save the object… flush the session, then close it.

    Session session = sessionFactory.openSession();
    session.save(user);
    session.flush();
    session.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Spring MVC application where I use hibernate, freemarker. It is setup
I have created simple HelloWorld project from Spring MVC template in Spring STS. I
Have a simple Spring MVC portlet, and from one of portal pages I have
I'm running into a Spring (3.1) mapping conundrum: I have a simple, findAll method:
I have an extremely simple web application running in Tomcat using Spring 3.0.2, Hibernate
I was trying to configure spring 3.1 and hibernate. I have written a simple
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I have a very simple HTML file (it's part of an MVC 4 project
I have a simple question about adding references to a .NET project. I'm adding
I have build a simple spring project and would like to configure JPA in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.