与Postgres序列保持一对一的关系

时间:2015-08-30 07:08:30

标签: postgresql jpa

我在几个月的空闲时间里和JavaEE一起玩游戏

我在Postgres中有2个表

表人

create table person(id bigint primary key default nextval('person_id_seq'), persontype varchar(5), name varchar(50), ERP_id smallint);

表格地址

create table address (customer_id bigint primary key NOT NULL references person(id), department varchar(50), street varchar(50), plz varchar(10), city varchar(50), country varchar(50));

模范人

    public class Person implements Serializable {

        @Id
        @SequenceGenerator(
                name="person_id_seq",
                sequenceName="person_id_seq",
                allocationSize=1,
                initialValue=1
        )
        @GeneratedValue(
                strategy=GenerationType.AUTO,
                generator="person_id_seq"
        )
        private Long            id;

        private char            personType;

        private String          name;

        private Long            erp_id;

        @OneToOne (cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
        @JoinColumn(name="id", insertable=false, updatable=false)
        private Address address;

[...]    

    @PrePersist
    public void initializeCustomerId(){
        this.address.setCustomerId(id);
        }

模型地址

public class Address implements Serializable {

    @Id
    @GeneratedValue(
            strategy=GenerationType.AUTO)
    @Column(name="person_id")
    private Long    personId;
    private String  department;
    private String  street;
    private int     plz;
    private String  city;
    private String  country;

我从https://blogs.oracle.com/WebLogicServer/entry/ejb3_mapping_of_one-to-one_rel

获得了这个解决方案

我的问题现在......在我坚持之前,我需要由Postgres-Sequence生成的id。

我对此的想法:在@PrePersist中,我可以做一个

select nextval('person_id_seq');

但是如果有更多的客户想同时保存怎么办!!

那么,我怎么能解决这个问题呢?或者我完全错了吗?

由于

Paddie

0 个答案:

没有答案