我需要帮助在java类中为这些关系表创建映射Hibernate Annotations(ONE-to-ONE):
CREATE TABLE book
(
id_book integer NOT NULL,
isdn character varying(10) NOT NULL,
CONSTRAINT "pk_bookId" PRIMARY KEY ("id_book")
)
CREATE TABLE info
(
id_info serial NOT NULL,
title character varying(200),
author character varying(200),
id_book integer NOT NULL,
CONSTRAINT info_pkey PRIMARY KEY ("id_info"),
CONSTRAINT "info_id_book_fkey" FOREIGN KEY ("id_book")
REFERENCES book ("id_book") MATCH SIMPLE
ON UPDATE CASCADE ON DELETE NO ACTION,
CONSTRAINT "info_id_book_key" UNIQUE ("id_book")
)
任何人都可以帮助我吗?谢谢。
答案 0 :(得分:0)
您可以在这两个实体之间创建一对一关系,如下所示。
@Entity
@Table(name = "book")
class book{
private int id;
private String isdn;
private Info info;
@Id
@Column(name="id_book", unique=true, nullable=false)
@GeneratedValue
public int getId(){
return this.id;
}
@Column(name="isdn")
public String getIsdn(){
return this.idsn;
}
public void setId(int id){
this.id = id;
}
public void setIsdn(String isdn){
this.isdn = isdn;
}
@OneToOne
@PrimaryKeyJoinColumn
public Info getInfo(){
return this.info;
}
public void setInfo(Info info){
this.info = info;
}
}
@Entity
@Table(name = "info")
class Info{
private int id;
private String title;
private String author;
private Book book;
@Id
@Column(name="id_info", unique=true, nullable=false)
@GeneratedValue
public int getId(){
return this.id;
}
@Column(name="title")
public String getTitle(){
return this.title;
}
public void setId(int id){
this.id = id;
}
public void setTitle(String title){
this.title = title;
}
@OneToOne(mappedBy="info", cascade=CascadeType.ALL)
public Book getBook(){
return this.book;
}
public void setBook(Book book){
this.book = book;
}
public void setAuthor(String author){
this.author = author;
}
@Column(name="author")
public String getAuthor(){
return this.author ;
}
}