弹簧数据JPA(Postgres) - 插入一张表格,从包含静态数据的表中读取外键

发布于 2025-01-22 05:15:42 字数 2238 浏览 1 评论 0原文

我目前正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2025-01-29 05:15:42

经过大量研究和尝试/错误,我得到了答案。

注释内的配置如下:

在类地址中

    @Column(name = "address_type_id")
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "address_type_id", referencedColumnName = "id")
    private AddressType addressType;

然后在地址类型中,您应始终通过参考子表映射一个。

   @OneToMany(mappedBy = "addressType")
   private Address address;

一个重要的要记住的事情是,在保存回数据库之前,您应该在数据库中获取要在子表中引用的实体的数据库,以便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

    @Column(name = "address_type_id")
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "address_type_id", referencedColumnName = "id")
    private AddressType addressType;

Then in AddressType, you should always have a mapped by reference to the child table.

   @OneToMany(mappedBy = "addressType")
   private Address address;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文