0

I am working on Spring Hibernate application. There i am trying to update Table to using session.saveOrUpdate method. But it is not working in very first time. from second attempt i am able to update data in DB. In first time no exception is coming. Everything is correct.

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 class

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 class

@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 class

    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";
            }
    }
5
  • Can you post your DaoImpl class, you posted the service twice. Jul 15, 2016 at 9:35
  • Sorry, Now you can get DaoImpl class
    – Gaurav
    Jul 15, 2016 at 9:58
  • Missing transactional annotation Jul 15, 2016 at 10:06
  • transactional annotation is declared in ServiceImpl class
    – Gaurav
    Jul 15, 2016 at 10:09
  • Can you try using merge() instead of saveOrUpdate() Jul 15, 2016 at 10:40

1 Answer 1

0

You are retrieving the Admins from the DB and updating some value, just call "update()" instead of "saveOrUpdate()", you know that the record already exists in the DB.

If this don't solve your problem post the exception please.

PS: Sorry for posting and answer directly, but i can't comment yet :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.