Hibernate外键错误

时间:2016-06-08 13:44:47

标签: java sql oracle hibernate foreign-keys

因此,在我的Oracle数据库中,我有两个表,它们正确定义了外键约束。

以下是两个表的DDL。

CREATE TABLE "BI***********"."PROJECT" 
   (    
        "PROJECT_ID" NUMBER(10,0) NOT NULL, 
        "PROJECT_CODE" VARCHAR2(20) NOT NULL, 
        "PRODUCT_ID" NUMBER(10,0) NOT NULL, 
        "DESCRIPTION" VARCHAR2(45) NOT NULL,
        CONSTRAINT PROJECT_PK PRIMARY KEY ("PROJECT_ID"),
        CONSTRAINT "PROJECT_FK1" FOREIGN KEY ("PRODUCT_ID")
            REFERENCES "BI***********"."PRODUCT" ("PRODUCT_ID")
   );
CREATE TABLE "BI***********"."PRODUCT" 
   (    
        "PRODUCT_ID" NUMBER(10,0) NOT NULL, 
        "PRODUCT_NAME" VARCHAR2(20) NOT NULL, 
        "DESCRIPTION" VARCHAR2(45) NOT NULL,
        CONSTRAINT PRODUCT_PK PRIMARY KEY ("PRODUCT_ID")
   );

这是带有Hibernate Annotations的相应Java代码:

@Entity
@Table (name="project")
@SequenceGenerator(name="seq_project",sequenceName="BI***********.SEQ_PROJECT", allocationSize=1, initialValue=1)
public class Project {

    //Fields
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_project")
    @Column(name="PROJECT_ID")
    private int id;
    @Column(name="PROJECT_CODE")
    private String projectCode;
    @Column(name="PRODUCT_ID")
    private int productId;
    @Column(name="DESCRIPTION")
    private String description;

@Entity
@Table (name="product")
@SequenceGenerator(name="seq_product",sequenceName="BI***********.SEQ_PRODUCT", allocationSize=1, initialValue=1)
public class Product {

    //Fields
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_product")
    @Column(name="PRODUCT_ID")
    private int id;
    @Column(name="PRODUCT_NAME")
    private String productName;
    @Column(name="DESCRIPTION")
    private String description;

但是,当我尝试使用hibernate添加到表中时,我收到以下错误代码:

Hibernate: insert into project (DESCRIPTION, PRODUCT_ID, PROJECT_CODE, PROJECT_ID) values (?, ?, ?, ?)
Jun 08, 2016 9:06:49 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 2291, SQLState: 23000
Jun 08, 2016 9:06:49 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-02291: integrity constraint (BIMB2013WMMEE.PROJECT_FK1) violated - parent key not found

Jun 08, 2016 9:06:49 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Jun 08, 2016 9:06:49 AM org.hibernate.internal.SessionImpl$5 mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [could not execute statement]
Jun 08, 2016 9:06:49 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:oracle:thin:@endeavour.us.manh.com:1523/pso11r2f]
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2921)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3421)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
    at com.luv2code.demo.CreateClientDemo.main(CreateClientDemo.java:37)
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (BIMB2013WMMEE.PROJECT_FK1) violated - parent key not found

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)

3 个答案:

答案 0 :(得分:1)

查看您的错误

Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (BIMB2013WMMEE.PROJECT_FK1) violated - parent key not found

你的外键"项目"引用"产品"中的主键那不存在。你必须在你的表之间建立链接

您必须更改类似的注释

@Entity
@Table (name="project")
@SequenceGenerator(name="seq_project",sequenceName="BI***********.SEQ_PROJECT", allocationSize=1, initialValue=1)
public class Project {

//Fields
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,  generator="seq_project")
@Column(name="PROJECT_ID")
private int id;
@Column(name="PROJECT_CODE")
private String projectCode;
@OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name="PRODUCT_ID",insertable=true,
updatable=true,nullable=false,unique=true)
private Product product;
@Column(name="DESCRIPTION")
private String description;

@Entity
@Table (name="product")
@SequenceGenerator(name="seq_product",sequenceName="BI***********.SEQ_PRODUCT", allocationSize=1, initialValue=1)
public class Product {

//Fields
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_product")
@Column(name="PRODUCT_ID")
private int id;
@Column(name="PRODUCT_NAME")
private String productName;
@Column(name="DESCRIPTION")
private String description
@OneToOne (mappedBy="PRODUCT_ID",fetch=FetchType.EAGER)
private Project project;

或者您可以使用@PrimaryKeyJoinColumn注释

@PrimaryKeyJoinColumn
private Project project;

您可以找到有关关系here

的更多信息

答案 1 :(得分:0)

我的猜测是你需要将Product类提供给Project类,而不是现在使用的productId Integer。

CONSTRAINT "PROJECT_FK1" FOREIGN KEY ("PRODUCT_ID")
REFERENCES "BI***********"."PRODUCT" ("PRODUCT_ID")

外键表示两个表之间存在引用,而在代码中只传递整数,而不是完整的Product类作为对象

因此我认为你需要替换:

@Column(name="PRODUCT_ID")
private int productId;
在您的Project类中使用以下代码

@Column(name="PRODUCT_ID")
@OneToOne
private Product product;

答案 2 :(得分:0)

cfg.xml文件中有错误。

中将hbm2dll更改为hbm2ddl

" hibernate.hbm2dll.auto">创建

然后生活将再次美丽。我知道这是因为我犯了同样的错误,并花了几个小时来搞清楚。