Search code examples
javaspringhibernatehibernate-annotations

Use of @OneToMany or @ManyToMany targeting an unmapped class


I've tried the solutions described in other questions but none of them worked, and I have no idea what's going on, here is the bean:

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table( name = "USER_ACTIVITY" )
public class UserActivity 
{
    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    @Column( name = "ID" )
    private long id;

    @ElementCollection( targetClass = Long.class )
    @OneToMany( cascade = CascadeType.ALL )
    @Column( name = "FAVORITE_PRODUCTS" )
    private Set<Long> favoriteProducts;

    public UserActivity() {}

    public Set<Long> getFavoriteProducts() { return favoriteProducts; }}

    public void setFavoriteProducts( Set<Long> favoriteProducts ) { this.favoriteProducts = favoriteProducts; }
}

And this is the Hibernate exception thrown:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: es.sidelab.cuokawebscraperrestserver.beans.UserActivity.favoriteProducts[java.lang.Long]

Any idea of why is this happening?


Solution

  • Try removing @OneToMany annotation from private Set<Long> favoriteProducts;

    You already defined that is a collection by using @ElementCollection, and @OneToMany is reserved for entities (java.lang.Long is not).

    You can find some information here: https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection