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

  • Home
  • SEARCH
  • 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 8644787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:23:09+00:00 2026-06-12T12:23:09+00:00

I have two persistence entity: User and UserDetail . They have one-to-one relationship. I

  • 0

I have two persistence entity: User and UserDetail. They have one-to-one relationship. I use hibernate annotations. But I am getting in my database several objects of user information for one same user. Apparently my knowledge of Hibernate annotations are not so good to solve this problem.

User class:

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

   @Id
   @GeneratedValue
   @Column(name = "ID")
   private Long id; 

   @Column(name = "NAME")
   private String name; 

   @Column(name = "PASSWORD")
   private String password;

   @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
   private UserDetail userDetail;

   // setters and getters

}

UserDetail class:

@Entity
@Table(name = "USER_DETAIL")
public class UserDetail {  

   @OneToOne
   @JoinColumn(name = "USER_ID")
   private User user;

   // other fields

}

I use this in my code as follows:

UserDetail userDetail = new UserDetail();
userDetail.setInfo(info);
userDetail.setUser(seventhUser);
hibernateTemplate.saveOrUpdate(userDetail);

And everything works properly. Here’s what my table USER_DETAIL:
USER_DETAIL table

But when I try to change user information, I get an incorrect behavior. I get following table after I again set user information:

UserDetail newUserDetail = new UserDetail();
newUserDetail.setInfo(newInfo);
newUserDetail.setUser(seventhUser);
hibernateTemplate.saveOrUpdate(newUserDetail);

enter image description here

Why the same two objects of information correspond to one user?? I have One-To-One relationship. How can I avoid this? What am I doing wrong?

  • 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-12T12:23:11+00:00Added an answer on June 12, 2026 at 12:23 pm

    If you want to modify an existing UserDetail, then you must set its ID, or get it from the session and modify it. Else, Hibernate thinks it’s a new one that must be saved, since it doesn’t have any ID.

    UserDetail existingUserDetail = session.get(UserDetail.class, theUserDetailId);
    existingUserDetail.setInfo(newInfo);
    

    To make sure you don’t save two UserDetail instances for the same user, you should add a unique constraint on the USER_ID column of the UserDetail database table.

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

Sidebar

Related Questions

I have defined a many-to-many relationship between my two entity classes User and Permission.
I use hibernate 3.5.4-final. I have two JPA classes. @Entity @Table(name = SPI_V_OP_OBJ_SUBJECT_VR) public
I'm just starting to learn Java database persistence using Hibernate ORM and have run
I have an app managed by maven with two modules: one for persistence, and
I have a two entity classes called User and Group, both reserved words that
I have two entities User and Group: @Entity public class User implements Serializable {
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two classes (MVC view model) which inherits from one abstract base class.
I have two columns say Main and Sub . (they can be of same
I have two entities person and Student , the student entity extends person and

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.