0

我有两个表(已经创建),说 Person 和 Address 具有以下模式:

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类的id字段添加注释。

4

3 回答 3

1

在 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;
于 2012-09-05T08:45:49.220 回答
0
 class Person {
   @Id @GeneratedValue
   @Column(name="id")
   int id;
   String name;

   @OneToOne
   @JoinColumn(name = "address_id", referencedColumnName = "id")
   Address address;
}

class Address {
    @id
    int id;
}
于 2012-09-05T08:45:07.427 回答
0

我认为您想使用@MapsId

@Entity
class Person {
   @Id 
   @GeneratedValue
   int id;

   String name;

   @OneToOne 
   @MapsId
   Address address;
}

@Entity
class Address {
    @Id
    int id;
}

查看@OneToOne文档中的示例 2。

于 2012-09-05T19:46:24.780 回答