robert trudel
robert trudel

Reputation: 5749

Relation not set in a One to Many relation

I use Spring Boot 2, Spring Data with Hibernate implementation. Database used is Postgres

I try to save CarServices with it's child CarComponentOccurrences:

@Entity
public class CarServices{

    @OneToMany(mappedBy = "carService", cascade = CascadeType.PERSIST)
    private List<CarComponentOccurrences> carComponentOccurrences;

}


@Entity
public class CarComponentOccurrences {
    ...
    @ManyToOne
    private CarComponents carComponent;

    @ManyToOne
    private CarServices carService;

}

CarComponentOccurrences is saved but in db CarComponents and CarService is null.

In CarComponentOccurrences, CarComponents and CarService is set.

Edit:

In my service layer

@Autowired
private CarServicesRepository repository;

public void save(){
    CarServices cs = new CarServices();
    cs.setName("name");

    List<CarComponentOccurrences> carComponentOccurrences = new ArrayList<>();

    CarComponentOccurrences cco = new CarComponentOccurrences();
    Optional<CarComponents> optCarComponents =carComponentsRepository.findById(1);
    if (optCarComponents.isPresent()) {
        cco.setCarComponentOccurrences(optCarComponents.get());
    }

    cco.CarServices(cs);

    carComponentOccurrences.add(cco);
    cs.setCarComponentOccurrences(carComponentOccurrences);

    repository.save(cs);
}

Edit 2

CREATE TABLE car_component_occurrences
(
  id integer NOT NULL,
  ...
  car_component_id integer,
  car_service_id integer,
  CONSTRAINT car_component_occurrences_pkey PRIMARY KEY (id),
  CONSTRAINT fka4fmpytg0s9a94377pdw5ssib FOREIGN KEY (car_service_id)
      REFERENCES car_services (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fko85tjs5s6f1o9u7kkk152d147 FOREIGN KEY (car_component_id)
      REFERENCES car_components (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

Edit 3

Hibernate: 
    select
        nextval ('public.car_component_occurrences_id_seq')
Hibernate: 
    insert 
    into
        public.car_component_occurrences
        ( car_component_id, car_service_id, name, id) 
    values
        (?, ?, ?, ?)
Hibernate: 
    update
        public.car_services 
    set
        name=?,
    where
        id=?

Upvotes: 2

Views: 205

Answers (2)

Kirinya
Kirinya

Reputation: 245

Try:

In CarServices:

  • cascade = CascadeType.PERSIST -> PERSIST, MERGE

In CarComponentOccurrences:

  • @ManyToOne private CarComponents carComponent; -> @ManyToOne(cascade=CascadeType.PERSIST)

Let me know if that helps.

Upvotes: 1

Maciej Kowalski
Maciej Kowalski

Reputation: 26502

If your column names in db are car_component_id and car_service_id then you should use the @JoinColumn annotation in order to override the defaults of the persistence provider, which is expecting: carcomponent_id and carservice_id (if the primary keys of those referenced entities are named id) respectively.

Reference from JoinColumn:

Default (only applies if a single join column is used): The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; "_"; the name of the referenced primary key column.

Upvotes: 0

Related Questions