Hibernate使用外键一对一映射ERROR: 不能将值 NULL 插入列 '',表 '';列不允许有 Null 值。INSERT 失败。

woaisinile 发布于 2016/01/10 17:42
阅读 1K+
收藏 0

hibernate使用外键一对一映射中出现错误ERROR: 不能将值 NULL 插入列 'DEPT_ID',表 'test.dbo.DEPARTMENTS';列不允许有 Null 值。INSERT 失败。 

目录结构:

配置文件:hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

    <!-- 配置连接数据库的基本信息 -->

    <property name="connection.username">sa</property>

    <property name="connection.password">131420</property>

    <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=test;SelectMethod=cursor;</property>

    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>

    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> 

    <property name="show_sql">true</property>

    <property name="format_sql">true</property>  

    <!-- 设置hibernate的事务隔离级别 -->

    <property name="connection.isolation">2</property>

    <!-- 删除对象后,使其OID置为null -->

    <property name="use_identifier_rollback">true</property>

    <!-- 指定自动生成数据表的策略,即当我们运行程序时hibernate帮助我们在数据库中自动生成数据表 -->

<property name="hbm2ddl.auto">update</property>

    <!-- 配置c3p0数据源 -->

    <property name="hibernate.c3p0.max_size">10</property>

    <property name="hibernate.c3p0.min_size">5</property>

    <property name="c3p0.acquire_increment">2</property>

    <property name="c3p0.idle_test_period">2000</property>

    <property name="c3p0.timeout">2000</property>

    <property name="c3p0.max_statements">10</property>


    <property name="hibernate.jdbc.fetch_size">100</property>

    <property name="hibernate.jdbc.batch_size">30</property>

    <mapping resource="com/liufuchen/hibernate/one2one/foreign/Manager.hbm.xml"/>

    <mapping resource="com/liufuchen/hibernate/one2one/foreign/Department.hbm.xml"/>



实体类:

Manager.java

package com.liufuchen.hibernate.one2one.foreign;

public class Manager {

private Integer mgrId;

private String mgrName;

private Department dept;

public Integer getMgrId() {

return mgrId;

}

public void setMgrId(Integer mgrId) {

this.mgrId = mgrId;

}

public String getMgrName() {

return mgrName;

}

public void setMgrName(String mgrName) {

this.mgrName = mgrName;

}

public Department getDept() {

return dept;

}

public void setDept(Department dept) {

this.dept = dept;

}

}


Departmen.java

package com.liufuchen.hibernate.one2one.foreign;

public class Department {

private Integer deptId;

private String deptName;

private Manager mgr;

public Integer getDeptId() {

return deptId;

}

public void setDeptId(Integer deptId) {

this.deptId = deptId;

}

public String getDeptName() {

return deptName;

}

public void setDeptName(String deptName) {

this.deptName = deptName;

}

public Manager getMgr() {

return mgr;

}


public void setMgr(Manager mgr) {

this.mgr = mgr;

}

}



映射文件:

Manager.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2016-1-8 19:59:21 by Hibernate Tools 3.5.0.Final -->

<hibernate-mapping package="com.liufuchen.hibernate.one2one.foreign">

    <class name="Manager" table="MANAGERS">

        <id name="mgrId" type="java.lang.Integer">

            <column name="MGR_ID" />

            <generator class="native" />

        </id>

        

        <property name="mgrName" type="java.lang.String">

            <column name="MGR_NAME" />

        </property>


        <one-to-one name="dept" class="Department" property-ref="mgr"></one-to-one>  

    </class>

</hibernate-mapping>



Department.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2016-1-8 19:59:21 by Hibernate Tools 3.5.0.Final -->

<hibernate-mapping package="com.liufuchen.hibernate.one2one.foreign">


    <class name="Department" table="DEPARTMENTS">

    

        <id name="deptId" type="java.lang.Integer">

            <column name="DEPT_ID" />

            <generator class="native" />

        </id>

        

        <property name="deptName" type="java.lang.String">

            <column name="DEPT_NAME" />

        </property>

       <many-to-one name="mgr" class="Manager"  column="MGR_ID" unique="true"></many-to-one>

       

    </class>

</hibernate-mapping>

加载中
返回顶部
顶部