cognition.common.data.BaseDao.java Source code

Java tutorial

Introduction

Here is the source code for cognition.common.data.BaseDao.java

Source

/*
Designed and developed by Ismail E. Kartoglu
Copyright 2015 King's College London
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package cognition.common.data;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BaseDao {

    @Autowired
    @Qualifier("sourceSessionFactory")
    private SessionFactory sourceSessionFactory;

    @Autowired
    @Qualifier("targetSessionFactory")
    private SessionFactory targetSessionFactory;

    private static Logger logger = Logger.getLogger(BaseDao.class);

    public SessionFactory getSourceSessionFactory() {
        return sourceSessionFactory;
    }

    public void setSourceSessionFactory(SessionFactory sourceSessionFactory) {
        this.sourceSessionFactory = sourceSessionFactory;
    }

    public SessionWrapper createSourceSession() {
        return new SessionWrapper(sourceSessionFactory.openSession());
    }

    public SessionWrapper createTargetSession() {
        return new SessionWrapper(targetSessionFactory.openSession());
    }

    public void setTargetSessionFactory(SessionFactory targetSessionFactory) {
        this.targetSessionFactory = targetSessionFactory;
    }

    public void executeSQLQueryForSource(String sqlQuery) {
        SessionWrapper sessionWrapper = createSourceSession();
        try {
            SQLQuery query = sessionWrapper.createSQLQuery(sqlQuery);
            query.executeUpdate();
        } finally {
            sessionWrapper.closeSession();
        }
    }

    public void executeSQLQueryForTarget(String sqlQuery) {
        SessionWrapper currentTargetSession = createTargetSession();
        try {
            SQLQuery query = currentTargetSession.createSQLQuery(sqlQuery);
            query.executeUpdate();
        } finally {
            currentTargetSession.closeSession();
        }
    }

    public List getSQLResultFromSource(String sqlQuery) {
        SessionWrapper sessionWrapper = createSourceSession();
        try {
            SQLQuery query = sessionWrapper.createSQLQuery(sqlQuery);
            return query.list();
        } finally {
            sessionWrapper.closeSession();
        }
    }

    public List getSQLResultFromTarget(String sqlQuery) {
        SessionWrapper sessionWrapper = createTargetSession();
        try {
            SQLQuery query = sessionWrapper.createSQLQuery(sqlQuery);
            return query.list();
        } finally {
            sessionWrapper.closeSession();
        }
    }

}