我在类Report和Flyleaf类之间有一对一的hibernate映射。 列出的代码已成功执行,映射效果很好。
在我的真实模型中,报告的主键由" id"和"索引" flyleaf也是如此(因为它的主键是报告的主键)。
如何更改我的代码以使主键复合(我尝试过,但是当我想要获取报告列表并且我还尝试将密钥定义为PKClass时抛出异常)。 (我使用Hibernate和Spring,如果这可以帮助的话)
@Entity
@Table(name="report_t")
public class Report {
private int id, nbPage;
private Flyleaf flyleaf;
public Report() {}
public Report(int id) {
this.id = id;
}
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="nb_page")
public int getNbPage() {
return nbPage;
}
public void setNbPage(int nbPage) {
this.nbPage = nbPage;
}
@OneToOne (fetch = FetchType.LAZY, mappedBy = "report", cascade = CascadeType.ALL)
public Flyleaf getFlyleaf() {
return flyleaf;
}
public void setFlyleaf(Flyleaf flyleaf) {
this.flyleaf = flyleaf;
}
@Override
public String toString() {
return "ID : "+id+"\t nb page : "+nbPage+"\n\t"+flyleaf;
}
}
@Entity
@Table(name="flyleaf_t")
public class Flyleaf {
private int id;
private String title, author, checker;
private Report report;
public Flyleaf() {}
public Flyleaf(int id) {
this.id = id;
}
@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "report_t"))
@Id
@GeneratedValue(generator = "generator")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column (name="title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column (name="author")
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Column (name="checker")
public String getChecker() {
return checker;
}
public void setChecker(String checker) {
this.checker = checker;
}
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public Report getReport() {
return report;
}
public void setReport(Report report) {
this.report = report;
}
@Override
public String toString() {
return "Flyleaf [id=" + id + ", title=" + title + ", author="
+ author + ", checker=" + checker + "]";
}
}