Tanya
Tanya

Reputation: 47

Many to Many Spring MVC mappedBy reference an unknown target entity property

Good day everyone I’m trying to create a relationship for the entities Shelter and Owner, many to many, but a mistake is climbing, I do not understand what's the matter

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @DynamicUpdate
    @Entity
    @Table(name = "owner")
    public class Owner {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idOwner;

    private String name;

    private String address;

    private String description;

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(
            name = "owner_shelter",
            joinColumns = {@JoinColumn(name = "owner")},
            inverseJoinColumns = {@JoinColumn(name = "shelter")}
    )
    private Set<Shelter> shelterOwner;
}

--

@Data
@DynamicUpdate
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Entity
    @Table(name = "shelter")
    public class Shelter {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;

        private String name;
        private String address;
        private String description;

    @ManyToMany(mappedBy = "shelter")
    private Set<Owner> sheltersOwner;
}

and the error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: ru.itis.springbootdemo.models.Owner.shelter in ru.itis.springbootdemo.models.Shelter.sheltersOwner

Upvotes: 1

Views: 329

Answers (2)

celikz
celikz

Reputation: 111

problem is field name of the shelter in Owner class, it must be private Set<Shelter> shelter; not private Set<Shelter> shelterOwner;

Upvotes: 1

CodeScale
CodeScale

Reputation: 3304

Error message is explicit , this is not correct

 @ManyToMany(mappedBy = "shelter")
 private Set<Owner> sheltersOwner;

should be

 @ManyToMany(mappedBy = "shelterOwner")
 private Set<Owner> sheltersOwner;

mappedBy references the other side attribute name and in your code it is not correctly set.

Upvotes: 3

Related Questions