hibernate使用注解配置映射关系
2014-04-19 14:49
441 查看
在hibernate3.6.10中,使用注解配置映射关系通常只需要在使用XML文件配置映射关系的基础上做两点修改:
1.修改hibernate配置文件里session-factory元素的子元素mapping的配置,不再配置resource属性,而是配置class属性,如下:
<mapping class="com.zzj.entity.Customer"/>
2.给实体类加上注解:
使用注解配置映射关系,不再需要*.hbm.xml文件,而是在实体类中以注解形式定义映射关系。
与使用XML配置映射关系类似,我们也可以以编程的方式指定被注解的实体类,而不需要在hibernate的配置文件中配置mapping元素:
Configuration.addAnnotatedClass(Class annotatedClass);
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 @Column public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column public 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);
相关文章推荐
- Hibernate中使用JPA(注解)配置对象关系映射
- Hibernate使用注解配置映射
- Hibernate使用hbm.xml配置映射关系解析
- Hibernate框架学习之注解配置关系映射
- Hibernate基于注解方式配置来实现实体和数据库之间存在某种映射关系
- hibernate基于注解实现映射关系的配置
- Hibernate学习笔记 3 - 关系映射配置(基于注解)
- java注解配置hibernate映射关系
- hibernate笔记--使用注解(annotation)方式配置单(双)向多对一的映射关系
- Hibernate关系映射中的注解
- Eclipse使用xdoclet1.2.3 生成hibernate配置文件和映射文件
- SSH——Hibernate继承关系映射详解(配置+实例+总结)
- Hibernate注解开发(第三部分—对象关系映射既表与表之间关系映射)
- hibernate用注解配置实体类的映射-问题01
- HIbernate 关系映射配置
- hibernate5实体映射注解配置注解全面解析
- Hibernate中继承关系注解配置
- EasyDBO中Java注解配置映射的原理及使用
- Hibernate使用注解,继承映射写法
- 在EasyDBO中使用Java注解配置映射的原理及使用