1:1 Relation (One to One)

  1. Create an entity Car and add the following attributes:

    1. registration of type date.

    2. mileage of type Integer.

    3. kw of type Integer.

    4. price of the type Double.

  2. Close the Car tab and click Yes in the following dialog to save the Car entity.

  3. Create an entity Logbook.

  4. In the Project Manager, click Car.java at Entities and paste the entity Car into the entity editor at Attributes.

  5. In the following dialog, select the One to One (1:1) option.

  6. Accept the setting Bidirectional.

  7. Click OK.

  8. Click Save.

    Image not found

Result:

  • Entity Car - The Car entity is extended by the logbook attribute of type Logbook.

@OneToOne(mappedBy = "car")
public Logbook getLogbook()
{
    return this.logbook;
}

public void setLogbook(final Logbook logbook)
{
    this.logbook = logbook;
}

Image not found

  • Entity Logbook - The Logbook entity is extended by the car attribute of type Car.

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "car_id")
public Car getCar()
{
    return this.car;
}

public void setCar(final Car car)
{
    this.car = car;
}

Image not found

  • Relation in code by annotation - In the code, the relation in both entities is defined using the annotation @OneToOne.

  • To correctly delete a bidirectional relation, you must delete the corresponding attributes in both entities.