日期:2014-05-17  浏览次数:20927 次

多对一关于插入空值
假如我现在有用户类和部门类
用户类
Java code
private int userId;
private String userCode;
private String userName;
private String password;
private EP_Depart deptNo;


部门类
Java code
private int deptID;
    private String deptName;
    private String parentID;
    private String remarks;
    private Set<EP_User> sysUsers = new HashSet<EP_User>();


HBM文件
XML code

        <id name="userId" type="java.lang.Integer">
            <generator class="identity" /> 
        </id>
        
        <property name="userCode" type="java.lang.String"></property>
        
        <property name="userName" type="java.lang.String"></property>
        
        <property name="password" type="java.lang.String"></property>
        
        <many-to-one name="deptNo" class="com.xxtw.persit.EP_Depart" lazy="false" not-found="ignore"></many-to-one>



XML code

       <id name="DeptID" type="java.lang.Integer">
            <generator class="identity" /> 
        </id>
        
        <property name="DeptName" type="java.lang.String"></property>
        
        <property name="ParentID" type="java.lang.String"></property>
        
        <property name="Remarks" type="java.lang.String"></property>
        
        <set name="sysUsers" inverse="true" lazy="false">
            <key column="deptNo" not-null="false"></key>
            <one-to-many class="com.xxtw.persit.EP_User"/>
        </set>


在增加用户的时候部门是下拉框的形式,如果是新建用户以空显示第一项
HTML code

<option value="" selected="selected">&nbsp;</option>


这个时候我把用户的其他基本信息都输入好,不选择部门直接保存报错
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xxtw.persit.EP_Depart
org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:248)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:258)
org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:619)
org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3141)
org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:501)
org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:227)
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:150)
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
org.hi