Domanda

I have two entities, let's say

Person.java:

@Entity
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = AUTO)
    private long id;

    @OneToMany(mappedBy = "personData", cascade = CascadeType.PERSIST)
    private List<SkillsData> skillsData;
        // ...
    }

SkillsData.java

@Entity
@Table(name = "SkillsData")
public class SkillsData implements Serializable {
    @Id
    @GeneratedValue(strategy = AUTO)
    private long id;

    @JoinColumn(name = "PERSONID")
    @ManyToOne(cascade = REMOVE)
    private Person personData;
    // ...
}

When I create a person, add a list of type SkillsData to it's skillsData field and persist it everything works with no exceptions thrown, but when I browse the database directly in the SkillsData table the field PERSONID is not populated and because of that the skills added can't be referenced to the right person. I'm trying to fix this problem for quite some time and I'll be thankful for any help.

È stato utile?

Soluzione

The problem might be in the fact that you're not setting SkillsData.personData before persisting leaving it null.

You must set it cause adding SkillsData to the Person.skillsData list is not enough since you declared this side of relationship as inverse(mappedBy attribute).

Therefore it is the SkillsData.personData non-inverse side who is responsible for establishing this relationship.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top