如做商用请注明出处itcast
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接URL -->
<property name="connection.url">
jdbc:mysql://localhost/testHibernate
</property>
<!-- 数据库连接驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 数据库用户名 -->
<property name="connection.username">root</property>
<!-- 数据库用户密码 -->
<property name="connection.password">sql</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 自动创建表 -->
<property name="hbm2ddl.auto">
create
</property>
<!-- 自动创建表 -->
<property name="show_sql">
true
</property>
<!-- 指定映射文件 -->
<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml"/>
<mapping resource="cn/itcast/hibernate/domain/Department.hbm.xml"/>
<mapping resource="cn/itcast/hibernate/domain/Employee.hbm.xml"/>
<mapping resource="cn/itcast/hibernate/domain/Student.hbm.xml"/>
<mapping resource="cn/itcast/hibernate/domain/Person.hbm.xml"/>
<mapping resource="cn/itcast/hibernate/domain/IdCard.hbm.xml"/>
<mapping resource="cn/itcast/hibernate/domain/Teacher.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="cn.itcast.hibernate.domain.Department" ><!-- 类名为关键字了可以用添加``的方式屏蔽或另起表名 -->
<!-- id元素用来定义主键标识,并指定主键自动递增 -->
<id name="id">
<generator class="native"></generator>
</id>
<!-- 定义其他属性 -->
<property name="name"></property>
<!-- inverse在有序的集合list里面不能使用,只能在one-to-many端配置使用 -->
<set name="emps" cascade="save-update" inverse="true"><!-- 一般对many-to-one,many-to-many不设置级联在one-to-one,one-to-many中根据需要设置级联 -->
<key column="depart_id"/>
<one-to-many class="cn.itcast.hibernate.domain.Employee"/>
</set>
<!-- bag与list对应使用,取消了list的顺序性
<bag name="emps">
<key column="depart_id"/>
<one-to-many class="Employee"/>
</bag> -->
<!-- <list name="emps">
<key column="depart_id"/>
<list-index column="order_col"/>
<one-to-many class="Employee"/>
</list> -->
<!-- <map name="emps">
<key column="depart_id"/>
<map-key type="string" column="name"/>
<one-to-many/>
</map> -->
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<!--<class name="cn.itcast.hibernate.domain.Employee" discriminator-value="0" > 类名为关键字了可以用添加``的方式屏蔽或另起表名 -->
<class name="cn.itcast.hibernate.domain.Employee" discriminator-value="0">
<!-- id元素用来定义主键标识,并指定主键自动递增 -->
<id name="id">
<generator class="hilo"></generator>
</id>
<!-- 定义其他属性 -在Employee类文件中根据对象depart找到其对应的类Department -->
<!-- 再在Department.hbm.xml中找到其对应的表department再找到depart对象对应记录的id将其赋给emp记录中的depart_id-->
<!-- <discriminator column="type" type="int"/>
<property name="name" unique="true"></property>
<many-to-one name="depart" column="depart_id"/>
<subclass name="cn.itcast.hibernate.domain.Skiller" discriminator-value="1">
<property name="skill"/>
</subclass>
<subclass name="cn.itcast.hibernate.domain.Sales" discriminator-value="2">
<property name="sell"/>
</subclass> -->
<!-- <property name="name" unique="true"/>
<many-to-one name="depart" column="depart_id"/>
<joined-subclass name="cn.itcast.hibernate.domain.Skiller" table="skiller">
<key column="emp_id"/>
<property name="skill"/>
</joined-subclass>
<joined-subclass name="cn.itcast.hibernate.domain.Sales" table="sales">
<key column="emp_id"/>
<property name="sell"/>
</joined-subclass> -->
<!-- <discriminator column="type" type="int"/>
<property name="name" unique="true"/>
<many-to-one name="depart" column="depart_id"/>
<subclass name="cn.itcast.hibernate.domain.Skiller" discriminator-value="1">
<property name="skill"/>
</subclass>
<subclass name="cn.itcast.hibernate.domain.Sales" discriminator-value="2">
<join table="sales">
<key column="emp_id"/>
<property name="sell"/>
</join>
</subclass> -->
<property name="name" unique="true"/>
<many-to-one name="depart" column="depart_id"/>
<union-subclass name="cn.itcast.hibernate.domain.Skiller" table="skiller">
<property name="skill"/>
</union-subclass>
<union-subclass name="cn.itcast.hibernate.domain.Sales" table="sales">
<property name="sell"/>
</union-subclass>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="cn.itcast.hibernate.domain.IdCard" table="id_card" ><!-- 类名为关键字了可以用添加``的方式屏蔽或另起表名 -->
<!-- id元素用来定义主键标识,并指定主键自动递增 -->
<id name="id">
<!-- <generator class="foreign">
<param name="property">person</param>
</generator> -->
<generator class="native"/>
</id>
<!-- 定义其他属性 -->
<property name="usefulLife" column="useful_life"></property>
<!-- 在Employee类文件中根据对象depart找到其对应的类Department
再在Department.hbm.xml中找到其对应的表department再找到depart对象对应记录的id将其赋给emp记录中的depart_id -->
<!-- <one-to-one name="person" constrained="true"/> -->
<many-to-one name="person" column="person_id" unique="true"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="cn.itcast.hibernate.domain.Person" ><!-- 类名为关键字了可以用添加``的方式屏蔽或另起表名 -->
<!-- id元素用来定义主键标识,并指定主键自动递增 -->
<id name="id">
<generator class="native"></generator>
</id>
<!-- 定义其他属性 -->
<property name="name"></property>
<!-- 在Employee类文件中根据对象depart找到其对应的类Department
再在Department.hbm.xml中找到其对应的表department再找到depart对象对应记录的id将其赋给emp记录中的depart_id -->
<one-to-one name="idCard" property-ref="person"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="cn.itcast.hibernate.domain.Student"><!-- 类名为关键字了可以用添加``的方式屏蔽或另起表名 -->
<!-- id元素用来定义主键标识,并指定主键自动递增 -->
<id name="id">
<generator class="native"></generator>
</id>
<!-- 定义其他属性 -->
<property name="name"></property>
<!-- 在Employee类文件中根据对象depart找到其对应的类Department
再在Department.hbm.xml中找到其对应的表department再找到depart对象对应记录的id将其赋给emp记录中的depart_id -->
<set name="teachers" table="teacher_student">
<key column="student_id"/>
<many-to-many class="cn.itcast.hibernate.domain.Teacher" column="teacher_id"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="cn.itcast.hibernate.domain.Teacher"><!-- 类名为关键字了可以用添加``的方式屏蔽或另起表名 -->
<!-- id元素用来定义主键标识,并指定主键自动递增 -->
<id name="id">
<generator class="native"></generator>
</id>
<!-- 定义其他属性 -->
<property name="name"></property>
<!-- 在Employee类文件中根据对象depart找到其对应的类Department
再在Department.hbm.xml中找到其对应的表department再找到depart对象对应记录的id将其赋给emp记录中的depart_id -->
<set name="students" table="teacher_student">
<key column="teacher_id"/>
<many-to-many class="cn.itcast.hibernate.domain.Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="cn.itcast.hibernate.domain.User" table="`user`"><!-- 类名为关键字了可以用添加``的方式屏蔽或另起表名 -->
<!-- id元素用来定义主键标识,并指定主键自动递增 -->
<id name="id">
<generator class="native"></generator>
</id>
<!-- 定义其他属性 -->
<property name="name"></property>
<property name="birthday"></property>
</class>
</hibernate-mapping>
hibernate xml configuration record
最新推荐文章于 2024-07-29 15:35:41 发布