您的位置:首页 > 编程语言 > Java开发

Spring整合Hibernate,使用注解,报错can't find hbm.cfg.xml

2015-06-05 16:32 489 查看
Spring整合Hibtenate的代码如下:  

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<!-- 设置Hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<!-- 设置Hibernate的数据库方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->
<prop key="hibernate.show_sql">true</prop>
<!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->
<prop key="hibernate.jdbc.batch_size">50</prop>
<!-- 加载hibernate自动生成数据库结构 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>

<!-- Spring注解hibernate实体类 -->
<property name="annotatedClasses">
<list>
<value>com.classmobile.entity.NewsEntity</value>
<value>com.classmobile.entity.NewsMenuEntity</value>
<value>com.classmobile.entity.ClazzMenuEntity</value>
<value>com.classmobile.entity.ClazzEntity</value>
<value>com.classmobile.entity.FileTypeEntity</value>
<value>com.classmobile.entity.SourceEntity</value>
<value>com.classmobile.entity.SourceMenuEntity</value>
<value>com.classmobile.entity.UserEntity</value>
<value>com.classmobile.entity.HomeWorkTitleEntity</value>
<value>com.classmobile.entity.UserHomeWorkEntity</value>
<value>com.classmobile.entity.TopicEntity</value>
<value>com.classmobile.entity.TopicMenuEntity</value>
<value>com.classmobile.entity.AnswerEntity</value>
</list>
</property>
</bean>


某一个Entity.java文件如下

@SuppressWarnings("serial")
@Entity
@Table(name="user_table",catalog="classnet")
public class UserEntity implements Serializable{

private Integer id;
private String phone;

private String username;
private String password;
private String email;
private boolean enable=true;
private String authorite; //ROLE_USER,ROLE_SUPERVISOR

@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="username",length=50,nullable=false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="phone",length=50,nullable=false)
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name="password",length=50,nullable=false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="email",length=50,nullable=false)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="enable")
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
@Column(name="authorite",length=50,nullable=false)
public String getAuthorite() {
return authorite;
}
public void setAuthorite(String authorite) {
this.authorite = authorite;
}

}


测试类如下:

public class test extends HibernateDaoSupport{
public static void main(String[] args) {
SessionFactory sf = null;
try{
Configuration cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = (Session) sf.openSession();
//          return session;
System.out.println("能打开session,那就没错了");
}catch(HibernateException e){
e.printStackTrace();
}
}
}


报错信息如下:

org.hibernate.HibernateException: /hibernate.cfg.xml not found

at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)

at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)

at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)

at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)

at test.test.main(test.java:27)

查了很久资料,最后得到了一个解释:

使用spring整合hibernate之后,sessionFactory由spring管理,所以再用hibernate的Configuration conf = new Configuration().configure();就会找不到配置文件,这时候必须用spring的sessionFactory或者使用spring提供的hibernateTemplate,用sessionFactory不能管理事务,用hibernateTemplate能管理事务。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息