如何创建两个实体并将它们关联

时间:2019-07-16 09:47:05

标签: java hibernate spring-boot

首先,为可能出现的语法错误道歉。我的英语不太好。

我正在尝试创建一个实体,并与其他实体建立关联。

这个想法是发送一个json文件并获取一些属性来创建该实体,然后将该实体与另一个实体相关联。但是,我不能因为抛出Exception这样的异常:

  

试图从空的一对一属性中分配ID

因此,在我的SchemeService中,我尝试创建两个实体:

protected Scheme createScheme(final String creatorId, final String name, final String description, final InputStream inputStream) { 

    DeserializeJSONFile desJsonFile = new DeserializeJSONFile();
    desJsonFile.init(inputStream);

    TableEntity table = new TableEntity();
    table.setCreator(creatorId);
    table.setProperties(desJsonFile.getProperties().toString());
    table.setGeometry(desJsonFile.getGeometry().toString());
    createTable(table);

    Scheme scheme = new Scheme();
    scheme.setCreator(creatorId);
    scheme.setName(name);
    scheme.setDescription(description);
    scheme.setTable(table);
    createScheme(scheme);
    return scheme;
}

private void createTable(final TableEntity table) {
    tableDao.create(table);
}

protected void createScheme(final Scheme scheme) {
    schemeDao.create(scheme);
}

这是我的TableEntity:

public class TableEntity extends BaseEntityActivable implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SCHEME_SEQ_GEN")
    @SequenceGenerator(name = "SCHEME_SEQ_GEN", sequenceName = "test_seq_table", allocationSize = 1)
    @Column(name = "table_id")
    private Long tableId;

    @Type(type= "jsonb")
    @Column(name = "properties", columnDefinition = "json")
    private String properties;

    @Type(type= "jsonb")
    @Column(name = "geometry", columnDefinition = "json")
    private String geometry;

    @OneToOne
    @MapsId
    private Scheme scheme;
}

这是我的SchemeEntity:

public class Scheme extends BaseEntityActivable implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SCHEME_SEQ_GEN")
    @SequenceGenerator(name = "SCHEME_SEQ_GEN", sequenceName = "test_seq_scheme", allocationSize = 1)
    @Column(name = "scheme_id")
    private Long schemeId;

    @Column(name = "name", nullable = false)
    @NotEmpty(message = AxisMapsErrorConstants.NAME_CANT_BE_EMPTY)
    private String name;

    @Column(name = "description")
    private String description;

    @OneToOne(mappedBy = "scheme", cascade = CascadeType.ALL)
    @JoinColumn(name = "scheme_id", referencedColumnName = "table_id", foreignKey = @ForeignKey(name = "fk_scheme_table_1"))
    private TableEntity table;

}

这是我的SQL:

create sequence test_seq_table start 1 increment 1;
create sequence test_seq_scheme start 1 increment 1;

create table maps_table (
    table_id int8 not null,
    created_at timestamp not null,
    created_by varchar(255),
    updated_at timestamp,
    updated_by varchar(255),
    is_active boolean not null,
    properties jsonb not null,
    geometry jsonb not null,
    primary key (table_id)
);


create table maps_scheme (
    scheme_id int8 not null,
    created_at timestamp not null,
    created_by varchar(255),
    updated_at timestamp,
    updated_by varchar(255),
    is_active boolean not null,
    description varchar(255),
    name varchar(255) not null,
    table_id int8 not null,
    primary key (scheme_id)
);

    alter table maps_scheme 
       add constraint fk_scheme_table_1 
       foreign key (scheme_id) 
       references maps_table;

2 个答案:

答案 0 :(得分:1)

因为您使用的是@mapsId,这意味着您在与scheme的关系中使用的是相同的标识符,这意味着首先,该方案不应该为空,并且每次刷新对象时都可以作为托管实体使用,这也意味着只能从关系的一侧进行持久化,因为持久化实体时应该可以使用scheme的ID。

我不确定您是否真的需要@mapsId,因为您已经具有双向关系,这意味着无论如何您都可以访问实体的两侧。

我建议在此处删除@mapsId。

答案 1 :(得分:0)

感谢大家的帮助。

这是我的解决方法。

方案:

public class Scheme extends BaseEntityActivable implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SCHEME_SEQ_GEN")
    @SequenceGenerator(name = "SCHEME_SEQ_GEN", sequenceName = "aguas_seq_scheme", allocationSize = 1)
    @Column(name = "scheme_id")
    private Long schemeId;

    @Column(name = "name", nullable = false)
    @NotEmpty(message = AxisMapsErrorConstants.NAME_CANT_BE_EMPTY)
    private String name;

    @Column(name = "description")
    private String description;

    @OneToOne(cascade= { CascadeType.ALL }, fetch = FetchType.LAZY)
    @JoinColumn(name="table_id")
    private TableEntity table;

}

TableEntity:

public class TableEntity extends BaseEntityActivable implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SCHEME_SEQ_GEN")
    @SequenceGenerator(name = "SCHEME_SEQ_GEN", sequenceName = "aguas_seq_table", allocationSize = 1)
    @Column(name = "table_id")
    private Long tableId;


    @Type(type= "jsonb")
    @Column(name = "properties", columnDefinition = "json")
    private String properties;

    @Type(type= "jsonb")
    @Column(name = "geometry", columnDefinition = "json")
    private String geometry;

    @OneToOne(mappedBy= "table")
    private Scheme scheme;
}

SQL:

create sequence aguas_seq_table start 1 increment 1;
create sequence aguas_seq_scheme start 1 increment 1;

create table maps_table (
    table_id int8 not null,
    created_at timestamp not null,
    created_by varchar(255),
    updated_at timestamp,
    updated_by varchar(255),
    is_active boolean not null,
    properties jsonb not null,
    geometry jsonb not null,
    primary key (table_id)
);


create table maps_scheme (
    scheme_id int8 not null,
    created_at timestamp not null,
    created_by varchar(255),
    updated_at timestamp,
    updated_by varchar(255),
    is_active boolean not null,
    description varchar(255),
    name varchar(255) not null,
    table_id int8 not null,
    primary key (scheme_id)
);

SchemeService创建方案和表:

protected Scheme createScheme(final String creatorId, final String name, final String description, final InputStream inputStream) { 

    DeserializeJSONFile desJsonFile = new DeserializeJSONFile();
    desJsonFile.init(inputStream);

    TableEntity table = new TableEntity();
    table.setCreator(creatorId);
    table.setGeometry(desJsonFile.loadGeometries().toString());
    table.setProperties(desJsonFile.loadProperties().toString());
    createTable(table);

    Scheme scheme = new Scheme();
    scheme.setCreator(creatorId);
    scheme.setName(name);
    scheme.setDescription(description);
    scheme.setTable(table);
    createScheme(scheme);
    return scheme;
}