Hibernate 与继承的关系
Hibernate Relationships with inheritance
我有 4 个类。条目、条目类型一、条目类型二和示例类
@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="entryType",
discriminatorType = DiscriminatorType.STRING)
public class Entry {
@Id
@GeneratedValue
protected Long id;
@Column(nullable = false)
protected String title;
@Column(nullable = false)
protected String description;
}
@Getter
@Setter
@Entity
@DiscriminatorValue("One")
public class EntryTypeOne extends Entry{
@ManyToMany(cascade = CascadeType.ALL)
private List<ExampleClass> exampleClasses;
}
@Getter
@Setter
@Entity
@DiscriminatorValue("Two")
public class EntryTypeTwo extends Entry{
@ManyToMany(cascade = CascadeType.ALL)
private List<ExampleClass> exampleClasses;
}
@Getter
@Setter
@Entity
public class ExampleClass {
@Id
@GeneratedValue
private Long id;
@ManyToMany(mappedBy = "exapmleClasses")
private List<Entry> entries;
}
当我开始这个时,我得到这个错误:
nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: ch.musiciansearch.vingrn.entity.Entry.exampleClasses in ch.musiciansearch.vingrn.entity.ExampleClass.entries
我的目标是制作这样的数据库:
DBIMAGE
我可以在不使用 EntryType 实现两个列表的情况下执行此操作吗(1 个用于 entrytypeone,1 个用于 entrytypetwo)?
您没有以正确的方式使用 @ManyToMany
注释。
这是您的操作方式...假设您有一个 Student
实体和 Course
个实体。
而且学生喜欢一些课程,不喜欢其他课程。您想要在数据库中存储有关喜欢的课程的信息。这是你的做法...
class Student {
// OK, so student class will be the owner of the relationship
// Here we describe the join table, which stores many-to-many data in the DB
@ManyToMany
@JoinTable(
name = "course_like",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"))
Set<Course> likedCourses;
}
当然 class 你喜欢这样...
// Just add a simple mapped by followed by the name of the field in the owning entity
@ManyToMany(mappedBy = "likedCourses")
Set<Student> likes;
这是否解决了您的问题?在评论中让我知道。
我有 4 个类。条目、条目类型一、条目类型二和示例类
@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="entryType",
discriminatorType = DiscriminatorType.STRING)
public class Entry {
@Id
@GeneratedValue
protected Long id;
@Column(nullable = false)
protected String title;
@Column(nullable = false)
protected String description;
}
@Getter
@Setter
@Entity
@DiscriminatorValue("One")
public class EntryTypeOne extends Entry{
@ManyToMany(cascade = CascadeType.ALL)
private List<ExampleClass> exampleClasses;
}
@Getter
@Setter
@Entity
@DiscriminatorValue("Two")
public class EntryTypeTwo extends Entry{
@ManyToMany(cascade = CascadeType.ALL)
private List<ExampleClass> exampleClasses;
}
@Getter
@Setter
@Entity
public class ExampleClass {
@Id
@GeneratedValue
private Long id;
@ManyToMany(mappedBy = "exapmleClasses")
private List<Entry> entries;
}
当我开始这个时,我得到这个错误:
nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: ch.musiciansearch.vingrn.entity.Entry.exampleClasses in ch.musiciansearch.vingrn.entity.ExampleClass.entries
我的目标是制作这样的数据库: DBIMAGE
我可以在不使用 EntryType 实现两个列表的情况下执行此操作吗(1 个用于 entrytypeone,1 个用于 entrytypetwo)?
您没有以正确的方式使用 @ManyToMany
注释。
这是您的操作方式...假设您有一个 Student
实体和 Course
个实体。
而且学生喜欢一些课程,不喜欢其他课程。您想要在数据库中存储有关喜欢的课程的信息。这是你的做法...
class Student {
// OK, so student class will be the owner of the relationship
// Here we describe the join table, which stores many-to-many data in the DB
@ManyToMany
@JoinTable(
name = "course_like",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"))
Set<Course> likedCourses;
}
当然 class 你喜欢这样...
// Just add a simple mapped by followed by the name of the field in the owning entity
@ManyToMany(mappedBy = "likedCourses")
Set<Student> likes;
这是否解决了您的问题?在评论中让我知道。