Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Hibernate One-To-Many non FK JoinColumn with same name
PostPosted: Sun Aug 13, 2017 5:36 pm 
Newbie

Joined: Sun Aug 13, 2017 5:32 pm
Posts: 2
Code:
Entity(name = "Assignment")
class Assignment {
  @Id
  private int id;

  private String assignmentCode;

}

Entity(name = "Review")
class Review {
  @Id
  private int id;

  private String assignmentCode;
}

I have 2 tables each with 2 columns as above. I want to create a list of reviews in the assignment entity and these reviews are joined on the column assignmentCode.

In the Assignment, I defined as below :

Code:
@OneToMany(targetEntity = Review.class, fetch=FetchType.EAGER)
@JoinColumn(name="assignmentCode")
private List<Review> reviews = new ArrayList<Review>();

but when I retrieve Assignment assignment = session.findById(Assignment.class, id); I'm getting the exception Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Ambiguous column name 'assignmentCode'

What's the correct to define the reviews collection? I cannot change the table structures to have different column names.


Top
 Profile  
 
 Post subject: Re: Hibernate One-To-Many non FK JoinColumn with same name
PostPosted: Mon Aug 14, 2017 3:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1516
Your mapping should look this:

Code:
@Entity(name = "Assignment")
class Assignment {
   @Id
   private int id;

   private String assignmentCode;

   @OneToMany(mappedBy = "assignment")
   private List<Review> reviews = new ArrayList<Review>();
   
   public void addReview(Review review) {
        reviews.add(review);
        reviews.setPost(this);
    }

    public void removeReview(Review review) {
        reviews.remove(review);
        reviews.setAssignment(null);
    }

}

@Entity(name = "Review")
class Review {
   @Id
   private int id;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(
      name = "assignmentCode",
      referencedColumnName = "assignmentCode"
   )
   private Assignment assignment;
}


There are several things I want you to pay attention to:

1. The @JoinColumn uses the referencedColumnName so that the FK is linked to a different column than the Parent entity PK.
2. Don't use EAGER fetching because is bad for performance. For more details, check out this article.
3. You should always use the mappedBy attribute on the @OneToMany because the other flavors of @OneToMany are suboptimal.
4. The add/remove utility methods allow you to synchronize both ends of the bidirectional associations as explained in this article.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Hibernate One-To-Many non FK JoinColumn with same name
PostPosted: Mon Aug 14, 2017 5:20 pm 
Newbie

Joined: Sun Aug 13, 2017 5:32 pm
Posts: 2
Great. Thank you very much for your answer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.