最近开始尝试hibernate annotations,终于成功的将手上一个小应用转为annotations
1、spring orm support
与原来使用LocalSessionFactoryBean相比,变动不大(AnnotationSessionFactoryBean本来就是从LocalSessionFactoryBean类继承过来的嘛)
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 <bean
2 id="sessionFactory"
3 class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
4 parent="AbstractSessionFactory">
5 <property name="annotatedClasses">
6 <list>
7 <value>xxx.xxx.xxx.domain.Accountvalue>
8 list>
9 property>
10 bean>
11 <bean
12 id="AbstractSessionFactory"
13 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
14 abstract="true">
15 <property
16 name="dataSource"
17 ref="DataSource" />
18 <property name="hibernateProperties">
19 <props>
20 <prop key="hibernate.dialect">${hibernate.dialect}prop>
21 <prop key="hibernate.show_sql">${hibernate.show_sql}prop>
22 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>
23 <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}prop>
24 <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}prop>
25 props>
26 property>
27 <property
28 name="lobHandler"
29 ref="DefaultLobHandler" />
30 bean>
2、id的配置
非常简单,在id的getter上面加个“@Id”就可以了。此时采用的id策略是javax.persistence.GenerationType.AUTO,也可以再加上“@GeneratedValue(generator =GenerationType.IDENTITY|GenerationType.SEQUENCE|GenerationType.TABLE)”换成其它策略。
我的应用采用的是hibernate的uuid策略,就不得不在这儿使用hibernate的扩展了
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> @Id
@Column(length = 32)
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
3、级联策略
在ejb3-persistence.jar中只定义了ALL、MERGE、PERSIST、REFRESH、REMOVE,比较恶心的就是,删除对象的时候,并不会级联删除关联对象,而是用update xx set parent_id=null where parent_id=?这类语句把关系干掉了事。不得已,在这儿用了hibernate的DELETE_ORPHAN。
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> @OneToMany(targetEntity = Attachment.class)
@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN,
org.hibernate.annotations.CascadeType.ALL})
@JoinColumn(name = "info_id")
4、CACHE
ejb3-persistence.jar里面没有找到cache的配置,继续请出hibernate来干活
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "T_INFO")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
5、自定义字段类型
我的POJO中有一个private String content;的属性,按ejb3配成@Lob后,被处理成了text类型,text 64k的存储容量还是比较可怜了。
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> @Lob
@Column(columnDefinition = "LongText")
分享到:
相关推荐
现在已经不用*.hbm.xml这种映射文件了,都是用Annotation(注解)方式来完成实体与表之间的映射关系,这样看起来比用xml文件来映射更具有可读性,自我感觉以后Hibernate Annotation的映射方式将代替hibernate 的*....
hibernate-annotations-3.4.0.GA hibernate-annotations-3.4.0.GA hibernate-annotations-3.4.0.GA
Hibernate Annotations API 中文文档 前言 1. 创建一个注解项目 1.1. 系统需求 1.2. 系统配置 2. 实体Bean 2.1. 简介 2.2. 用EJB3注解进行映射 2.2.1. 声明实体bean 2.2.1.1. 定义表(Table) 2.2.1.2. 乐观...
hibernate annotations 中文参考手册
Hibernate Annotations 中文版
webwork2.1.7 中文开发文档 Hibernate Annotations参考文档-3.20中文开发文档 Hibernate_3.2.0_Reference_zh_CN中文开发文档
hibernate annotations 3.4.0.GA API
hibernate 和hibernate_annotations(中文API)帮助文档。
hibernate annotations
Hibernate3.2 Annotations中文文档 Hibernate3.2 Annotations中文文档 Hibernate3.2 Annotations中文文档 网页格式
hibernate annotations 的中文帮助文档 可以帮您更好的学习hibernate 的注解配置
NULL 博文链接:https://xiongjiajia.iteye.com/blog/1947270
hibernate-annotations.jar
hibernate annotations 核心包,欢迎下载
hibernate-annotations-3.4.0
Hibernate Annotations API英文版,CHM格式
使用注解Annotation时用的jar包。 包括: hibernate-commons-annotations-3.3.0.ga.jar hibernate-entitymanager.jar ejb3-persistence.jar hibernate-annotations.jar
hibernate-annotations-3.4.0.CR1中文电子书
hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...