1.Configuration对象
2.添加映射文件
2.1在hibernate.cfg.xml中
<mapping resource="it/com/cn/hibernate/entity/User.hbm.xml"/>
2.2 在类中
Configuration cfg = new Configuration();
cfg.configure();
cfg.addResource("it/com/cn/hibernate/entity/User.hbm.xml")
2.3在类中添加类
cfg.addClass(User.class)
原因源码中找到如下:
@Deprecated
public MetadataSources addClass(Class entityClass) {
if ( entityClass == null ) {
throw new IllegalArgumentException( "The specified class cannot be null" );
}
LOG.debugf( "adding resource mappings from class convention : %s", entityClass.getName() );
final String mappingResourceName = entityClass.getName().replace( '.', '/' ) + ".hbm.xml";
addResource( mappingResourceName );
return this;
}