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

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能管理事务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值