hibernate使用注解配置映射关系
来源:互联网 发布:矩阵的几种分解介绍 编辑:程序博客网 时间:2024/06/06 03:28
在hibernate3.6.10中,使用注解配置映射关系通常只需要在使用XML文件配置映射关系的基础上做两点修改:
1.修改hibernate配置文件里session-factory元素的子元素mapping的配置,不再配置resource属性,而是配置class属性,如下:
<mapping class="com.zzj.entity.Customer"/>
2.给实体类加上注解:
package com.zzj.entity;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="customer")public class Customer implements Serializable{private static final long serialVersionUID = 1L;private String username;private String password;public Customer() {}public Customer(String username, String password) {this.username = username;this.password = password;}@Id@Columnpublic String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Columnpublic String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}在博文《hibernate加载映射文件浅析》讲到,当我们调用configuration的config()方法时,会去解析hibernate的XML配置文件。解析mapping元素的时候会调用一个私有方法:
private void parseMappingElement(Element mappingElement, String name) {final Attribute resourceAttribute = mappingElement.attribute( "resource" );final Attribute fileAttribute = mappingElement.attribute( "file" );final Attribute jarAttribute = mappingElement.attribute( "jar" );final Attribute packageAttribute = mappingElement.attribute( "package" );final Attribute classAttribute = mappingElement.attribute( "class" );//获取class属性if ( resourceAttribute != null ) {final String resourceName = resourceAttribute.getValue();log.debug( "session-factory config [{}] named resource [{}] for mapping", name, resourceName );addResource( resourceName );}else if ( fileAttribute != null ) {final String fileName = fileAttribute.getValue();log.debug( "session-factory config [{}] named file [{}] for mapping", name, fileName );addFile( fileName );}else if ( jarAttribute != null ) {final String jarFileName = jarAttribute.getValue();log.debug( "session-factory config [{}] named jar file [{}] for mapping", name, jarFileName );addJar( new File( jarFileName ) );}else if ( packageAttribute != null ) {final String packageName = packageAttribute.getValue();log.debug( "session-factory config [{}] named package [{}] for mapping", name, packageName );addPackage( packageName );}else if ( classAttribute != null ) {final String className = classAttribute.getValue();log.debug( "session-factory config [{}] named class [{}] for mapping", name, className );try {addAnnotatedClass( ReflectHelper.classForName( className ) );//通过class属性解析实体类}catch ( Exception e ) {throw new MappingException("Unable to load class [ " + className + "] declared in Hibernate configuration <mapping/> entry",e);}}else {throw new MappingException( "<mapping> element in configuration specifies no known attributes" );}}由以上代码可知,如果我们配置class属性,最后会调用addAnnotatedClass()方法,这个方法跟addResource()方法是等价的,都是把配置信息放入configuration的metadataSourceQueue属性中,只不过addAnnotatedClass()方法是通过读取实体类的注解信息获取映射关系,而addResource()是通过读取XML映射文件获取映射关系。
使用注解配置映射关系,不再需要*.hbm.xml文件,而是在实体类中以注解形式定义映射关系。
与使用XML配置映射关系类似,我们也可以以编程的方式指定被注解的实体类,而不需要在hibernate的配置文件中配置mapping元素:
Configuration.addAnnotatedClass(Class annotatedClass);
0 7
- hibernate使用注解配置映射关系
- Hibernate中使用JPA(注解)配置对象关系映射
- Hibernate使用注解配置映射
- java注解配置hibernate映射关系
- hibernate注解 关系映射
- Hibernate注解关系映射
- Hibernate注解关系映射
- Hibernate注解关系映射
- Hibernate关系注解映射
- Hibernate映射关系注解
- hibernate关系注解映射
- hibernate注解关系映射
- Hibernate学习笔记 3 - 关系映射配置(基于注解)
- Hibernate框架学习之注解配置关系映射
- 使用annotation配置hibernate映射关系
- hibernate 映射关系注解实现
- hibernate映射关系级别注解
- hibernate常用注解&映射关系
- 《富爸爸穷爸爸》读书笔记(四)
- SSH框架整合开发详解(个人笔记)
- 《富爸爸穷爸爸》读书笔记(五)
- 算法导论-4-6 VLSI芯片测试
- Bouncing Ball Game Code on Cortex-M4
- hibernate使用注解配置映射关系
- code strike round 1 A
- Android使用Application总结
- qq不定时掉线的对策
- spring test---restful与文件上传
- python不是内部命令或外部命令,也不是可执行程序解决方案
- -----------------委托的使用场合 (使用方式)----------------
- 南阳 686 An Interesting Sequence
- APIO2008免费道路 Bsoj2714 并查集+两次最小生成树