【问题标题】:Use of @OneToMany or @ManyToMany targeting an unmapped class使用 @OneToMany 或 @ManyToMany 定位未映射的类
【发布时间】:2016-06-25 23:26:43
【问题描述】:

我尝试了其他问题中描述的解决方案,但没有一个有效,我不知道发生了什么,这是 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; }
}

这是引发的 Hibernate 异常:

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

知道为什么会这样吗?

【问题讨论】:

  • 发生这种情况是因为关系的目标不是映射类。

标签: java spring hibernate hibernate-annotations


【解决方案1】:

尝试从private Set&lt;Long&gt; favoriteProducts;中删除@OneToMany注解

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2011-06-24
    • 2016-08-25
    • 1970-01-01
    相关资源
    最近更新 更多