Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OneToOne unidirectional and bidirectional

I have two examples that first is @OneToOne unidirectional mapping and second bidirectional. In unidirectional mapping, the owning-side table must contain a join column that refers to the other table's id; then in bidirectional both of them must contain a foreign key column for each other. But after generating the database schema with autogenerate strategy, two examples have the same effect on database schema. Unidirectional mapping is normal but the bidirectional example only contains one foreign key column, but it must be involve each other's foreign key!

Unidirectional Mapping

@Entity
public class Customer43 {

@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
@OneToOne
@JoinColumn(name = "address_fk")
private Address43 address;

// Getters, Setters and Constructors.
}

@Entity
public class Address43 {

@Id
@GeneratedValue
private Long id;
private String street1;
private String street2;
private String city;
private String state;
private String zipcode;
private String country;

// Getters, Setters and Constructors.
}

Bidirectional Mapping

@Entity
public class Customer44 {

@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
@OneToOne
@JoinColumn(name = "address_fk")
private Address43 address;

// Getters, Setters and Constructors.
}

@Entity
public class Address44 {

@Id
@GeneratedValue
private Long id;
private String street1;
private String street2;
private String city;
private String state;
private String zipcode;
private String country;
@OneToOne(mappedBy = "address")
private Customer44 customer;

// Getters, Setters and Constructors.
}

Why is the database schema output the same and why does bidirectional mapping act like unidirectional?

like image 338
Rahman Usta Avatar asked Feb 23 '12 20:02

Rahman Usta


People also ask

What is difference between unidirectional and bidirectional?

The direction of a relationship can be either bidirectional or unidirectional. A bidirectional relationship has both an owning side and an inverse side. A unidirectional relationship has only an owning side.

What is difference between JPA unidirectional One-To-One and ManyToOne?

The main difference between a OneToOne and a ManyToOne relationship in JPA is that a ManyToOne always contains a foreign key from the source object's table to the target object's table, whereas a OneToOne relationship the foreign key may either be in the source object's table or the target object's table.

What is unidirectional and bidirectional mapping in hibernate?

If you have a unidirectional @OneToMany association, it means you can only access the relationship from the parent side which manages the foreign key. For the bidirectional @OneToMany association, you can navigate the association in both ways, either from the parent or from the child side.

Is One-To-One bidirectional?

@OneToOne(mappedBy="user") //defines a bidirectional relationship.


1 Answers

Because you didn't understand how a bidirectional OneToOne is mapped. You don't need two foreign keys. A single one is sufficient to perform a join between both tables, in both directions:

select a.* from address a inner join customer c on c.addressId = a.id;
select c.* from customer c inner join address a on c.addressId = a.id;

The fact that the association is unidirectional or bidirectional doesn't change how the tables are linked together. It just changes the mapping, and allows navigating through the association in both directions.

In a bidirectional association, you always have an owning side (which tells how the association is mapped, using which join column), and an inverse side, where you just say: I'm the other side of the association that is mapped by this field (the field in the mappedBy attribute value) in the target entity.

like image 147
JB Nizet Avatar answered Sep 20 '22 12:09

JB Nizet