我为@OneToMany
关系创建了一个新表。以下是用于创建新表的liquibase脚本。新表包含其他两个表的ID。
<changeSet id="1" author="XXX">
<comment>create table for UEN-ORG_C</comment>
<createTable tableName="UEN_ORG_C">
<column name="ORG_DTLS_ID_N" type="BIGINT">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="ORG_CODE_ID_N" type="BIGINT">
<constraints nullable="false" />
</column>
</createTable>
<addForeignKeyConstraint
constraintName="FK_ORG_DTLS"
referencedTableName="ORG_DTLS"
baseColumnNames="ORG_DTLS_ID_N"
baseTableName="UEN_ORG_C"
referencedColumnNames="ID_N" />
<addForeignKeyConstraint
constraintName="FK_ORG_CODE"
referencedTableName="ORG_CODE"
baseColumnNames="ORG_CODE_ID_N"
baseTableName="UEN_ORG_C"
referencedColumnNames="ID_N" />
</changeSet>
下面是实体类:
@Data
@Entity
@EqualsAndHashCode(of = {"id"})
@Table(name = "ORG_DTLS")
public class OrganizationDetails implements BaseEntity<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_N")
private Long id;
@OneToMany
@JoinTable(name = "UEN_ORG_C",
joinColumns = {@JoinColumn(name = "ORG_DTLS_ID_N")},
inverseJoinColumns = {@JoinColumn(name = "ORG_CODE_ID_N")})
private List<OrgCode> orgCodes;
}
在保存ORG_DTLS的同时,我应该能够选择多个ORG_CODES,并且在创建ORG_DTLS时可以选择相同的ORG_CODES
示例:
ORG_DTLS_ID_N ORG_CODE_ID_N
82 21
83 21
84 21
85 25
86 25
87 25
88 25
当我尝试将两个表的ID保存到具有单向关系的新表中时。保存数据时出现异常
ChangeEntityCommand ID: 48ab032d-c226-4d5a-af8a-38bca7b79735
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] DEBUG org.hibernate.SQL - insert into uen_org_c (org_dtls_id_n, org_code_id_n) values (?, ?)
Hibernate: insert into uen_org_c (org_dtls_id_n, org_code_id_n) values (?, ?)
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [128]
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [31]
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] DEBUG org.hibernate.SQL - insert into uen_org_c (org_dtls_id_n, org_code_id_n) values (?, ?)
Hibernate: insert into uen_org_c (org_dtls_id_n, org_code_id_n) values (?, ?)
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [128]
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [30]
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1, SQLState: 23000
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-00001: unique constraint (AUTHENTICATION_SERVICE.PK_UEN_ORG_C) violated
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] INFO o.h.e.j.b.internal.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] ERROR o.h.i.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] INFO s.g.m.m.c.f.UserSessionInterceptor - request and response is completed
2019.11.30;12:17:09 [http-nio-8092-exec-1] [rootId: parentId: eventId: ] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] oracle.jdbc.OracleDatabaseException: ORA-00001: unique constraint (AUTHENTICATION_SERVICE.PK_UEN_ORG_C) violated