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 8528053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:42:59+00:00 2026-06-11T08:42:59+00:00

I have a problem to persist an object into my database. I have a

  • 0

I have a problem to persist an object into my database.
I have a User with a many-to-one relation with a Profil.

My JSP just add a new user. So, the JSP contains fields and a comboxBox with the profil wanted. The problem is, when I run the program, even if I selected a profil in the comboxBox, the property user.profil is NULL in the controller.

This is the User object:

package com.app.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class User {

    private long id;
    private String firstname;
    private String lastname;
    private String login;
    private String password;
    private Profil profil;

    public User() {
    }

    /**
     * @param firstname
     * @param lastname
     * @param login
     * @param password
     * @param profil
     */
    public User(String firstname, String lastname, String login,
            String password, Profil profil) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.login = login;
        this.password = password;
        this.profil = profil;
    }

    /**
     * Get User Id
     * 
     * @return long - User Id
     */
    @Id
    @Column(name="id", unique = true, nullable = false)
    public long getId() {
        return id;
    }

    /**
     * Set User Id
     * 
     * @param long - User Id
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * Get User Firstname
     * 
     * @return String - User Firstname
     */
    @Column(name="firstname", unique = false, nullable = false)
    public String getFirstname() {
        return firstname;
    }

    /**
     * Set User Firstname
     * 
     * @param String - User Firstname
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * Get User Lastname
     * 
     * @return String - User Lastname
     */
    @Column(name="lastname", unique = false, nullable = false)
    public String getLastname() {
        return lastname;
    }

    /**
     * Set User Lastname
     * 
     * @param String - User Lastname
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    /**
     * @return the login
     */
    @Column(name="login", unique = true, nullable = false)
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * @return the password
     */
    @Column(name="password", unique = false, nullable = false)
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the profil
     */
    @ManyToOne( cascade = CascadeType.REFRESH, fetch = FetchType.EAGER )
    @JoinColumn( name = "fk_profil_id", nullable = false )
    public Profil getProfil() {
        return profil;
    }

    /**
     * @param profil the profil to set
     */
    public void setProfil(Profil profil) {
        this.profil = profil;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "User [id=" + id + ", firstname=" + firstname + ", lastname="
                + lastname + ", login=" + login + ", password=" + password
                + "]";
    }
}

This is the Profil object:

package com.app.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="profil")
public class Profil {

    private long id;
    private String name;

    public Profil() {
    }

    /**
     * @param id
     * @param name
     */
    public Profil(long id, String name) {
        this.id = id;
        this.name = name;
    }   

    /**
     * @return the id
     */
    @Id
    @Column(name="id", unique = true, nullable = false)
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    @Column(name="name", unique = true, nullable = false)
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Profil [id=" + id + "]";
    }
}

This is the Controller:

package com.app.web;

import java.util.HashMap;
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.springframework.web.servlet.ModelAndView;

import com.app.model.User;
import com.app.service.impl.ProfilService;
import com.app.service.impl.UserService;

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

    @Autowired
    private UserService userService;

    @Autowired
    private ProfilService profilService;

    @ModelAttribute("userForm")
    public User createForm()
    {
        return new User();
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveUser ( @ModelAttribute("userForm") User user, BindingResult result ) {

        userService.addUser( user );

        return new ModelAndView ( "redirect:/users.html" );
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView viewUsers() {
        Map model = new HashMap();
        model.put ( "users", userService.getUsers() );

        return new ModelAndView ( "usersView", model );
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addUser() {
        Map model = new HashMap();
        model.put ( "profils", profilService.getProfils() );

        return new ModelAndView ( "userAdd", model );
    }
}

This is the JSP page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add User</title>

</head>

<body>
<h1>Add User</h1>
<c:url var="viewUsersUrl" value="/users.html" />
<a href="${viewUsersUrl}">View Existing Users</a>

<br /><br />
<c:url var="saveUserUrl" value="/users/save.html" />
<form:form modelAttribute="userForm" method="POST" action="${saveUserUrl}">
    <form:label path="firstname">Firstname:</form:label>
    <form:input path="firstname"/><br />
    <form:label path="lastname">Lastname:</form:label>
    <form:input path="lastname"/><br />
    <form:label path="login">Login:</form:label>
    <form:input path="login"/><br />
    <form:label path="password">Password:</form:label>
    <form:input path="password"/><br />
    <form:select path="profil">  
        <form:option value="0" label="---- Select ----" />
        <form:options items="${profils}" itemValue="id" itemLabel="name" />   
    </form:select> 
    <input type="submit" value="Save User" />
</form:form>

</body>
</html>

The Exception:

SEVERE: Servlet.service() for servlet [spring] in context with path [/app] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Column 'fk_profil_id' cannot be null; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Column 'fk_profil_id' cannot be null] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'fk_profil_id' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
...
...
...

This exception occurs because user.profil is NULL in the controller.

If you need more information, feel free to ask.
And btw, if you notice something wrong or a better way to do, please tell me, I new to spring and spring-mvc.

Thanks a lot

  • 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-11T08:43:01+00:00Added an answer on June 11, 2026 at 8:43 am

    in your JSP Page combo box should have path as below

    <form:select path="profil.id">  
        <form:option value="0" label="---- Select ----" />
        <form:options items="${profils}" itemValue="id" itemLabel="name" />   
    </form:select>
    

    This will ask spring conversion to create new instance of profil and attach with User.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to persist my entity with ManyToMany relation. But i have some problem
I have a problem when I try to persist my model. An exception is
I have problem while loading data into html select when users press or click
I have a problem getting my change(s) to data object retrieved using NHibernate to
I'm using DDD and NHibernate to persist my domain object. In my database every
i have problem with load data from Database i use Primefaces (or other use
I am using EntityFramework to persist my entities. The problem I have is that
I have a problem, private void button_Submit_Click(object sender, EventArgs e) { try { string
I have a problem when I try to persist objects using multiple threads .
i'm trying to remove an item from a one to many list and have

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.