weixin_42332083
2009-11-13 11:53
浏览 233
已采纳

hibernate中一对一外键双向关联插入时违反非空约束如何解决?

作了个测试,一直都是违反非空约束,请帮忙看一下。
我的两张表wife和husband都是有外键参照对方主键,而且外键都有非空约束。建立起的是基于外键一对一双向关联,但我在向表中插入Wife对象时,显示husband_id违反非空约束。请问这应该怎么解决呢?

异常如下:
[code="java"]org.hibernate.exception.ConstraintViolationException: could not insert: [com.zyh.hibernate.domain.Wife]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.zyh.hibernate.persist.BaseDaoImpl.insert(BaseDaoImpl.java:39)
at com.zyh.hibernate.web.TestOne2One.test1(TestOne2One.java:37)
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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: Column 'husband_id' cannot be null
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1125)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:677)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1357)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1274)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1259)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 37 more
[/code]

两个实体类如下(get和set方法就不贴了):
[code="java"]public class Husband {

private Integer id;
private String name;
private Date birth;
private Wife wife;

}

public class Wife{

private Integer id;
private String name;
private Date birth;
private Husband husband;

}[/code]

husband.hbm.xml:

[code="java"]









[/code]

wife.hbm.xml:
[code="java"]






column="husband_id" unique="true" />

[/code]

测试代码:

[code="java"]@Test
public void test1() {

    Husband h = new Husband();
    h.setName("张三");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Wife w = new Wife();
    w.setName("李丽");
    try {
        h.setBirth(sdf.parse("1986-11-28"));
        w.setBirth(sdf.parse("1988-03-15"));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //h.setWife(w);

    //new HusbandDaoImpl().insert(h);

    w.setHusband(h);
    new WifeDaoImpl().insert(w);
}[/code]

[b]问题补充:[/b]
感谢回答,可是one-to-one结点里并没有inverse属性啊!

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

5条回答 默认 最新

  • Ryankay 2009-11-16 12:30
    已采纳

    [quote]
    你这应该是基于主键关联的一对一吧,可我做的是基于外键的啊!我的wife和husband两张表的id都是独立的,如果不改变表结构,有办法解决吗?
    另外,基于外键的一对一,不也是一对多的一个特例吗?
    [/quote]

    收到你短信了,还是发这里吧。
    ID独立是一样的,我使用ID作为外键只不过是为了节省列,你要额外单独提供一个列的话一样的道理。你只需要两边都配one-to-one,many-to-one里通过column来指定外键列,one-to-one的话要用foreign-key=""。填上对应的外键列即可,比如在wife里填husband,husband里填wife。

    这种方式我以前好像也用过,有些印象,你可以试试。

    点赞 评论
  • liuiyu220 2009-11-13 12:22

    你在husbend.hbm.xml中修改 ,添加inverse="true".不要让两张表都维护外键。

    点赞 评论
  • liuiyu220 2009-11-13 14:02

    那wife.hbm.xml中应该有inverse属性吧。你改下试一下吧。

    点赞 评论
  • Ryankay 2009-11-13 16:20

    你的配置有问题。
    第一:
    一对一不是这样配的。
    既然有了one-to-one,你的many-to-one就是多余的,还有需要设置主控方,谁是主要的,谁是次要的。这里以husband做主控方为例子。

    正确配置:

    [code="xml"]











      1. [/code]

    [code="xml"]



    1. husband


    2. <one-to-one name="husband" class="com.zyh.hibernate.domain.Husband"
    3. constrained="true" access="field" />


      1. [/code] [quote]
    4. @Test
    5. public void test1() {

    6. Husband h = new Husband();
    7. h.setName("张三");
    8. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    9. Wife w = new Wife();
    10. w.setName("李丽");
      1. try {
      2. h.setBirth(sdf.parse("1986-11-28"));
      3. w.setBirth(sdf.parse("1988-03-15"));
      4. } catch (ParseException e) {
      5. // TODO Auto-generated catch block
      6. e.printStackTrace();
      7. }

      8. //h.setWife(w);

      9. //new HusbandDaoImpl().insert(h);

      10. h.setWife(w); w.setHusband(h);// 两边都要set
      11. new HusbandDaoImpl().insert(h);// husband是主控制方
      12. }
        [/quote]

    核心代码:


    husband

    将ID作为外键主键,引用husband的ID。

    点赞 评论
  • temp20100809 2009-11-17 14:47

    你husband.hbm.xml中的元素的property-ref属性的值应该是husband才对,这才表明是wife的husband_id对应husband的husband_id,你现在配的就成了wife的wife_id对应husband的husband_id,你自己想下,是不是这意思。

    点赞 评论

相关推荐 更多相似问题