[英]How to create two entities and relate them
首先,為您可能犯的語法錯誤道歉。 我的英文不是很好。
我正在嘗試創建一個實體並與其他實體建立關系。
這個想法是發送一個 json 文件並獲取一些屬性來創建該實體,然后將該實體與另一個相關聯。 但是,我不能因為拋出異常,例如:
試圖從空一對一屬性分配 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;
由於您使用的是 @mapsId,這意味着您在與方案的關系中使用相同的標識符,這意味着首先方案不應為空,並且每次刷新對象時它都應作為托管實體可用,這也意味着您可以僅從關系的一側進行持久化,因為在持久化實體時方案的 id 應該可用。
我不確定您是否真的需要 @mapsId ,因為您已經有了雙向關系,這意味着無論如何您都可以訪問實體的兩側。
我建議在這里刪除@mapsId。
謝謝大家幫助我。
這是我的解決方案。
方案:
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;
}
表實體:
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;
}
查詢語句:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.