1

Im trying to save in postgres 2 entities, Configuration and Screen. Configuration can have many Screens, then is oneToMany, and one Screen can have one Configuration, then ismanyToOne. I'm using JPA to save those entities. The problem I have is, in Screen table, configuration_id relationship is not saved. That's my code:

@Data
public class Configuration  {

@Id
private String id = UUID.randomUUID().toString();

@OneToMany(mappedBy = "id_configuration", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Screen> screen;

And this other

@Table
@Entity
@Data
public class Screen {

private String status;

@JoinColumn(name = "configuration_id")
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Configuration configuration;

when I create a new Configuration, I indicate the Screen that should be created except the 'id' field (it's configuration id created randomlly)

The data is saved well, both in Configuration and Screen, except for that (id_configuration) field. There is a configuration_id field in Screen table, but it is always empty and never filled. Why is this happening? I'm indicating the cascade type ALL, and relationships are well-formed.

0

1 Answer 1

0

Due to tutorials like: https://www.baeldung.com/hibernate-one-to-many

I guess the problem in your model is mappedBy in @OneToMany. In your case it should point to class field which mapping this relation.

So it should be: @OneToMany(mapped_by = "configuration", ...)

2
  • That's not the problem. I changed it @OneToMany(mapped_by = "configuration"....) but still the id is not saved in database
    – Talenel
    Nov 21 at 7:03
  • It is A problem. You have a few apparently. Next is - do your screens really have a configuration set? If it is null, the foreign key "configuration_id" will be null.
    – Chris
    Nov 21 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.