hibernate xml基本配置复习小结

多对一,多对多 ,一对一

 

public class Item {

private Integer id; 

private String productName; 

private BigDecimal unitPrice; 

private int amount; 

private Order order;

}

public class Order {

private Integer id; 

private Date createDate;

private Set<Item> items = new HashSet<Item>();

//private int itemsNum; //用于计算item个数

//private int version //乐观锁

}

//继承关系映射

public class Product { 

private Integer id; 

private String name; 

private BigDecimal price;

}

public class Book extends Product { 

private String author; 

private String publisher;

}

//组合关系映射

public class Question { 

private Integer id; 

private String name; 

private String level;

}

public class ChoiceQuestion extends Question { 

private String options;

}

//组件关系映射

public class Person { 

private Integer id; 

private String name; 

private Address address;

}

public class Address { 

private String city; 

private String street;

}

 

Item.hbm.xml

<?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.tarena.tts.po">

<class name="Item" table="t_item">

<id name="id" column="t_id" type="integer">

<generator class="identity"></generator>

</id>

<property name="productName" column="t_product_name" type="string"/>

<property name="unitPrice" column="t_unit_price" type="big_decimal"/>

<property name="amount" column="t_amount" type="integer"/>

<many-to-one name="order" class="Order" column="t_order_id"// unique="true"一对一加添unique属性></many-to-one>

</class>

</hibernate-mapping>

 

Order.hbm.xml

<?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.tarena.tts.po">

<class name="Order" table="t_order">

//指定Order为二级缓存对象

<cache usage="read-only" region="SampleCache1"/>

<id name="id" column="t_id" type="integer">

<generator class="identity"></generator>

</id>

<!-- 

//乐观锁version标签必须跟在id标签后面加个字段

    <version name="version" column="ver" type="integer"></version>

//用于计算item个数

<property name="itemsNum" type="integer" formula="(select count(*) from t_item i where i.t_order_id=t_id)" /> 

 

//继承关系映射

<joined-subclass name="Book" table="t_book"> 

<key column="t_product_id" /> 

<property name="author" type="string" column="t_author" /> 

<property name="publisher" type="string" column="t_publisher" /> 

</joined-subclass>

 

//组合关系映射

<discriminator type="string" column="t_type" /> 

<subclass name="ChoiceQuestion" discriminator-value="c"> 

<property name="options" column="t_options" type="string" />

</subclass>

 

//组件关系映射

<component name="address" class="Address"> 

<property name="city" column="t_city" type="string" />

<property name="street" column="t_street" type="string" /> 

</component>

 

//自定义hql变量

<query name="findEmpByName"> <![CDATA[from Emp emp where emp.name=?]]> </query>

-->

<property name="createDate" type="timestamp" column="t_create_date" />

//一对多

<set name="items" cascade="all-delete-orphan" inverse="true" lazy="true" fetch="join">

<key column="t_order_id"/>

<one-to-many class="Item"/>

</set>

<!--

//多对多

<set name="courses" table="t_student_course"> 

<key column="t_student_id" />

<many-to-many class="Course" column="t_course_id" />

</set>

//一对多many(List)在list有序

<list name="persons">

<key column="t_team_id" />

<list-index column="t_turn" base="0" /> 

<one-to-many class="Person" /> </list>

//一对一

<one-to-one name="user" class="User" property-ref="xuesheng" />

-->

</class>

</hibernate-mapping>

 

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="connection.url">

jdbc:mysql://localhost:3306/hibernate_test?useUnicode=true&amp;characterEncoding=utf8

</property>

<property name="connection.username">root</property>

<property name="connection.password">root</property>

<property name="connection.pool_size">2</property>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="connection.driver_class">

com.mysql.jdbc.Driver

</property>

<!-- 

//使用线程单例

<property name="current_session_context_class">thread</property>

//添加二级缓存,需要配置ehcache.xml

<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>

//启用查询缓存,session.createQuery(...);query.setCacheable(true);...

<property name="hibernate.cache.use_query_cache">true</property>

-->

<property name="hibernate.show_sql">true</property>

<mapping resource="com/tarena/tts/po/Order.hbm.xml" /> 

<mapping resource="com/tarena/tts/po/Item.hbm.xml" />

</session-factory>

</hibernate-configuration>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值