javahibernateentitygraph

Unable to use @NamedEntityGraph with @ElementCollecion


I'm trying to set simple entity with NamedEntityGraph on it. Unfortunately it won't work. Could you have any ideas how to fix it?

ServiceType entity has @ElementCollection of with Set of String which are simply ids of PictureModel associated with entity. On run I got:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [backgroundPicIds] on this ManagedType [pl.mihome.djcost.model.ServiceType]

@Entity
@Table(name = "service_type")
@Getter
@Setter
@NoArgsConstructor
@NamedEntityGraph(
        name = "serviceType.with.backgroundPicIds",
        attributeNodes = @NamedAttributeNode("backgroundPicIds")
)
public class ServiceType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Setter(AccessLevel.NONE)
    @Column(nullable = false, updatable = false)
    private int id;

    @Length(max = 100)
    @NotBlank
    private String name;

    private boolean active;

    @OneToMany(mappedBy = "serviceType")
    private Set<Account> accounts;

    @ManyToMany(mappedBy = "servicesApplicable")
    private Set<AccountType> accountTypes;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "serviceType", orphanRemoval = true, cascade = CascadeType.ALL)
    private Set<PictureModel> backgroundPicture;

    @ElementCollection
    @CollectionTable(name = "image", joinColumns = {@JoinColumn(name = "service_type_id")})
    @Column(name = "id")
    private Set<String> backgroundPictureId;

    @PrePersist
    void activate() {
        this.active = true;
    }
}
@Entity
@Table(name = "image")
@NoArgsConstructor
@Getter
@Setter
@Builder
@AllArgsConstructor
public class PictureModel {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(nullable = false, updatable = false)
    @Setter(AccessLevel.NONE)
    private String id;

    private String type;

    private String name;

    @Lob
    private byte[] body;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_id")
    private Account account;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "public_platform_id")
    private PublicPlatform publicPlatform;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_type_id")
    private ServiceType serviceType;
}

Solution

  • You simply do not have an attribute with the name backgroundPicIds in the entity ServiceType.

    Try to correct your graph in this way:

    @NamedEntityGraph(
       name = "serviceType.with.backgroundPicIds",
       attributeNodes = @NamedAttributeNode("backgroundPictureId")
    )