(2)Hibernate3.2 中的类继承关系

主要内容:

1 简单的 Hibernate 的使用.

2 Hibernate 中 关于继承.

3 Hibernate 中的数据源.

 

个人认为类的继承和表的 one-to-one 很像。在这里就说说吧。

UserBO.hbm.xml 如下:

<hibernate-mapping>
	<class name="com.isw2.entity.UserBO" table="t_user"
		catalog="superclass" lazy="true">
		<id column="userId" name="userId">
			<generator class="native"></generator>
		</id>
		<!-- 用于在一张表里存储子类数据
			<discriminator column="userType" type="string"></discriminator>
		-->
		<property name="userName" column="userName" type="string"
			length="16">
		</property>
		<property name="userSex" column="userSex" type="string"
			length="2">
		</property>
		<!-- 这里用于在多张表里存放子类信息 -->
		<joined-subclass name="com.isw2.entity.BlueUserBO"
			table="t_blueUser">
			<key column="userId"></key>
			<property name="userLevel" column="userLevel"
				type="integer">
			</property>
		</joined-subclass>
		<joined-subclass name="com.isw2.entity.RedUserBO"
			table="t_redUser">
			<key column="userId"></key>
			<property name="userTime" column="userTime" type="date"></property>
		</joined-subclass>


		<!-- 子类在此说明,类状态由 discriminator 标签说明(用于在一张表中存储子类信息)
			<subclass name="com.isw2.entity.BlueUserBO"
			discriminator-value="blueUser">
			<property name="userLevel" column="userLevel"
			type="integer">
			</property>
			</subclass>
			<subclass name="com.isw2.entity.RedUserBO"
			discriminator-value="redUser">
			<property name="userTime" column="userTime" type="date">
			</property>
			</subclass>
		-->
	</class>

这里用到两种方式:

1一张表中存放父类,子类。

2 单独存放 父类,子类.

 

它们的关系可以被 Hibernate 识别:

	public void findAll() {
		Session session = HibernateSessionFactory.getSession();
		Query query = session.createQuery("from UserBO");
		List list = query.list();
		for (Object object : list) {
			this.displayEntity(object);
		}
		session.close();
	}

	public void addUser(UserBO userBO) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tr = session.beginTransaction();
		session.save(userBO);
		tr.commit();
		session.close();
	}

 

 

 

hibernate.cfg.xml 中的相关配制:

<session-factory>
	<property name="connection.username">root</property>
	<property name="connection.url">
		jdbc:mysql://127.0.0.1:3306/superclass
	</property>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="myeclipse.connection.profile">MySQL</property>
	<property name="connection.password">123456</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<!-- 输出SQL语句 -->
	<property name="show_sql">true</property>
	<!-- 设置JDBC 连接池  需添加 c3p0-0.8.4.5.jar -->
	<property name="hibernate.c3p0.max_size">20</property>
	<property name="hibernate.c3p0.min_size">5</property>
	<property name="hibernate.c3p0.timeout">1800</property>
	<property name="hibernate.c3p0.max_statements">50</property>
	<!-- 优化查询策略 -->
	<!-- 每次执行语句数量 -->
	<property name="jdbc.batch_size">50</property>
	<!-- 返回查询结果数量 -->
	<property name="jdbc.fetch_size">30</property>
	
	<mapping resource="com/isw2/entity/UserBO.hbm.xml" />

</session-factory>

你可能感兴趣的:(sql,mysql,Hibernate,jdbc,MyEclipse)