staff表主码为ID,引用department表的DEPARTMENT_ID作为外码,向staff中插入数据时,报异常为sequence不存在,但是NEWID这个sequence我是建了的。怎么回事呢,hibernate配置文件如下
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.liyanframework.demo.domain">
<class name="Staff" table="STAFF">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="NEWID"></param>
</generator>
</id>
<property name="name" column="NAME" type="string" />
<property name="age" column="AGE" type="integer" />
<property name="birthday" column="BIRTHDAY" type="date" />
<property name="salary" column="SALARY" type="big_decimal" />
<property name="level" column="LEVELNESS" type="float" />
<property name="createTime" column="CREATETIME" type="timestamp" />
<property name="enable" column="ENABLE" type="character" />
<property name="status" column="STATUS" type="string" />
<many-to-one name="department" column="DEPARTMENT_ID" class="Department" />
</class>
</hibernate-mapping>
另外想要设置DEPARTMENT_ID 的值是不是这样:
Staff staff = new Staff();
Department department = new Department ();
department.setDepartmentId(XXX);
staff.setDepartment(department);
我觉得有可能就是这几行代码导致的错误。。。。
------解决方案--------------------
如果你打算用手工设置的方式来控制主键:
department.setDepartmentId(XXX);
就不要设置sequence了,应该是assigned
------解决方案--------------------
你可以确认一下你的sequence是否已经启用,或者直接在数据库下执行一下select NEWID.nextval from dual看是否可以正常获取sequence值
------解决方案--------------------
<param name="NEWID"></param>
这里 错了,应该是:
<param name="sequence">ID</param>
name指定的是 主键的生成策略,具体值放 后面即可。