我有4節課。Entry、EntryTypeOne、EntryTypeTwo 和 ExampleClass
@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
我可以在不使用 EntryTypes 實作兩個串列的情況下執行此操作嗎(1 個用于 entrytypeone,1 個用于 entrytypetwo)?
uj5u.com熱心網友回復:
您沒有@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;
}
對于課程,你喜歡這樣......
// Just add a simple mapped by followed by the name of the field in the owning entity
@ManyToMany(mappedBy = "likedCourses")
Set<Student> likes;
這能解決您的問題嗎?在評論中告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/361203.html
上一篇:使用RazorPagePartial創建動態html日期表
下一篇:有沒有辦法把這些條放在圓圈后面?
