无法将自动装配的SessionFactory转换为dao

时间:2016-04-16 17:51:18

标签: java spring hibernate configuration-files sessionfactory

我尝试自动连接会话工厂到dao layer.I有错误:java.lang.NoClassDefFoundError: 我锁定了更多配置,但没有人为我工作。 这是我的道:

  @Component
        public class UserDaoImpl implements UserDao{
    @Autowired 
   private SessionFactory sessionFactory;  


  @Override
      public void saveUser(User user) {
    sessionFactory.getCurrentSession().save(user);
   }

我有hibernate配置文件:

<hibernate-configuration>
   <session-factory>
  <property    name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306 /vezba2?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<mapping class="com.entiti.User"/>
<mapping class="com.entiti.Rezervacija"/>
   </session-factory>
</hibernate-configuration>

我用bean创建xml文件:

             <context:annotation-config/>

    <bean id="user" class="com.dao.UserDaoImpl">
     <property name="sessionFactory" ref="sessionFactory"></property>
      </bean>
     <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="configLocation">
      <value>classpath*:hibernate.cfg.xml</value>
      </property>
     </bean>  

如果有人知道我的错在哪里,请帮忙。

1 个答案:

答案 0 :(得分:0)

由于您使用的是Spring,最好不要忘记使用hibernate.cfg.xml文件,而是使用您需要的设置填充xml文件。如果有帮助,请参阅下面的xml。 此示例使用c3p0作为连接池的数据源。 TransactionTemplate也可用,但已注释掉。如果需要,请取消注释。

<context:annotation-config/>
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" > 
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/vezba2"/>        
    <property name="user" value="root"/>
    <property name="password" value="root"/>        
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
</bean> 
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" depends-on="myDataSource">
    <property name="dataSource" ref="myDataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>               
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop> 
            <prop key="hibernate.connection.charSet">UTF-8</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.entiti.User</value>
            <value>com.entiti.Rezervacija</value>                       
        </list>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="mySessionFactory"/>
    </property>
</bean>   
<!--<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" p:transactionManager-ref="transactionManager"/>-->