hibernate xml根本装备温习小结ITeyecsdn - 众发娱乐

hibernate xml根本装备温习小结ITeyecsdn

2019-02-07 08:47:59 | 作者: 向山 | 标签: 联系,核算,个数 | 浏览: 8357

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"

        "https://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 /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" 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"

        "https://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 /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"  

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 /

/set

!

//多对多

set name="courses" table="t_student_course"  

key column="t_student_id" /

many-to-many 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 / /list

//一对一

one-to-one name="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"

        "https://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

 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表众发娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章