1:1 Relation (One to One)

  1. Create a new Car entity and add the following attributes:
    1. registration of type Date 
    2. mileage of type Integer
    3. kw of type Integer
    4. price of type Double
  2. Close the Car tab and click Yes in the following dialog to save the Car entity
  3. Create a Logbook entity.
  4. Under Entities in the Project Manager, click Car.java and add the Car entity to the entity editor under Attributes.
  5. In the following dialog, choose the One to One (1:1) option. 
  6. Apply the setting Bidirectional.
  7. Click OK.
  8. Click Save.



Result:
  • Entity Car - The Car entity is enhanced by the logbook attribute of type Logbook.



    @OneToOne(mappedBy = "car")
    public Logbook getLogbook() {
    	return logbook;
    }
    
    public void setLogbook(Logbook logbook) {
    	this.logbook = logbook;
    }
  • Entity Logbook - The Logbook entity is enhanced by the car attribute of type Car.


    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "car_id")
    public Car getCar() {
    	return car;
    }
    
    public void setCar(Car car) {
    	this.car = car;
    }
Note:
  • Relation im Code per Annotation - In the code, the relation is defined in both entities with the annotation @OneToOne.
  • To correctly delete a 1:1 relation, you have to delete the corresponding attributes in both entities.


XDEV Software Corp. - One Embarcadero Center, San Francisco, CA 94111, US
Copyright © 2015. XDEV Software Corp. All rights reserved.