【发布时间】:2016-03-08 22:13:00
【问题描述】:
我正在努力设置我的 JPA 实体类以及哪些注释应该放在哪里
我有以下表格:
Table Customer {
id: primary key,
name
}
Table CustomerDimension {
id: primary key, foreign key(Customer.id),
detail
}
目前我有以下实体类:
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@OneToOne
private CustomerDimension customerDimension;
}
public class CustomerDimension {
// ? what is meant to go here?
private long id;
@Column(name = "detail")
private String detail;
}
CustomerDimension.id 上的注释是什么,以允许我插入具有新 CustomerDimension 的新客户?
CustomerDimension 是否也应该有对 Customer 的引用?
【问题讨论】:
-
只需输入
@Id,因为您的关系是单向的 -
@Ramanlfc - 这不是双向关系吗,我可以从 CustomerDimension -> Customer 去,反之亦然?
-
你还需要在两个类上添加注解
@Entity和@Table。 -
第二类你也需要@GeneratedValue,你应该通过一些基础教程来查看示例
-
您的映射与您的架构所需的完全相反