我正在尝试在springboot jpa中使用@onetomany关系

rwqw0loc  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(340)

我在试着用 @OneToMany 关系,但出现错误: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.digitalProfile.digitalProfile.entity.Education.faculty. 我使用的jdk 11 my education.java模型如下所示:

package com.digitalProfile.digitalProfile.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="Education")
public class Education extends BaseEntity {

private String name;
private String establishDateNepali;
private String establishDateEnglish;
@OneToOne
private OwnedType ownedType;
private double area;
private int teacherCount;
private int studentCount;
private int staffCount;
@OneToOne
private AcademicLevel academicLevel;

@OneToMany(mappedBy = "education", fetch = FetchType.LAZY,
        cascade = CascadeType.ALL)
private Faculty faculty;

private String province;
private String district;
private String munvdc;
private String ward;
private String gauntole;
private String streetname;
private String city;

//getter setter

faculty.java如下所示:

package com.digitalProfile.digitalProfile.entity;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table
public class Faculty extends BaseEntity{

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "education_id", nullable = false)
private Education education;

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

基本实体:包com.digitalprofile.digitalprofile.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

@MappedSuperclass
public class BaseEntity {

@Id
@Column(nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Temporal(TemporalType.TIMESTAMP)
@CreationTimestamp
@Column(updatable = false)
private Date dateCreated;

@Temporal(TemporalType.TIMESTAMP)
@UpdateTimestamp
private Date dateUpdated;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Date getDateCreated() {
    return dateCreated;
}

public void setDateCreated(Date dateCreated) {
    this.dateCreated = dateCreated;
}

public Date getDateUpdated() {
    return dateUpdated;
}

public void setDateUpdated(Date dateUpdated) {
    this.dateUpdated = dateUpdated;
}

}

f4t66c6m

f4t66c6m1#

您目前已将教育课程定义为对教员有一个单一的参考变量:

public class Education extends BaseEntity {

    private Faculty faculty;

}

但是如果你想拥有一个从教育到教师的单一关系,那就意味着一个教育对象需要引用多个教师对象。因此,您需要更改数据结构,以便您的教育类可以容纳对faculty对象的多个引用。
这通常是通过将字段定义为类似集合的列表来实现的:

public class Education extends BaseEntity {

    private List<Faculty> faculties;

}
jfewjypa

jfewjypa2#

如果你说教育可以有多种能力,那么它应该被定义为

@OneToMany(mappedBy = "education", fetch = FetchType.LAZY,
        cascade = CascadeType.ALL)
private Set<Faculty> faculties;

相关问题