照着例子做一个级联插入的DEMO不过现在有问题,操作后显示的HQL语句只有SELECT 而没有INSERT是怎么回事呢?
代码:
-----------------------------------------------------------------------------------------------
private static void demo2()
{
                configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
myTrans =  session.beginTransaction();

String hql = "from com.miko.dao.TUser as user where user.name='goo'";
Query query = session.createQuery(hql);
List userList = query.list();
tUser = (TUser)userList.get(0);
tAddress = new TAddress();
tAddress.setAddress("abcdefg");
tAddress.setTel("11111");
tAddress.setTUser(tUser);
tAddress.setZipcode("211111");
tUser.getTAddresses().add(tAddress);
session.save(tUser);
myTrans.commit();
System.out.println("save 2 ok!");
}-----------------------------------------------------------------------------------------------
TUser.hbm.xml:
-----------------------------------------------------------------------------------------------
<hibernate-mapping>
    <class name="com.miko.dao.TUser" table="t_user" schema="dbo" catalog="HibernateDemoDB">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
        <property name="groupId" type="java.lang.Integer">
            <column name="group_id" />
        </property>
        <property name="userType" type="java.lang.String">
            <column name="user_type" length="20" />
        </property>
        <set name="TAddresses" inverse="true">
            <key>
                <column name="user_id" not-null="true" />
            </key>
            <one-to-many class="com.miko.dao.TAddress"/>
        </set>
    </class>
</hibernate-mapping>
-----------------------------------------------------------------------------------------------
TAddress.hbm.xml
-----------------------------------------------------------------------------------------------
<hibernate-mapping>
    <class name="com.miko.dao.TAddress" table="t_address" schema="dbo" catalog="HibernateDemoDB">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="TUser" class="com.miko.dao.TUser" fetch="select" cascade="all">
            <column name="user_id" not-null="true" />
        </many-to-one>
        <property name="address" type="java.lang.String">
            <column name="address" length="50" />
        </property>
        <property name="zipcode" type="java.lang.String">
            <column name="zipcode" length="6" />
        </property>
        <property name="tel" type="java.lang.String">
            <column name="tel" length="13" />
        </property>
    </class>
</hibernate-mapping>
请知道原因的大虾帮助一下!谢谢!

解决方案 »

  1.   

    现在将TUser.hbm.xml: 的<set>...</>的参数中加入 cascade="all"后已经可以出现INSERT的语句了可是出现
    Cannot insert the value NULL into column 'id', table 'HibernateDemoDB.dbo.t_address'; column does not allow nulls. INSERT fails.
    的异常 改怎么解决呢?
      

  2.   

    dynamic-update
    dynamic-insert
    为true
      

  3.   

    t_address  改为允许为空
      

  4.   

    本来是主键的 现在去掉主键 允许为空了 可是插进去的数据的id字段还是null,其他字段是有值的
      

  5.   

    ...原来是设置了为identity 不过数据库理没有设置成这个....现在解决了