xukongmoji
2011-11-25 12:25
浏览 285
已采纳

1对1主键关联, 使用jpa映射, 添加记录时报org.hibernate.exception.ConstraintViolationException

实体代码:
[code="java"]
package entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="tbl_husband")
public class Husband {
private Integer id;
private String name;
private Wife wife;

@Id @GeneratedValue
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

@OneToOne(cascade={CascadeType.ALL})
@PrimaryKeyJoinColumn
public Wife getWife() {
    return wife;
}
public void setWife(Wife wife) {
    this.wife = wife;
}

}

[/code]

[code="java"]
package entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tbl_wife")
public class Wife {
private Integer id;
private String name;

@Id
@GeneratedValue
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

[/code]

测试代码:
[code="java"]
@Test
public void savehuswife(){
Husband husband = new Husband();
husband.setName("zhangsan");

    Wife wife = new Wife();
    wife.setName("husi");

    husband.setWife(wife);

    session.save(husband);
}

[/code]
报错:
[code="java"]
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at entity.TestOne2OneUniPK.after(TestOne2OneUniPK.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.BatchUpdateException: ORA-02291: 违反完整约束条件 (HIB.FK60DE70BA49A57B77) - 未找到父项关键字

at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:342)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10656)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 31 more

[/code]

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • 爱上一条鱼 2011-11-25 12:47
    已采纳
    已采纳该答案
    打赏 评论
  • 爱上一条鱼 2011-11-25 12:33

    [code="java"]@Test
    public void savehuswife(){
    Husband husband = new Husband();
    husband.setName("zhangsan");

        Wife wife = new Wife();
        wife.setName("husi");
    
        husband.setWife(wife);
    
        session.save(husband);
    }[/code]
    

    违反约束嚒,你的wife在wife表里还不存在值,不能插入嚒。。。。。。。

    打赏 评论
  • wangyijiangshui 2011-11-25 12:34

    先保存wife,在保存husband

    打赏 评论
  • 爱上一条鱼 2011-11-25 12:48

    [code="java"] @PrimaryKeyJoinColumn [/code]
    你用的关联是共享主键的方式,不是设置外键的方式

    打赏 评论
  • wangyijiangshui 2011-11-25 12:51

    JPA里需要你在Unit里去声明Mapped的Cascade操作
    你不声明的话应该会有默认的Cascade操作模式,因为我不确定各种JPA实现是否一致,我一般都主动声明。

    打赏 评论

相关推荐 更多相似问题