首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

SpringMVC3.2+hibernate4诠注形式保存数据无效

2014-01-05 
SpringMVC3.2+hibernate4注解形式保存数据无效代码如下:web:context-paramparam-namecontextConfigLoc

SpringMVC3.2+hibernate4注解形式保存数据无效
代码如下:
web:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


applicationContext:
<!-- 配置数据源 -->  
<bean id="dataSource"  
class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver" />  
<property name="url" value="jdbc:mysql://127.0.0.1/jlmedicine" />  
<property name="username" value="root" />  
<property name="password" value="root" />  
</bean> 
<!--  配置hibernate SessionFactory-->  
<bean id="sessionFactory"  
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.jl.medicine.model"/>  
<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="hiberante.format_sql">true</prop>
<prop key="current_session_context_class">thread</prop>  
</props>  
</property>
</bean>
<bean id="transactionManager"  
class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED</prop>
<prop key="execute*">PROPAGATION_REQUIRED</prop>
<prop key="load*">PROPAGATION_REQUIRED</prop>
<prop key="merge*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean> 

Service层:

@Service("teacherService")
public class TeacherService {

@Autowired
private ExamDAOTest examdao;
public void addtest(){


ExamTopicQuestion test = new ExamTopicQuestion();
test.setMorequestion("123123213");
test.setTopicid("1111111");
test.setTopicname("adasadsa");
examdao.save(test);

}
}


DAO层:

@Repository
public class ExamDAOTest{

@Autowired
private SessionFactory sessionFactory;
private Session session;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public Session getSession() {
//事务配置后,可通过getCurrentSession方法获得session
return sessionFactory.getCurrentSession();
}
public void setSession(Session session) {
this.session = session;
}


public void save(ExamTopicQuestion test){
this.getSession().save(test);
}
}


基本代码就是这些,可以查到数据但是往数据库插数据的时候也不报错,但是数据库里就是没有数据,纠结一天了求支援。。。。。
[解决办法]
   既然用注解,把
    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager"></property>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="find*">PROPAGATION_REQUIRED</prop>
                <prop key="get*">PROPAGATION_REQUIRED</prop>
                <prop key="execute*">PROPAGATION_REQUIRED</prop>
                <prop key="load*">PROPAGATION_REQUIRED</prop>
                <prop key="merge*">PROPAGATION_REQUIRED</prop>
                <prop key="add*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
改为
 <!-- 注入事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

再加入
<!-- 自动扫描 bean -->
<context:annotation-config />
<context:component-scan base-package="zzz.xxx"/>

service上加@Transactional

@Transactional
@Service("teacherService")
public class TeacherService {
  
[解决办法]
多半是事务引起的,我用的是aop你试试看我的代码
<context:component-scan base-package="com.share.service.*">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:annotation-driven proxy-target-class="true" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="search*" read-only="true" propagation="REQUIRED" />
<tx:method name="isExist*" read-only="true" propagation="REQUIRED" />
<tx:method name="load*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>


</tx:advice>

<aop:config>
<aop:pointcut expression="execution(public * com.share.service..*.*(..))"
id="bussinessService" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />
</aop:config>


[解决办法]
还是别用这种方式控制事务了,spring这块有严重问题,对同一个表读写频繁很容易锁死数据源。

热点排行