我刚到春天冬眠。我创建一个具有列的表,它们的实体应像这样
School.java
@Entity
@Table(name = "school")
public class School implements Comparable<School>{
@Transient
private int functionValue;
@Id
@GeneratedValue
private Integer id;
@Size(min = 3, message = "Name must be at least 3 characters!")
@UniqueUsername(message = "School Name already exists!")
@Column(length = 1000, unique = true)
private String name;
private SchoolTier schoolTier1 = new SchoolTier();
private SchoolTier schoolTier2 = new SchoolTier();
private SchoolTier schoolTier3 = new SchoolTier();
//their getter/setter
}
学校层
@Entity
@Table(name = "schooltier")
public class SchoolTier {
@Id
@GeneratedValue
private Integer id;
@Id
private Integer schoolId;
// Pojo's
@Size(min = 0, message = " must be at least 4 characters!")
private String CampusImprovement;
//getter/setter
}
具有学校名称的表,该表具有在school.java中定义的相同列,并且还具有表schooltier,其表具有与schooltier.java中定义的相同列
但是问题出在这里,像是学校表没有“ SchoolTier”的任何列,但是在school.java中,我有3个schooltier对象。现在它给出了一个异常
Caused by: org.hibernate.MappingException: Could not determine type for com.scp.bid.entity.SchoolTier at table: school, for columns: [org.hibernate.mapping.Column(schoolTier1)]
现在该如何解决这个问题?如果我做错了,请指导我。帮我
最佳答案
使用休眠的JPA @OneToOne映射通过SchoolTier映射学校课程,
School将是父实体,就像数据库中的父实体一样,它将是具有SchoolTier实体的子表的三个外键的父表。
看起来像这样:
@Entity
@Table(name = "school")
public class School implements Comparable<School>{
@Transient
private int functionValue;
@Id
@GeneratedValue
private Integer id;
@Size(min = 3, message = "Name must be at least 3 characters!")
@UniqueUsername(message = "School Name already exists!")
@Column(length = 1000, unique = true)
private String name;
//This will create a foreign key of SchoolTier table.
@JoinColumn(name = "school_tier_id_1")
@OneToOne
private SchoolTier schoolTier1;
//This will create a foreign key of SchoolTier table.
@JoinColumn(name = "school_tier_id_2")
@OneToOne
private SchoolTier schoolTier2 = new SchoolTier();
//This will create a foreign key of SchoolTier table.
@JoinColumn(name = "school_tier_id_3")
@OneToOne
private SchoolTier schoolTier3;
//their getter/setter
}
在您的情况下,SchoolTier将是子实体。
@Entity
@Table(name = "schooltier")
public class SchoolTier {
@Id
@GeneratedValue
private Integer id;
// Pojo's
@Size(min = 0, message = " must be at least 4 characters!")
private String CampusImprovement;
//getter/setter
}