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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:38:13+00:00 2026-06-09T19:38:13+00:00

I have a question to ask: I have to tables user and user_login joined

  • 0

I have a question to ask:

I have to tables user and user_login joined OneToOne by user.id -> user_login.user_id.

The issue is when I do .updateObject(user) I get 2 queries executed:

Hibernate: insert into User (created, modified, email, first_name,
last_name) values (?, ?, ?, ?, ?) Hibernate: insert into user_login
(created, modified, password, user_id) values (?, ?, ?, ?) [2012-08-15
12:15:04,192] [ERROR] [http-bio-8080-exec-1] SqlExceptionHelper [144]:
Column ‘user_id’ cannot be null

and looks like there is no reference between 2 objects. If into the Entity User, method setUserLogin I add line
userLogin.setUser(this); its working but I dont find this way elegant honestly. Is there anything I missed in entity configuration
maybe that does not do that automatically ?

Thank you

Here are my Entities

@Entity
@NamedQueries({ @NamedQuery(name = "user.list", query = "select u from User u") })
public class User implements java.io.Serializable {

    @Column(name = "first_name", nullable = true)
    private String firstName;   

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="user_id", nullable=false)
    private UserLogin userLogin;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public UserLogin getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(UserLogin userLogin) {
        this.userLogin = userLogin;
        //userLogin.setUser(this); THIS IS THE LINE THAT FIXES IT, BUT I DONT FIND THIS WAY ELEGANT
    }

}


@Entity
@Table(name="user_login")
public class UserLogin implements java.io.Serializable {

    @Column(name = "password", nullable = false)
    private String password;


    @OneToOne(optional = false, fetch = FetchType.LAZY) 
  private User user;


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }   

}

JSP File:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>Registration Page</title>
</head>
<body>
    <form:form action="/test" commandName="user">
        <tr>
            <td>User Name :</td>
            <td><form:input path="firstName" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:input path="userLogin.password" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Register"></td>
        </tr>
        </table>
    </form:form>
</body>
</html>

Spring Controller:

@Controller(value = "/")
public class Test { 

  @Autowired
    UserServiceImpl userServiceImpl;

  @RequestMapping(method = RequestMethod.GET, value = "/test")
  public void test(ModelMap model) {

    model.addAttribute("user", new User());

  }

  @RequestMapping(method = RequestMethod.POST, value = "/test")
  public void test(User user) {

    userServiceImpl.update(user);    

  }
} 
  • 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-09T19:38:14+00:00Added an answer on June 9, 2026 at 7:38 pm

    As usual, bidirectional relationships do have owning side. Owning side of relationship is attribute that is referenced by mappedBy. In your case attribute user in UserLogin entity is the owning side.

    When relationship is persisted to the database, only owning side is consulted. This means, that you have to set value for user attribute to be able to persist. To keep also entity graph in memory consistent both sides of the relationship should be set.

    In JPA 2.0 specification this is told with following words:

    Bidirectional relationships between managed entities will be persisted
    based on references held by the owning side of the relationship. It is
    the developer’s responsibility to keep the in-memory references held
    on the owning side and those held on the inverse side consistent with
    each other when they change.

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

Sidebar

Related Questions

I have a two separate tables that contain parts of user name (don't ask
I have 3 tables comments(id,question_id, user_Id) // here the user_id is of the user
guys I have two question to ask, they're easy, but bothering me for a
Well,i have a weird question to ask,sometimes when designing a form and using lots
I've only have a simple question to ask : Is it possible to call
I have seen stackoverflow ask question page they disable button until my postback event
I have a question that I'm ashamed to ask, but I'm going to have
I have to ask this question because I'm stuck, and I think its because
I have a SQL question that i need to ask the experts. To be
Before I ask the question let me state that I have attempted to google

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.