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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:21:34+00:00 2026-05-28T13:21:34+00:00

I have User and Address classes as follows: class User { … … @OneToOne(

  • 0

I have User and Address classes as follows:

class User
{
   ...
   ...
   @OneToOne( cascade=CascadeType.ALL)
   @JoinColumn(name="addr_id")
   private Address address;
}

class Address
{
   ...
   ...
   @OneToOne(mappedBy="address")
   private User user;
}

Here my assumption is with this domain model I can associate one address to only one user(user.addr_id should be unique).

But with this domain model, I am able to create multiple users and associate them to the same address.

User u1 = new User();
//set data
Address addr1 = new Address();
//set data
u1.setAddress(addr1);
...
session.save(u1); 

–> this is creating one address record with addr_id=1 and inserting one user record in user table with addr_id=1. Fine.

Again,

User u2 = new User();
//set data
Address addr1 = (Address) session.load(Address.class, 1);
//set data
u2.setAddress(addr1);
...
session.save(u2);

–> This is creating second user record and associating to existing address with addr_id=1, which is not desired.

I can use @OneToOne(..)@JoinColumn(name=”addr_id”, unique=true) to prevent this.
But what will be the difference of using @OneToOne rather than @ManyToOne(.., unique=true).

@OneToOne itself should impose “unique=true” condition..right?

-Siva

  • 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-05-28T13:21:35+00:00Added an answer on May 28, 2026 at 1:21 pm

    @OneToOne is annotating the Java to express the idea of the relationship between the two classes. If this was a @OneToMany then we’d have a collection instead. So reading the annotations we understand the realtionships, and the JPA runtime also understands those.

    The actual policing of the one-to-one is performed in the database – we need the schema to have the uniqueness constraints. The @JoinColumn expresses how that relationship is manifest in the DatabaseSchema.This can be useful if we are generating the schema.

    However in many cases we use bottom-up tools to generate the Java from the schema. In this case there’s no actual need for the Java annotations to reflect the database constraints, from a Java perspective we just see the relationship.

    An intelligent compiler might warn us if the semantics of our @JoinColumn doesn’t match the @oneToOne, but I’m not sure whether current implementations do that.

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

Sidebar

Related Questions

I got classes: @Entity @Table(name=users) public class User{ private Integer id; private String name;
i have a User class with a field for the e-mail-address and a password.
I have classes: class User{ String id; ArrayList<UserAttribute> attributeList=new ArrayList<UserAttribute>(); } class UserAttribute{ User
i have the following classes : class Address { String street; String street2; String
I have two POCO classes a User and an Address. Address is a complex
I have two domain classes: User : class User { String login String password
I have a model that holds user address. This model has to have first_name
I currently have a user control that is used on both an /Address/Edit and
I have an address finder system whereby a user enters a postcode, if postcode
I have a web page where the user will enter their address. They will

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.