A
A
Albert2020-05-13 15:39:27
Java
Albert, 2020-05-13 15:39:27

Why are Entity dependencies not being loaded?

There are classes managers:

@Entity
@Table (name = "managers")
public class Managers {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
...........
///////////////////// Worcs worcs ////////////////////////
    @OneToMany(cascade = CascadeType.PERSIST,
            orphanRemoval = true)
    @JoinTable(name="worcs_managers",
            joinColumns =
                    {@JoinColumn(name = "managers_id", referencedColumnName = "id")},
            inverseJoinColumns =
                    {@JoinColumn(name = "worcs_id", referencedColumnName = "id")})
    private Set<Worcs> worcs;

    public Set<Worcs> getWorcs() {
        return worcs;
    }

    public void setWorcs(Set<Worcs> worcs) {
        this.worcs = worcs;
    }
    ///////////////////// Worcs worcs ////////////////////////
.......

and class works
@Entity
@Table (name = "worcs")
public class Worcs {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

..................
///////////////////// Manager manager ////////////////////////
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    @JoinTable(name="worcs_managers",
            joinColumns =
                    {@JoinColumn(name = "worcs_id", referencedColumnName = "id")},
            inverseJoinColumns =
                    {@JoinColumn(name = "managers_id", referencedColumnName = "id")})
    private Managers managers;

    public Managers getManagers() {
        return managers;
    }

    public void setManagers(Managers managers) {
        this.managers = managers;
    }

/////////// Manager manager ////////////////////////
........

As you can see in the above classes, I tried to "hard-code" the One-to-many and many-to-one relationships.
But when I call the manager.getWorks() class, I don't receive more than one worcs object, although I add it beforehand and there is a connection in the database.
Link table :
5ebbe8f1605ad130195880.png
Table worcs:
5ebbe93ad23c9719789265.png
Table with managers:
5ebbe97b7141e513717427.png
Well, actually the code itself that shows 0:
5ebbea5bd4d35327463722.png

Gave me an exhaustive answer on form one, after reading the documentation provided at the link below, all questions disappeared:
https://docs.jboss.org/hibernate /orm/3.6/reference...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2020-05-14
@D3lphi

Add mappedBy="managers" to the OneToMany annotation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question