Java 如何设置JPA实体类?
我正在努力解决如何设置我的JPA实体类以及哪些注释应该放在哪里 我有以下表格:Java 如何设置JPA实体类?,java,jpa,Java,Jpa,我正在努力解决如何设置我的JPA实体类以及哪些注释应该放在哪里 我有以下表格: Table Customer { id: primary key, name } Table CustomerDimension { id: primary key, foreign key(Customer.id), detail } 目前,我拥有以下实体类: public class Customer { @Id @GeneratedValue(strategy
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
Table Customer {
id: primary key,
name
}
Table CustomerDimension {
id: primary key,
foreign key(Customer.id),
detail
}
CustomerDimension
是拥有方。因此,@OneToOne
映射应该如下
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
}
public class CustomerDimension {
@Id
private long id;
@Column(name = "detail")
private String detail;
@OneToOne
private Customer customer;
}
您有以下问题:
- Customer和CustomerDimension需要注释
@Entity
- 在DDL中,表
在CustomerDimension
上有一个外键。因此,Customer
关系应该在@OneToOne
一侧声明CustomerDimension
- 仍然在DDL中,外键没有显式名称。我将假定它是
,并使用它来声明customer\u id
(见下文)@JoinColumn
只有在需要列的名称与属性的名称不同时,才需要注释(但为了清晰起见,可以保留注释)@Column
@Entity
@Table(name = "Customer") //Optional
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name") //Optional
private String name;
}
对于客户尺寸
:
@Entity
@Table(name = "CustomerDimension") //Optional
public class CustomerDimension {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "detail") //Optional
private String detail;
@OneToOne
@JoinColumn(name = "customer_id") //NOT optional
private Customer customer
}
编辑(回答您的评论):
如果您确实希望FK成为主键,可以这样做:
@Entity
@Table(name = "CustomerDimension") //Optional
public class CustomerDimension {
@Column(name = "detail") //Optional
private String detail;
@Id
@OneToOne
@JoinColumn(name = "id") //NOT optional
private Customer customer
}
我仍然想知道你为什么不把所有信息放在同一张表中。这将为您节省一个SQL联接。这里您拥有的是一个带有外键的
OneToMany
biidirection关系,而不是联接表。连接表似乎是供应商的首选,但它没有问题
因此,您在Customer
中有一个CustomerDimensions
列表(或集合),但带有mappedBy
值集
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy="customer")
List<CustomerDimensions> dimensions;
}
客户
有一组维度是很自然的。通过双向映射,如果您有一个维度,那么您可以轻松地查找客户(只需参考客户字段)
编辑:由于
CustomerDimension
表有一个Customer
id引用,因此可以为一个Customer
选择多个CustomerDimensions
,从而形成一个OneToMany
关系。要设置CustomerDimension.customer_id
字段,只需在Customers
维度列表中添加CustomerDimension
只要把@Id
,因为你有一个单向的relationship@Ramanlfc-这不是一种双向关系吗,我可以从CustomerDimension->Customer开始,反之亦然?您还需要在这两个类上添加注释@Entity
和@Table
。对于第二个类,您还需要@GeneratedValue,您应该通过一些基本教程来了解exmaplesyou已经得到了与您的模式要求完全相反的映射我觉得我可能给出了错误的表定义Customer
是根对象(上下文/主题,如果您愿意)CustomerDimension
只是Customer
的额外信息,因此我希望从Customer
实体获得CustomerDimension
实体。此外,不会生成CustomerDimension.id
。它应该与引用的Customer.id
相同<代码>客户是生成id@Cheetah然后您可以将FK定义为PK,但这似乎是一个奇怪的用例。编辑了答案。这是一个单独的表,因为CustomerDimension
记录很大,并不总是必需的。它实际上是一对一的映射,因此客户只有一个维度。但它是双向的,但我不明白的是,CustomerDimension.id
上没有注释……这个值是如何填充的?是什么让你认为它是一个?您需要CustomerDimension.ID上的典型ID注释。我把它们漏掉了,因为你漏掉了。请参见编辑。
public class CustomerDimension {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "detail")
private String detail;
@ManyToOne
Customer customer;
}