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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:58:06+00:00 2026-06-13T16:58:06+00:00

I have two models. @Entity public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) protected long id;

  • 0

I have two models.

@Entity
public class Student
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    protected long id;

    @?
    protected Address homeAddress;

    @?
    protected Address schoolAddress;
}

@Entity
public class Address
{
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   protected long id;

   @?
   protected List<Student> students;
}

What JPA/hibernate annotations do I need to put above homeAddress, schoolAddress and students to make the association work?

Of course I’ve tried many things and nothing worked.
For example, setting

    @ManyToOne
    @JoinColumn (name="student_homeAddress_id", updatable = false, insertable = false)
    protected Address homeAddress;

    @ManyToOne
    @JoinColumn (name="student_schoolAddress_id", updatable = false, insertable = false)
    protected Address schoolAddress;

and

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinColumns({
    @JoinColumn(name = "student_homeAddress_id", referencedColumnName = "student_homeAddress_id"),
    @JoinColumn(name = "student_schoolAddress_id", referencedColumnName = "student_schoolAddress_id")})
    @IndexColumn(name="students_index")
    protected List<Student> students;

yealds Unable to find column with logical name: student_homeAddress_id in org.hibernate.mapping.Table(Address) and its related supertables and secondary tables.
Also tried using mappedBy but that takes a single argument (can’t do mappedBy="student_homeAddress_id, student_schoolAddress_id".

Also looked into moving the JoinColumns to the Student tablet but I am not sure what the annotations should look like for OneToMany and ManyToOne as I have multiple Addresses there which JoinColumns doesn’t make much sense.

The thing that did work but was not creating the associations was having:

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinColumn(name="address_id")
    @IndexColumn(name="students_index")
    protected List<Student> students;

Using this, when storing in the DB the models, the student_homeAddress_id and student_schoolAddress_id were always null even after storing both ends (the Student and Address model).

My thought is that on the Address table there will be 3 extra columns: student_homeAddress_id (the id of the Student in the Student table for the homeAddress), student_schoolAddress_id (the id of the Student in the Student table for the schoolAddress) and students_index (the 0-based location on the students list). That should suffice, correct?

Any ideas?

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-13T16:58:07+00:00Added an answer on June 13, 2026 at 4:58 pm

    We tried mael’s suggestion but we couldn’t make it work.

    We ended up following this which worked.

    In other words, we have OneToMany relations:

    On Student:

    protected List<AddressStudentAssociation> addresses;
    

    On Address:

    protected List<AddressStudentAssociation> students;
    

    And on AddressStudentAssociation:

    @ManyToOne
    @PrimaryKeyJoinColumn(name="STUDENTID", referencedColumnName="id")
    private Student student;
    @ManyToOne
    @PrimaryKeyJoinColumn(name="ADDRESSID", referencedColumnName="id")
    private Address address;
    

    plus an argument to separate the one address from the other (isHome).

    Finally, inside Student we have public Address getHomeAddress() which traverses the addresses list and returns the correct address. We also had to play with the annotations to make it work. Not optimal in general but it works and we already spent too much time trying to make things work. 😐

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

Sidebar

Related Questions

Alright, so I have two models Teacher and Account: public class Teacher : Person
I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two entities person and Student , the student entity extends person and
I have two models which are related to each other: /** @Entity @Table(name=permissions) */
I have two models, Item and ShopSection. They have a many-to-many relationship. @Entity(name =
I have a model called 'UserRoleHolder' like below. @Entity public class UserRoleHolder extends Model
I have two mapped class with hibernate: SETTLEMENT_MODEL_GROUP (the parent): @Entity @Table(name = MODEL_GROUP)
I have need of a view that combines two entity models. I created a

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.