⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basedao.java

📁 开源的OpenId的一个java实现
💻 JAVA
字号:
/*                                                                              * Copyright 2005,2006 WSO2, Inc. http://www.wso2.org *                                                                              * 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 org.wso2.solutions.identity.persistence.dao;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.exception.ConstraintViolationException;import org.wso2.solutions.identity.IdentityProviderConstants;import org.wso2.solutions.identity.i18n.Messages;import org.wso2.solutions.identity.persistence.DuplicateEntityException;import org.wso2.solutions.identity.persistence.HibernateConfig;import org.wso2.solutions.identity.persistence.dataobject.AbstractDataObject;import java.util.Date;public class BaseDAO {	public static final Log log = LogFactory.getLog(BaseDAO.class);    protected static final Messages messages = Messages            .getInstance(IdentityProviderConstants.RESOURCES);    HibernateConfig hbConfig = null;    public BaseDAO(HibernateConfig config) {        hbConfig = config;    }    public Long create(AbstractDataObject trasientInstance)            throws DuplicateEntityException {        Session session = hbConfig.getCurrentSession();        Transaction tx = session.beginTransaction();        try {            trasientInstance.setLastUpdatedTime(new Date());            session.persist(trasientInstance);            session.flush();            tx.commit();        } catch (ConstraintViolationException e) {            String msg = "createDuplicateEntity";            log.warn(msg, e);            throw new DuplicateEntityException(msg, e);        } catch (Exception e) {            e.printStackTrace();            tx.rollback();            throw new RuntimeException(messages                    .getMessage("errorCreatingEntity"), e);        } finally {            hbConfig.closeSession();        }        return trasientInstance.getId();    }    public Long createOrUpdate(AbstractDataObject instance) {        Session session = hbConfig.getCurrentSession();        Transaction tx = session.beginTransaction();        try {            instance.setLastUpdatedTime(new Date());            session.saveOrUpdate(instance);            session.flush();            tx.commit();        } catch (ConstraintViolationException e) {            String msg = "createDuplicateEntity";            log.warn(msg, e);            throw new RuntimeException(msg, e);        } catch (Exception e) {            e.printStackTrace();            tx.rollback();            throw new RuntimeException(messages                    .getMessage("errorCreatingOrUpdatingEntity"), e);        } finally {            hbConfig.closeSession();        }        return instance.getId();    }    public void update(AbstractDataObject abstractDO) {        Session session = hbConfig.getCurrentSession();        Transaction tx = session.beginTransaction();        try {            abstractDO.setLastUpdatedTime(new Date());            session.merge(abstractDO);            session.flush();            tx.commit();        } catch (Throwable e) {            tx.rollback();            String msg = messages.getMessage("errorUpdating");            log.error(msg, e);            throw new RuntimeException(msg, e);        } finally {            hbConfig.closeSession();        }    }    public void delete(AbstractDataObject abstractDO) {        Session session = hbConfig.getCurrentSession();        Transaction tx = session.beginTransaction();        try {            session.delete(abstractDO);            session.flush();            tx.commit();        } catch (Throwable e) {            tx.rollback();            String msg = messages.getMessage("errorDeleting");            log.error(msg, e);            throw new RuntimeException(msg, e);        } finally {            hbConfig.closeSession();        }    }    /**     * Executes a single DML statement and returns the number of rows effected.     *      * @param stmt     * @param variable -     *            Variable     * @param value -     *            Value     * @return     */    public int executeSingleDMLStatement(String stmt, String variable,            String value) {        Session session = hbConfig.getCurrentSession();        Transaction tx = session.beginTransaction();        int effectedEntities = 0;        try {            effectedEntities = session.createQuery(stmt).setString(variable,                    value).executeUpdate();            tx.commit();        } catch (Throwable e) {            tx.rollback();            String msg = messages.getMessage("errorUpdating");            log.error(msg, e);            throw new RuntimeException(msg, e);        } finally {            hbConfig.closeSession();        }        return effectedEntities;    }    public AbstractDataObject getDataObject(String objClassName, Long val) {        String stmt = "from " + objClassName + " as obj where obj.id =" + val;        Session session = hbConfig.getCurrentSession();        Transaction tx = session.beginTransaction();        AbstractDataObject obj = null;        try {            obj = (AbstractDataObject) session.createQuery(stmt).uniqueResult();            tx.commit();        } catch (Throwable e) {            tx.rollback();            String msg = messages.getMessage("errorReadingFromDatabase");            log.error(msg, e);            throw new RuntimeException(msg, e);        } finally {            hbConfig.closeSession();        }        return obj;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -