论坛首页 入门讨论版 Hibernate

数据表之间的关系

浏览 317 次
该帖已经被评为新手帖
作者 正文
最后更新时间:2008-05-11 关键字: 数据表之间的关系
数据表之间的关系
User表  1------------------n    QianDao表

QianDao表  1-----------------n   qingJia表



即一个用户对应多个签到 记录,设置为1对多关系
  而请假的也要签到,只是自动签到而已,由于可能一日请的长短有别,故签到表和请假表也是1对多,


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sang.domain">
	<class name="User" table="USERS" lazy="false" dynamic-insert="true"
		dynamic-update="true">
		<id name="id" column="ID" type="long" length="8">
			<generator class="increment" />
		</id>
		<property name="name" column="USER_NAME" type="string"
			not-null="false" length="15" unique="true" />
		<property name="password" column="password" type="string"
			not-null="false" length="15" />

		<set name="qian_daoJiLu">
			<key column="qian_daoJiLu" not-null="false" />
			<one-to-many class="Qian_dao" />
		</set>

	</class>
</hibernate-mapping>


======================================================================


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sang.domain">


	<class name="Qian_dao" table="qiandao_dsj" lazy="false"
		dynamic-insert="true" dynamic-update="true">
		<id name="id" column="ID" type="long" length="18">
			<generator class="increment" />
		</id>

		<property name="name" column="qiandaoxingming" type="string"
			not-null="false" />
		<property name="qianDaoShiJian" column="qianDaoShiJian"
			type="date" not-null="false" />
		<property name="shangBanShiJian" column="shangBanShiJian"
			type="date" not-null="false" />
		<property name="xiaBanShiJian" column="xiaBanShiJian"
			type="date" not-null="false" />
		<property name="shiFouDaiQian" column="shiFouDaiQian"
			type="boolean" not-null="false" />
		<property name="liYou" column="liYou" type="string"
			not-null="false" />
		<property name="zhuangTaiBiaoShi" column="zhuangTaiBiaoShi"
			type="string" not-null="false" />

		<property name="qingJiaShiJian_Qi" column="qingJiaShiJian_Qi"
			type="date" not-null="false" />
		<property name="qingJiaShiJian_Zhi" column="qingJiaShiJian_Zhi"
			type="date" not-null="false" />


		<!--		<many-to-one name="tableCreated"-->
		<!--			class="com.sang.domain.TableCreated" cascade="all">-->
		<!--			<column name="tableCreated_id"></column>-->
		<!--		</many-to-one>-->
		<set name="qing_jia" cascade="save-update">
			<key column="qing_jia" not-null="false" />
			<one-to-many class="Qing_jia" />

		</set>
		<many-to-one name="user" cascade="save-update" class="User" >
		<column name="user_id" not-null="false"/>
		</many-to-one>



	</class>
</hibernate-mapping>



========================================================================





<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sang.domain">


	<!--	private String keNeiShenPiRen;//可能会改用一个类来映射-->

	<class name="Qing_jia" table="qing_jia_dsj" lazy="false"
		dynamic-insert="true" dynamic-update="true">
		<id name="id" column="ID" type="long" length="18">
			<generator class="increment" />
		</id>

		<property name="jiLuShiJian" column="jiLuShiJian" type="date"
			not-null="false" />
		<property name="name" column="qingjia_name" type="string"
			not-null="false" />
		<property name="shiYou" column="shiYou" type="string"
			not-null="false" />
		<property name="qingJiaShiJian_Qi" column="qingJiaShiJian_Qi"
			type="date" not-null="false" />
		<property name="qingJiaShiJian_Zhi" column="qingJiaShiJian_Zhi"
			type="date" not-null="false" />
		<property name="keNeiShenPiRen" column="keNeiShenPiRen"
			type="string" not-null="false" />
		<property name="keNeiSHenPiYiJian" column="keNeiSHenPiYiJian"
			type="string" not-null="false" />

		<property name="zhuGuanJuZhangShenPiRen"
			column="zhuGuanJuZhangShenPiRen" type="string" not-null="false" />
		<property name="zhuGuanJuZhangYiJian"
			column="zhuGuanJuZhangYiJian" type="string" not-null="false" />

		<property name="xiaoJiaShiJian" column="xiaoJiaShiJian"
			type="date" not-null="false" />


		<many-to-one name="Qian_dao" class="Qian_dao"
			cascade="save-update" not-null="false">
			<column name="Qian_dao_id" not-null="false" />
		</many-to-one>



	</class>
</hibernate-mapping>







这样可以吗?


测试代码如下

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"sang-dao.xml");
		Qian_daoDao qian_daoDao = (Qian_daoDao) context.getBean("qian_daoDao");

		UserDao userDao = (UserDao) context.getBean("userDao");

		User user = new User();
		user.setName("sang");
		user.setPassword("sang");

		Qian_dao qian_dao = new Qian_dao();
		qian_dao.setName("qian_dao");
		
//
		user.getQian_daoJiLu().add(qian_dao);
		
		userDao.saveUser(user);



报错如下

Hibernate: select max(ID) from USERS
Hibernate: insert into USERS (USER_NAME, password, ID) values (?, ?, ?)
Hibernate: update qiandao_dsj set qian_daoJiLu=? where ID=?
org.hibernate.event.def.AbstractFlushingEventListener -16   [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize database state with session
org.hibernate.TransientObjectException: com.sang.domain.Qian_dao
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:697)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1037)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sang.dao.UserDaoImpl.saveUser(UserDaoImpl.java:30)
at App.main(App.java:40)
Exception in thread "main" org.hibernate.TransientObjectException: com.sang.domain.Qian_dao
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:697)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1037)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sang.dao.UserDaoImpl.saveUser(UserDaoImpl.java:30)
at App.main(App.java:40)
   
论坛首页 入门讨论版 Hibernate

跳转论坛:
JavaEye推荐