我正在开发Spring Hibernate应用程序。 我试图使用session.saveOrUpdate方法更新表。 但它不是第一次工作。从第二次尝试,我能够更新数据库中的数据。 在第一次没有例外。一切都是正确的。
config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- This bean configuration only for your local tomcat deployment -->
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="${db.driverClassName}" />
<beans:property name="url" value="${db.url}" />
<beans:property name="username" value="${db.username}" />
<beans:property name="password" value="${db.password}" />
</beans:bean>
<!-- weblogic server Jndi configuration for LYRA DB Datasource -->
<!-- <beans:bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiName" value="jdbc/lyraDB" />
</beans:bean> -->
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.orange.lyra.web.model.AdminGestionProfiles</beans:value>
<beans:value>com.orange.lyra.web.model.BuzonesPermisions</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="scoreCardDao" class="com.orange.lyra.web.dao.impl.ScoreCardDaoImpl">
<beans:property name="sessionFactory"
ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="administracionDao" class="com.orange.lyra.web.dao.impl.AdminControllerDaoImpl">
<beans:property name="sessionFactory"
ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory"
ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
</beans:beans>
Modal Bean类
package com.orange.lyra.web.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "LYRA_ADMIN_GESTION_PROFILE")
public class AdminGestionProfiles {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "ID")
private String Id;
@Column(name = "PROFILE_ID")
private String profileIds;
@Column(name = "BUZONE_ID")
private String buzoneIds;
@Column(name = "PERMISOS_ID")
private String permisosIds;
@Column(name = "STATUS")
private String statusCheck;
public String getProfileIds() {
return profileIds;
}
public void setProfileIds(String profileIds) {
this.profileIds = profileIds;
}
public String getBuzoneIds() {
return buzoneIds;
}
public void setBuzoneIds(String buzoneIds) {
this.buzoneIds = buzoneIds;
}
public String getPermisosIds() {
return permisosIds;
}
public void setPermisosIds(String permisosIds) {
this.permisosIds = permisosIds;
}
public String getStatusCheck() {
return statusCheck;
}
public void setStatusCheck(String statusCheck) {
this.statusCheck = statusCheck;
}
}
ServiceImpl类
@Override
@Transactional
public String updatePermisosStatusByAdmin(String profileId, String buzoneID, String[] permisosIdList) {
List<AdminGestionProfiles> adminList = administracionDao.getAdminisotable(profileId,buzoneID);
for(int j =0;j<permisosIdList.length;j++)
{
for (AdminGestionProfiles adminGestionProfiles : adminList) {
if(permisosIdList[j].toString().equals(adminGestionProfiles.getPermisosIds())){
adminGestionProfiles.setStatusCheck("1");
}
}
}
String message = administracionDao.updatePermisosStatusByAdmin(adminList);
return message;
}
DaoImpl类
public class AdminControllerDaoImpl implements AdminControllerDao{
private static final Logger LOGGER = Logger .getLogger(AdminControllerDaoImpl.class);
/** The session factory. */
private SessionFactory sessionFactory;
/**
* Sets the session factory.
* @param sf
* the new session factory
*/
public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}
@Override
public String updatePermisosStatusByAdmin(List<AdminGestionProfiles> adminList) { LOGGER.debug("AdminControllerDaoImpl::updatePermisosStatusByAdmin::start");
try{
Session session = this.sessionFactory.getCurrentSession();
for (AdminGestionProfiles adminGestionProfiles : adminList) {
session.saveOrUpdate(adminGestionProfiles);
}
}catch(HibernateException error){
String err = error.getMessage();
LOGGER.debug("Hibernate Exception throw in updatePermisosStatusByAdmin mehthod ::" +err);
}
LOGGER.debug("AdminControllerDaoImpl::updatePermisosStatusByAdmin::End");
return "El proceso se ha realizado correctamente";
}
}
答案 0 :(得分:0)
您正在从数据库中检索管理员并更新某些值,只需调用“update()”而不是“saveOrUpdate()”,您就知道该数据库中已存在该记录。
如果这不能解决您的问题,请发布例外。
PS:很抱歉直接发帖和回答,但我还不能评论:)