我正在尝试通过Hibernate HQL Query更新记录,但导致继续加载页面。
首次尝试时更新记录没有任何问题。
对于映射我正在使用注释配置,@ Transaction在服务层中绑定,
Spring配置文件中的Hibernate配置:
<mvc:annotation-driven/>
<tx:annotation-driven />
<context:component-scan base-package="com.spring.datasolr"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<!-- Configures spring view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Configures validation message property file -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages"></property>
</bean>
<!-- Configures database property -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Configures Hibernate Sessionfactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.spring.datasolr.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
</bean>
<!-- Configures Hibernate Transaction Management -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
道上课:
@Autowired
SessionFactory sessionFactory;
private Session getSession(){
return sessionFactory.openSession();
}
@Override
public void update(User user) {
System.out.println("User ID :: "+user.getId());
Query query = getSession().createQuery("UPDATE User SET name=:name, email=:email, message=:message WHERE id=:id");
query.setParameter("name", user.getName());
query.setParameter("email", user.getEmail());
query.setParameter("message", user.getMessage());
query.setParameter("id", user.getId());
query.executeUpdate();
//sessionFactory.openSession().saveOrUpdate(user);
}
服务层:
@Transactional
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
UserDAO userDao; // This is for database
@Autowired
SolrTemplate solrTemplate;
@Resource
private UserRepository userRepository; // This is for solr
@Override
public void updateDocument(Object obj) {
User user =(User) obj;
userDao.update(user);
//solr repository, save all user in solr indexing
userRepository.save(userDao.userList());
}
}
答案 0 :(得分:1)
您的交易环境不起作用。使用此:
@Override
public void update(User user) {
System.out.println("User ID :: "+user.getId());
Session s = getSession();
Transaction t = s.beginTransaction();
Query query = s.createQuery("UPDATE User SET name=:name, email=:email, message=:message WHERE id=:id");
query.setParameter("name", user.getName());
query.setParameter("email", user.getEmail());
query.setParameter("message", user.getMessage());
query.setParameter("id", user.getId());
query.executeUpdate();
t.commit();
}
因为:如果您调用它两次,则第一次尝试成功但事务永远不会关闭。因此,更改在事务中,但尚未在数据库中。如果您从会话中读取它首先查看当前事务所具有的更改,那么您认为它们已存储但它们仅在事务中。
答案 1 :(得分:0)
您还可以使用主键更新到Hibernate farmework。
ObjectofClassName classobj = HibernateUtil.currentsession.get(ClassName.class,primareyKey);
获得classobj之后,您可以使用hibernate进行更新。
答案 2 :(得分:-2)
ObjectofClassName classobj = HibernateUtil.currentsession.get(ClassName.class,primareyKey);
Transaction tx = HibernateUtil.currentsession().beginTransaction();
classobj .setUpdatedValue("value");
HibernateUtil.currentsession().update(classobj);
tx.commet();