使用 @OneToMany 或 @ManyToMany 定位未映射的 class

Use of @OneToMany or @ManyToMany targeting an unmapped class

我已经尝试过其他问题中描述的解决方案,但 none 的解决方案有效,但我不知道发生了什么,这就是问题所在:

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; }
}

这是抛出的 Hibernate 异常:

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

知道为什么会这样吗?

尝试从 private Set<Long> favoriteProducts;

中删除 @OneToMany 注释

您已经使用@ElementCollection 定义了它是一个集合,@OneToMany 是为实体保留的(java.lang.Long 不是)。

您可以在这里找到一些信息:https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection