使用 entityManager 将其中一个表中的 FK 保存到两个表中
Use entityManager to save into two tables with FK in one of the tables
我必须使用 EntityManager 将第一个持久实体的 ID 插入 table。
我的实体有一个像这样生成的 ID
@Id
@Column(name = "PUSH_ID", nullable = false)
@SequenceGenerator(name = "dbSequence", sequenceName = "VVV_PUSH_S", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dbSequence")
public Integer getIdentifier() {
return identifier;
}
然后我保留这个实体以生成 ID
this.getEntityManager().persist(pushEntity);
this.getEntityManager().flush();
我将 pushEntity.getIdentifier() 传递给 sql 语句以插入到第二个 table 中,像这样
selectQueryToInsert.addJoin(PUSH_SOURCE_TABLE, SQLJoinOperator.LEFT_JOIN, PUSH_SOURCE_TABLE + "." + SOURCE_ID_COLUMN + "=" + sourceTableName + "." + DEFAULT_ID_COLUMN + " AND " + PUSH_SOURCE_TABLE + "." + PUSH_ID_COLUMN + "=" + String.valueOf(pushEntity.getIdentifier));
selectQueryToInsert.addListValuesRestriction(sourceTableName, DEFAULT_ID_COLUMN, SQLOperator.IN, sourceList);
selectQueryToInsert.addValueRestriction(PUSH_SOURCE_TABLE, PUSH_ID_COLUMN, SQLOperator.NULL, null);
selectQueryToInsert.addSelectColumn(DEFAULT_ID_COLUMN);
String insertQuery = INSERT_SOURCE_QUERY + stringBuilder.toString(selectQueryToInsert);
this.getEntityManager().createNativeQuery(insertQuery).executeUpdate();
技巧来了,在第二个 table "PUSH_SOURCE_TABLE" 中,我对推送的 ID 有一个 FK,并且由于 entityManager 没有提交事务,所以 PUSH_ID数据库中尚不存在,第二次插入失败并显示
integrity constraint (%s.%s) violated - parent key not found
有没有其他方法可以做到这一点。 Tkx
我通过为 PUSH_SOURCE_TABLE 创建一个新实体而不是使用 preparedStatement
解决了这个问题
并设置一个 @Embeddable class 像这样保存推送实体的父密钥
@Embeddable
public class PushEntityPK implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5599662020926504220L;
private Integer push_id;
private String entity_id;
//Default Constructor
public PushEntityPK(){
}
public PushEntityPK(Integer push_id, String entity_id){
this.push_id = push_id;
this.entity_id = entity_id;
}
并使用 @Idclass
注释源 table 实体
@Entity
@Table(name = "PUSH_SOURCE_TABLE")
@IdClass(PushEntityPK.class)
public class PushSourceEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5162782386822573902L;
public PushSourceEntity(PushEntityPK pushEntityPK){
this.push_id = pushEntityPK.getPush_id();
this.entity_id = pushEntityPK.getSource_name();
}
@Id
@AttributeOverrides({@AttributeOverride(name = "push_id", column = @Column(name = "PUSH_ID"))})
private Integer push_id;
@AttributeOverrides({@AttributeOverride(name = "entity_id", column = @Column(name = "ENTITY_ID"))})
private String entity_id;
我必须使用 EntityManager 将第一个持久实体的 ID 插入 table。
我的实体有一个像这样生成的 ID
@Id
@Column(name = "PUSH_ID", nullable = false)
@SequenceGenerator(name = "dbSequence", sequenceName = "VVV_PUSH_S", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dbSequence")
public Integer getIdentifier() {
return identifier;
}
然后我保留这个实体以生成 ID
this.getEntityManager().persist(pushEntity);
this.getEntityManager().flush();
我将 pushEntity.getIdentifier() 传递给 sql 语句以插入到第二个 table 中,像这样
selectQueryToInsert.addJoin(PUSH_SOURCE_TABLE, SQLJoinOperator.LEFT_JOIN, PUSH_SOURCE_TABLE + "." + SOURCE_ID_COLUMN + "=" + sourceTableName + "." + DEFAULT_ID_COLUMN + " AND " + PUSH_SOURCE_TABLE + "." + PUSH_ID_COLUMN + "=" + String.valueOf(pushEntity.getIdentifier));
selectQueryToInsert.addListValuesRestriction(sourceTableName, DEFAULT_ID_COLUMN, SQLOperator.IN, sourceList);
selectQueryToInsert.addValueRestriction(PUSH_SOURCE_TABLE, PUSH_ID_COLUMN, SQLOperator.NULL, null);
selectQueryToInsert.addSelectColumn(DEFAULT_ID_COLUMN);
String insertQuery = INSERT_SOURCE_QUERY + stringBuilder.toString(selectQueryToInsert);
this.getEntityManager().createNativeQuery(insertQuery).executeUpdate();
技巧来了,在第二个 table "PUSH_SOURCE_TABLE" 中,我对推送的 ID 有一个 FK,并且由于 entityManager 没有提交事务,所以 PUSH_ID数据库中尚不存在,第二次插入失败并显示
integrity constraint (%s.%s) violated - parent key not found
有没有其他方法可以做到这一点。 Tkx
我通过为 PUSH_SOURCE_TABLE 创建一个新实体而不是使用 preparedStatement
解决了这个问题并设置一个 @Embeddable class 像这样保存推送实体的父密钥
@Embeddable
public class PushEntityPK implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5599662020926504220L;
private Integer push_id;
private String entity_id;
//Default Constructor
public PushEntityPK(){
}
public PushEntityPK(Integer push_id, String entity_id){
this.push_id = push_id;
this.entity_id = entity_id;
}
并使用 @Idclass
注释源 table 实体@Entity
@Table(name = "PUSH_SOURCE_TABLE")
@IdClass(PushEntityPK.class)
public class PushSourceEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5162782386822573902L;
public PushSourceEntity(PushEntityPK pushEntityPK){
this.push_id = pushEntityPK.getPush_id();
this.entity_id = pushEntityPK.getSource_name();
}
@Id
@AttributeOverrides({@AttributeOverride(name = "push_id", column = @Column(name = "PUSH_ID"))})
private Integer push_id;
@AttributeOverrides({@AttributeOverride(name = "entity_id", column = @Column(name = "ENTITY_ID"))})
private String entity_id;