Hibernate以错误的顺序生成查询,同时使用多余的列保存多对多关系

时间:2018-08-24 12:39:19

标签: java hibernate

我正在将Hibernate 5.0.12与Spring Framework 4.3.10一起使用。我的项目有两个带有多列关系的实体。保存其中一个实体后,Hibernate会以错误的顺序生成sql查询。导致ORA-02291错误。

我的实体:

SignedDocument.java

@Entity
@Table(name="SIGNED_DOCUMENTS", schema="KTS_OWNER")
@Data @EqualsAndHashCode(exclude={"signers"})
public class SignedDocument implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private long id;
    @Column(name="file_name", nullable = false)
    private String fileName;
    @Column(name="file_content", nullable = false)
    @Lob
    private byte[] fileContent;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="id.doc")
    private Set<SIGNED_DOCUMENTS_SIGNERS> signers;
}

Signer.java

@Entity
@Table(name="SIGNERS", schema="KTS_OWNER")
@Data @EqualsAndHashCode(exclude={"docs"})
public class Signer implements Serializable {

    @Id
    @Column(name="id")
    private long id;
    @Column(name="first_name", nullable = false)
    private String firstName;


    @OneToMany(mappedBy="id.signer")
    private Set<SIGNED_DOCUMENTS_SIGNERS> docs;

}

以及它们之间的关联表。

@Entity
@Table(name="SIGNED_DOCUMENTS_SIGNERS", schema="KTS_OWNER")
@Data
public class SIGNED_DOCUMENTS_SIGNERS implements Serializable {

    @EmbeddedId
    private SIGNED_DOCUMENTS_SIGNERS_PK id;

    @Column(name="sign_order", nullable = false)
    private long signOrder;
    @Column(name="esign")
    private byte[] esign;

}

主键为

@Embeddable
@Data public class SIGNED_DOCUMENTS_SIGNERS_PK implements Serializable{
    @ManyToOne(fetch=FetchType.LAZY)
    private SignedDocument doc;

    @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private Signer signer;
}

休眠输出:

2018-08-24 15:30:22.577 DEBUG 8972 --- [nio-8080-exec-3] org.hibernate.SQL                        : insert into kts_owner.signed_documents (file_name, id, file_content) values (?, ?, ?)
Hibernate: insert into kts_owner.signed_documents (file_name, id, file_content) values (?, ?, ?)
2018-08-24 15:30:22.708 DEBUG 8972 --- [nio-8080-exec-3] org.hibernate.SQL                        : insert into kts_owner.signed_documents_signers (esign, sign_order, doc_id, signer_id) values (?, ?, ?, ?)
Hibernate: insert into kts_owner.signed_documents_signers (esign, sign_order, doc_id, signer_id) values (?, ?, ?, ?)
2018-08-24 15:30:22.735 DEBUG 8972 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : could not execute statement [n/a]

java.sql.SQLIntegrityConstraintViolationException: ORA-02291: ...

0 个答案:

没有答案