弹簧数据JPA(Postgres) - 插入一张表格,从包含静态数据的表中读取外键
我目前正在使用Spring Data JPA,特别是Postgres。我们的数据库高度归一化。在某些情况下,我们有一些包含静态数据的表。在这种情况下,如果我要插入表address
例如,表address_type
包含静态数据,我需要插入表的主键address_type
在表格地址的一列中,要从表地址>地址>到表
address_type (静态数据)进行外键参考。
示例:
数据库代码
create table address_type(
id Serial Primary Key,
type char(3),
description (100)
);
insert into address_type(id, type, description) values(1, 'PRIMARY', 'Primary description');
insert into address_type(id, type, description) values(2, 'SECONDARY', 'Secondary description');
-n)
create table address(
id Serial Primary Key,
address varchar(50) not null,
address_type_id integer references address_type(id)
);
insert into address(id, address, address_type) values(1, 'address somewhere 1', 1);
insert into address(id, address, address_type) values(2, 'address somewhere 2', 1);
insert into address(id, address, address_type) values(3, 'address somewhere 3', 2);
关系
@Table(name = "address_type")
public class AddressType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "type")
private String type;
@Column(name = "description")
private String description;
@OneToMany(mappedBy = "addressType")
private Address address;
// Getters and Setters (Lombok)
}
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "type")
private String address;
@Column(name = "address_type_id")
@JoinColumn(---> logic?????????? <-----)
@ManyToOne(---> logic?????????? <-----)
private AddressType addressType;
// Getters and Setters (Lombok)
}
( 1 @joincolumn
和@manytoone
地址
实体中的注释?
pd 插入仅应在表地址
中发生,弹簧数据只能从表address_type
中读取以使外键存储在<<<代码>地址表。
I am currently working with Spring Data JPA, specifically Postgres. Our database is highly Normalized. In some scenarios we have some tables that contains static data. In that case if I am going to insert into table address
for example and table address_type
contains the static data, I would need to insert the Primary Key of table address_type
in a column of table address
, to make a Foreign Key reference from table address
to table address_type
(static data).
Example:
Database Code
create table address_type(
id Serial Primary Key,
type char(3),
description (100)
);
insert into address_type(id, type, description) values(1, 'PRIMARY', 'Primary description');
insert into address_type(id, type, description) values(2, 'SECONDARY', 'Secondary description');
Relation (1 - N)
create table address(
id Serial Primary Key,
address varchar(50) not null,
address_type_id integer references address_type(id)
);
insert into address(id, address, address_type) values(1, 'address somewhere 1', 1);
insert into address(id, address, address_type) values(2, 'address somewhere 2', 1);
insert into address(id, address, address_type) values(3, 'address somewhere 3', 2);
Spring Data JPA Code
@Table(name = "address_type")
public class AddressType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "type")
private String type;
@Column(name = "description")
private String description;
@OneToMany(mappedBy = "addressType")
private Address address;
// Getters and Setters (Lombok)
}
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "type")
private String address;
@Column(name = "address_type_id")
@JoinColumn(---> logic?????????? <-----)
@ManyToOne(---> logic?????????? <-----)
private AddressType addressType;
// Getters and Setters (Lombok)
}
I guess my question is how should I need to setup the logic inside the @JoinColumn
and @ManyToOne
annotations in the Address
entity?
P.D. The Insert should only happen in table address
, Spring Data should only read from table address_type
to get the foreign key to be stored in the address
table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量研究和尝试/错误,我得到了答案。
注释内的配置如下:
在类地址中
然后在地址类型中,您应始终通过参考子表映射一个。
一个重要的要记住的事情是,在保存回数据库之前,您应该在数据库中获取要在子表中引用的实体的数据库,以便Spring Data JPA识别这不是地址类型表中的新记录,但只是参考并将外键保存在地址表中。
I got the answer after a lot of research and try/error.
The configuration inside the annotations are the following:
In class Address
Then in AddressType, you should always have a mapped by reference to the child table.
One IMPORTANT thing to remember, is that before saving back to the database, you should fetch in the database for the entity you want to refer in your child table, so that Spring Data JPA recognize that this is NOT a new record in AddressType table, BUT just a reference and saves the foreign key in the Address table.