0

I am trying to understand OneToOne mapping between two Entities.

I am not able to understand how to specify the mappedBy attribute in OneToOne for making it bi-directional relation.

UserID embeddable type:

@Embeddable
public class UserID  implements Serializable {

    private static final long serialVersionUID = 1L;
    private int ssnID;
    private int uniqueNum;
    // getters and setters..
}

VehicleID embeddable type:

@Embeddable
public class VehicleID implements Serializable {
    private int vehicleID;
    private int regNum;
    // getters and setters
}

Vehicle Entity having composite primary key VehicleID:

@Entity
public class Vehicle {

    @EmbeddedId
    private VehicleID vehicleID;
    private String description;
    // getters and setters..
}

UserInfo Entity having composite priamary key UserID and is owning-side entity for OneToOne relationship between UserInfo and Vehicle:

@Entity
public class UserInfo {
    private String full_name;
    @EmbeddedId
    UserID userID;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumns({@JoinColumn(name="vehicle_ID") , @JoinColumn(name="reg_Num")}) // Composite primary key in Vehicle
    Vehicle veh;
    // getters, setters and other code
}

UserInfo is the owning-side entity and having OneToOne relationship with Vehicle entity (which has composite VehicleID as primary key).

My doubt is here:

In UserInfo entity, we specify the OneToOne relationship (and the annotations):

@OneToOne(cascade=CascadeType.ALL)
@JoinColumns({@JoinColumn(name="vehicle_ID") , @JoinColumn(name="reg_Num")})
Vehicle veh;

As can be seen above that in UserInfo table, we are having a composite foreign key on Vehicle table, and name of the JoinColumn specifying as vehicle_ID and reg_Num.

Now, when I want to make it bi-directional relation we need to use the mappedBy attribute, and it is that where I am not able to understand what property to refer.

@Ent/ity
public class Vehicle {
    @EmbeddedId
    private VehicleID vehicleID;
    private String description;

    @OneToOne(mappedBy="UserID")
    private UserInfo userInfo;

    // getters and setters..
}

I kept "UserID" as it is "composite" primary key for UserInfo, but it is throwing exception:

Exception in thread "main" org.hibernate.AnnotationException: Unknown mappedBy in: com.example.entity.Vehicle.userInfo, referenced property unknown: com.example.entity.UserInfo.UserID

I am not able to understand what property to refer here; the examples which i have seen in tutorials, most of them have a single PK, FK; but in my case it is composite PK, FK.

Can anyone help me understand this?

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 2
    It should be `@OneToOne(mappedBy="veh")` – Hadi J Dec 28 '17 at 13:27
  • @HadiJeddizahed: It works, but could you elaborate a bit on this? I still don't understand this. – CuriousMind Dec 28 '17 at 13:31
  • 1
    The `mappedBy` element identifies the inverse side of a relationship. here you have relation between `Vehicle` and `UserInfo` so to identify inverse side of relation should using it by other side property. here `veh`. sorry for bad explaining :( see [this](https://stackoverflow.com/questions/9108224/can-someone-please-explain-mappedby-in-hibernate) – Hadi J Dec 28 '17 at 13:41
  • 2
    @HadiJeddizahed Please don't write answers in the comments. Can you please post this as the answer. – Simon Martinelli Dec 28 '17 at 19:09

1 Answers1

1

It should be like this:

@OneToOne(mappedBy="veh")
private UserInfo userInfo;

The mappedBy element identifies the inverse side of a relationship. here you have relation between Vehicle and UserInfo so to identify inverse side of relation should using it by other side property. here veh.

for more information see this

Hadi J
  • 16,989
  • 4
  • 36
  • 62