Java | JPA |休眠| AnnotationException:使用 @OneToMany 或 @ManyToMany 定位未映射的类

问题描述 投票:0回答:1

我刚接触 Java Spring Boot 2.7.15,但遇到了一些麻烦。我已经应用了几种解决方案,但它们都导致标题中出现相同的错误。

一些背景: 我正在构建一个宠物项目学习管理应用程序,其中讲师有社交媒体联系链接。为此,我尝试对讲师与社交使用 OneToMany 关系。

这是我的代码:


**|InstructorEntity.java|**

package com.<companyname>.learningmanagement.entity;

import com.<companyname>.learningmanagement.model.InstructorSocial;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "instructor", uniqueConstraints = @UniqueConstraint(columnNames = {"email"}))
public class InstructorEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String uuid;
    private String firstName;
    private String lastName;
    private String profession;
    @Column(columnDefinition = "longtext", length = 65555)
    private String bio;
    @Column(unique = true)
    private String email;
    private String phone;
    private String website;

    @OneToMany(
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    @JoinTable(
            name = "instructor_social",
            joinColumns = {@JoinColumn(name = "instructor_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "instructor_social_id", referencedColumnName = "id")}
    )
    private List<InstructorSocial> socials = new ArrayList<>();

    public InstructorEntity() {
    }

    public InstructorEntity(long id, String uuid, String firstName, String lastName, String profession, String bio, String email, String phone, String website) {
        this.id = id;
        this.uuid = uuid;
        this.firstName = firstName;
        this.lastName = lastName;
        this.profession = profession;
        this.bio = bio;
        this.email = email;
        this.phone = phone;
        this.website = website;
    }

    **Getters and Setters**
}

---------------------------------------------------------------------------------
**|InstructorSocialEntity.java|**

package com.<companyname>.learningmanagement.entity;

import com.<companyname>.learningmanagement.model.Instructor;

import javax.persistence.*;

@Entity
@Table(name = "instructor_social")
public class InstructorSocialEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String uuid;
    private String name;
    private String image;

    public InstructorSocialEntity() {
    }

    public InstructorSocialEntity(long id, String uuid, String name, String image) {
        this.id = id;
        this.uuid = uuid;
        this.name = name;
        this.image = image;
    }

    **Getters and Setters**
}

任何帮助将不胜感激。

java hibernate jpa one-to-many lms
1个回答
0
投票

它抱怨

@OneTomany
所注释的类
InstructorSocial
不是实体。实体是一个用
@Entity
注释的类。

所以认为应该是:

@OneToMany(....)
@JoinTable(.....)
private List<InstructorSocialEntity > socials = new ArrayList<>();

© www.soinside.com 2019 - 2024. All rights reserved.