hibernate映射属性

来源:互联网 发布:kingcms php 编辑:程序博客网 时间:2024/05/01 01:57

1、普通属性

@Entity:修饰的持久化类的所有属性都会被映射到底层数据库。

@Column:修饰属性

@Transient:修饰不想持久化保存的属性。

@Lob:修饰大数据属性。

当属性为byte[]、Byte[]时数据库对应Blob

当属性为char[]、character[]、String时,对应为Clob.

@Temporal修饰日期类型

支持TemporalType.DATE、TemporalType.TIME、TemporalType.TIMESTAMP

@GenericGenerator注解主键生成器  name strategy

例如:

@GenericGenerator(name="fk_hilo",strategy="hilo" )    //指定主键生成策略为hilo

@GeneratedValue(generator="fk_hilo")     //使用该主键生成策略

2、映射集合属性

集合分为两种类型:一、List、Set、数组

二、Map

hibernate要求持久化集合值字段必须声明为接口。

不论哪种集合,统一使用@ElementCollection注释

属性fetch指定抓取策略   targetClass:指定集合类型。

外键列@JoinColumn

当集合元素为基础数据类型时,用@Column注释定义集合元素即可

@OrderColumn可以定义List集合、数组的索引列

@MapKeyColumn用于映射Map集合的索引列


例如:数组、list集合可以这样写

@ElementCollection(targetClass=String.class)   //集合属性保存对象关联关系

@CollectionTable(name="school_inf",joinColumns=@joinColumn(name="person_id",nullable=false))  //映射保存集合属性的表

@Column(name="school_name")    //指定保存集合元素的列为school_name

@OrderColumn(name="array_order")   //映射集合的索引列

private String[] schools;

例如:set集合,set集合无序,所以不用写OrderColumn

<list name="school_name" table="school_inf">

<key>

<column name="person_id" not-null="true"/>

</key>

<list-index>

<column name="array_order"/>

<list-index>

<element type="String" column="school_name"></element>

</list>

例如:

@ElementCollection(targetClass=String.class)   //集合属性保存对象关联关系

@CollectionTable(name="school_inf",joinColumns=@joinColumn(name="person_id",nullable=false))  //映射保存集合属性的表,nullable=false说明该列为非空。

@Column(name="school_name")    //指定保存集合元素的列为school_name

private Set<String> schools;

  <setname="school_name"table="school_inf">
            <key>
                <columnname="person_id" not-null="true"/>
            </key>
            <element type="string"column="school_name"></element>
        </set>


例如:

@ElementCollection(targetClass=Float.class)   //集合属性保存对象关联关系

@CollectionTable(name="score_inf",joinColumns=@joinColumn(name="person_id",nullable=false))  //映射保存集合属性的表,nullable=false说明该列为非空。

@MapKeyColumn(name="subject_name") //指定Map Key

@Column(name="mark")    //指定保存集合元素的列为school_name

3、集合属性性能分析

在初始化持久化实体时,立即抓取所有的集合属性,将导致性能急剧下降,对于集合属性,通常使用延时加载策略。

@ElementCollection(fetch=FetchType.EAGER)

无序集合只能遍历,性能差

虽然数组也是有序结合,但是数据无法使用延时加载(因为数组长度不可变),所以实际上用数组作为集合性能并不高,通常认为List、Map集合性能较高,而

Set紧随其后

提高性能的策略:

对于删除集合中的元素,保留部分元素的情况,应该先清空集合,再插入保留元素。

List<String> tmp=person.getSchools();

person.setSchools(null);  //清空操作

person.setSchools(tmp);

 4、有序集合映射

     如果希望数据库查询自己对集合元素排序,可以利用Hibernate自己提供的@OrderBy注解

@OrderBy("traing_name desc");

5、映射数据库对象

如果希望在映射文件中创建和删除触发器,存储过程等数据库对象,hibernate提供了<database-object/>元素来满足这种需求

<database-object>

<create>create table test(t_name varchar(255))</create>

<!--指定仅对特定方言有效-->

<dialect-scope name="org........"/>

</database-object>

   这样在创建sessionFactory对象时,上面代码就生效了,数据库中就新建了test表。

Hibernate提供了schemaExport工具,生成数据库对象。

Configuration conf=new Configuration().configure();

SchemaExport se=new SchemaExport(conf);

se.setFormat(true)   //格式化sql

  .setOutputFile("news.sql")    //设置保存sql脚本的文件

.create(true,true);  //输入sql脚本并执行

6、映射组件属性:

持久化类属性并不是基础数据类型,例如privte Name name;

一、可以用@Embeddable注释Name类,作为组件使用,用@Parent修饰该属性。(修饰Name里的 private Person owner)

二、直接在Person实体类中,包含一个组件类型(Name类)

@Embedded

@AttributeOverrides({

@AttributeOverride(name="first",column=@Column(name="person_firstname")),

@AttributeOverride(name="last",column=@Column(name="person_lastname")),

})








0 0
原创粉丝点击