-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: @JoinTable and @EmbeddedId
PostPosted: Tue Aug 02, 2016 4:18 pm 
Newbie

Joined: Tue Aug 02, 2016 2:56 pm
Posts: 1
Hi guys, i have Record table which contain @EmbeddedId. I try to add in Record entity which already it contains, by using @JoinTable. I need add to Record two different Doctors without many-to-many relation. Functionally it will be two diffrent Doctor. Can you help me ?

Code:
@Table(name = "T_Record")
public class Record{

   @EmbeddedId
   private RecordPrimaryKey id;

   @ManyToOne(targetEntity = Operation.class)
   @JoinColumn(name = "operationID")
   private Operation operation;

   @ManyToOne(targetEntity = Patient.class)
   @JoinColumn(name = "patientID")
   private Patient patient;

   @ManyToOne(targetEntity = Doctor.class)
   @JoinTable(
            name = "Doctor_Record",
            joinColumns = { @JoinColumn(name = "RecordPrimaryKey")},
            inverseJoinColumns = {@JoinColumn(name="doctorId",referencedColumnName="id")}
          )
   private Doctor doctor;

Code:
@Entity
@Table(name = "T_Doctor")
public class Doctor extends User {

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

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

   @Column(name = "work", length = 128)
   private String work;

   @Column(name = "card_number", length = 16)
   private Long cardNumber;

   @Column(name = "bank_name")
   private String bankName;

   @OneToMany(targetEntity = Record.class)
   private List<Record> records;


Top
 Profile  
 
 Post subject: Re: @JoinTable and @EmbeddedId
PostPosted: Wed Aug 03, 2016 1:03 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
The RecordPrimaryKey cannot be a column in the database. If you are interested in this topic, I suggest you read The best way to map a Composite Primary Key with JPA and Hibernate article.

After you fix the @ManyToOne mapping as indicated in that article, you need to change the @OneToMany side to:

Code:
@OneToMany(mappedBy = "doctor")
private List<Record> records;

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.