关于hibernate的保存问题,many-to-one “one”的一方无法持久化,求原理与解决途径
我在数据库设置了两个表,职工表worker:wno(主键),wname,..职工记录表workerstate:wno,date,state.其中(wno,date)主键,...
我在数据库设置了两个表,
职工表worker:wno(主键),wname,..
职工记录表workerstate:wno,date,state.其中(wno,date)主键,wno外键。
这是配置文件,利用方向工程生成:
Worker.hbm.xml
<hibernate-mapping>
<class name="dao.Worker" table="worker" catalog="j2ee">
<id name="wno" type="java.lang.String">
<column name="WNO" length="8" />
<generator class="native" />
</id>
<property name="wpass" type="java.lang.String">
<column name="WPASS" length="16" not-null="true" />
</property>
<!--....其他属性-->
<set name="workerstates" inverse="true" lazy="false">
<key>
<column name="WNO" length="8" not-null="true" />
</key>
<one-to-many class="dao.Workerstate" />
</set>
</class>
</hibernate-mapping>
Workerstate.hbm.xml
<hibernate-mapping>
<class name="dao.Workerstate" table="workerstate" catalog="j2ee">
<composite-id name="id" class="dao.WorkerstateId">
<key-many-to-one name="worker" class="dao.Worker" lazy="false">
<column name="WNO" length="8" />
</key-many-to-one>
<key-property name="date" type="java.util.Date">
<column name="DATE" length="10" />
</key-property>
</composite-id>
<property name="state" type="java.lang.String">
<column name="STATE" length="3" />
</property>
</class>
</hibernate-mapping>
现有一个新建的worker对象(不对其Set workerstate赋值),workerDAO.attachDirty(worker);在这里,卡住了。
初学hibernate,求高人解救 展开
职工表worker:wno(主键),wname,..
职工记录表workerstate:wno,date,state.其中(wno,date)主键,wno外键。
这是配置文件,利用方向工程生成:
Worker.hbm.xml
<hibernate-mapping>
<class name="dao.Worker" table="worker" catalog="j2ee">
<id name="wno" type="java.lang.String">
<column name="WNO" length="8" />
<generator class="native" />
</id>
<property name="wpass" type="java.lang.String">
<column name="WPASS" length="16" not-null="true" />
</property>
<!--....其他属性-->
<set name="workerstates" inverse="true" lazy="false">
<key>
<column name="WNO" length="8" not-null="true" />
</key>
<one-to-many class="dao.Workerstate" />
</set>
</class>
</hibernate-mapping>
Workerstate.hbm.xml
<hibernate-mapping>
<class name="dao.Workerstate" table="workerstate" catalog="j2ee">
<composite-id name="id" class="dao.WorkerstateId">
<key-many-to-one name="worker" class="dao.Worker" lazy="false">
<column name="WNO" length="8" />
</key-many-to-one>
<key-property name="date" type="java.util.Date">
<column name="DATE" length="10" />
</key-property>
</composite-id>
<property name="state" type="java.lang.String">
<column name="STATE" length="3" />
</property>
</class>
</hibernate-mapping>
现有一个新建的worker对象(不对其Set workerstate赋值),workerDAO.attachDirty(worker);在这里,卡住了。
初学hibernate,求高人解救 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询