简体   繁体   English

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

[英]How to create two entities and relate them

First of all, apologize for the grammatical errors that you can make.首先,为您可能犯的语法错误道歉。 My English is not very good.我的英文不是很好。

I'm trying to create dinamically an Entity and relathion with other Entity.我正在尝试创建一个实体并与其他实体建立关系。

The idea is send a json file and get some properties to create that Entity, later associate that entity with the other.这个想法是发送一个 json 文件并获取一些属性来创建该实体,然后将该实体与另一个相关联。 However, I can't because throw Exception like:但是,我不能因为抛出异常,例如:

attempted to assign id from null one-to-one property试图从空一对一属性分配 id

So, here in my SchemeService I try to create both entities:因此,在我的 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);
}

Here is my TableEntity:这是我的 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;
}

Here is my SchemeEntity:这是我的 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;

}

Here is my sql:这是我的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;

since you are using @mapsId this means you are using the same identifier in your relation with scheme, which means first the scheme should not be nullable and it should be available as managed entity each time your object is flushed, which also means that you can do the persistence only from one side of the relation since the id of scheme should be available when persisting your entity.由于您使用的是 @mapsId,这意味着您在与方案的关系中使用相同的标识符,这意味着首先方案不应为空,并且每次刷新对象时它都应作为托管实体可用,这也意味着您可以仅从关系的一侧进行持久化,因为在持久化实体时方案的 id 应该可用。

I am not sure if you really need @mapsId here, since you already have a bidirectional relation which means anyway you will be able to access both sides of your entity.我不确定您是否真的需要 @mapsId ,因为您已经有了双向关系,这意味着无论如何您都可以访问实体的两侧。

I would suggest to remove @mapsId here.我建议在这里删除@mapsId。

Thanks everyone for helping me.谢谢大家帮助我。

This is my solution.这是我的解决方案。

Scheme:方案:

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 to create scheme and table: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM