首先是实体类:
TAddress.java(多的一方)
|
TUser.java(一的一方)
|
然后是各个实体类的配置文件
TAddress.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <!-- 一对多 --> <hibernate-mapping> <class name="com.model.TAddress" table="t_address" dynamic-update="false" dynamic-insert="false"> <id name="id" type="java.lang.Integer" column="id" unsaved-value="0"> <generator class="native" /> </id> <property name="address" column="address" type="string" /> <property name="tel" column="tel" type="string" /> <property name="zipcode" column="zipcode" type="string" /> <property name="type" column="type" type="string" /> <!-- 必须有many-to-one 否则关联字段(user_id)为null --> <many-to-one name="user" class="com.model.TUser" cascade="none" outer-join="auto" update="true" insert="true" access="property" column="user_id" not-null="true"> </many-to-one> </class> </hibernate-mapping>
TUser.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <!-- 一对多 外键关联 --> <!-- Select from TUser where id=1 Select from TUser where id=1 to Select from TUser where id=1 or id=2 --> <!-- batch-size 批量加载机制 可以自定义每次批量加载的数量 --> <hibernate-mapping> <class name="com.model.TUser" table="t_user" dynamic-update="true" > <id name="id" type="java.lang.Integer" column="id" unsaved-value="0"> <generator class="native" /> </id> <property name="name" column="name" /> <property name="age" column="age" /> <set name="address" table="t_address" cascade="all" order-by="zipcode asc" lazy="true" inverse="true"> <key column="user_id" /><!-- 确定关联的外键列 --> <one-to-many class="com.model.TAddress" /> </set> </class> </hibernate-mapping>
其次是hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <!-- properties --> <!-- 数据库URL --> <property name="hibernate.connection.url">jdbc:mysql://localhost/onetomany</property> <!-- 数据库JDBC驱动 --> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <!-- 数据库用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">hello</property> <!-- 数据库方言 --> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <!-- 是否日志调试 --> <property name="show_sql">true</property> <!-- 是否使用数据库外连接 --> <property name="use_outer_join">true</property> <!-- 事务管理 使用JDBC Transaction(使用JTA会报错) --> <property name="transaction.factory_class"> net.sf.hibernate.transaction.JDBCTransactionFactory </property> <!-- 指定hibernate每次提交的SQL数量 对批量操作的性能提升帮助很大!!!!!!!!!!!!! --> <property name="hibernate.jdbc.batch_size">25</property> <!-- 映射文件配置,配置文件名必须包含其相对于根的全路径 --> <mapping resource="com/model/TUser.hbm.xml" /> <mapping resource="com/model/TAddress.hbm.xml" /> </session-factory> </hibernate-configuration>
测试代码(部分)
增加
|
查询
|
批量插入(可以提高性能)
实现机制:如果使用了批量加载机制,hibernate在进行数据查询操作前,会自动在当前session中寻找是否还存在
其他同类型待加载的数据,如果有,则将其查询条件合并在当前的select语句中一并提交,这样,通过
一次数据库操作即完成了多个读取任务。
|