gpt4 book ai didi

java - JPA/hibernate : Annotations for already created table

转载 作者:行者123 更新时间:2023-12-01 15:11:34 27 4
gpt4 key购买 nike

我有两个表(已创建),即具有以下架构的“人员”和“地址”:

create table Person (
`id` int(11) not null auto_increment,
`name` varchar(255) default null,
primary key(`id`)
)

create table Address (
`id` int(11) not null,
`city` varchar(255) default null,
primary key (`id`),
constraint foreign key (`id`) references `Person`(`id`)
)

现在,我应该在代码中使用哪些注释?

这两个类的骨架是:

class Person {
@Id @GeneratedValue
@Column(name="id")
int id;
String name;

Address address;
}

class Address {
int id;
}

我需要为 Person 类中的 address 字段和 Address 类中的 id 字段添加注释。

最佳答案

在 Person.java

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id",nullable=false)
@ForeignKey(name = "fk_id")
private Address address;

和地址.java -

@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "id", updatable = false, insertable = false, nullable=false)
private Person id;

@Column(name = "id", insertable = false, updatable = false, nullable=false)
private Integer id;

关于java - JPA/hibernate : Annotations for already created table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12277501/

27 4 0