您的位置:首页 > 其它

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.给实体类加上注解:

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: