2 * Copyright 2016 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.aai.esr.dao;
19 import io.dropwizard.hibernate.AbstractDAO;
20 import io.dropwizard.util.Generics;
22 import org.hibernate.Criteria;
23 import org.hibernate.HibernateException;
24 import org.hibernate.Query;
25 import org.hibernate.Session;
26 import org.hibernate.SessionFactory;
27 import org.hibernate.criterion.Restrictions;
28 import org.onap.aai.esr.exception.ExtsysException;
29 import org.onap.aai.esr.util.HqlFactory;
31 import java.util.List;
35 * a base class for Hibernate DAO classes.<br>
36 * provide the common methods to create,delete,update and query data
40 public class BaseDao<T> extends AbstractDAO<T> {
45 * @param sessionFactory session Factory
47 public BaseDao(SessionFactory sessionFactory) {
48 super(sessionFactory);
49 this.sessionFactory = sessionFactory;
50 this.entityClass = Generics.getTypeParameter(getClass());
53 public String[] excludeProperties;
54 private SessionFactory sessionFactory;
55 protected Session session;
56 private final Class<?> entityClass;
59 protected Session currentSession() {
66 * @param data the object to update
67 * @throws ExtsysException when db abnormal
69 public void update(T data, String filter) throws ExtsysException {
71 String hql = HqlFactory.getUpdateHql(data, excludeProperties, filter);
73 Query query = this.session.createQuery(hql);
74 query.executeUpdate();
76 } catch (Exception error) {
77 transactionRollBack();
78 throw new ExtsysException("", "error while updating data.errorMsg:" + error.getMessage());
87 * @param data the object to delete
88 * @throws ExtsysException when db abnormal
90 public void delete(T data) throws ExtsysException {
93 this.session.delete(data);
95 } catch (Exception error) {
96 transactionRollBack();
97 throw new ExtsysException("", "error while deleting data.errorMsg:" + error.getMessage());
106 * @param data the object to create
108 * @throws ExtsysException when db abnormal
111 public T create(T data) throws ExtsysException {
116 } catch (HibernateException error) {
117 transactionRollBack();
118 throw new ExtsysException("", "error while creating data.errorMsg:" + error.getMessage());
126 * query entity by condition.
128 * @param unionHql query condition.
130 * @throws ExtsysException when db abnormal
133 public List<T> unionQuery(String unionHql) throws ExtsysException {
137 Query query = this.session.createQuery(unionHql);
140 } catch (Exception error) {
141 transactionRollBack();
142 throw new ExtsysException("", "error while union query data.errorMsg:" + error.getMessage());
150 * delete entity by condition.
152 * @param unionHql delete condition.
154 * @throws ExtsysException when db abnormal
157 public int unionDelete(String unionHql) throws ExtsysException {
161 Query query = this.session.createQuery(unionHql);
162 num = query.executeUpdate();
164 } catch (Exception error) {
165 transactionRollBack();
166 throw new ExtsysException("", "error while union query data.errorMsg:" + error.getMessage());
174 * query entity by condition map.
176 * @param queryParams the condition map used to query objects
178 * @throws ExtsysException when db abnormal
180 @SuppressWarnings("unchecked")
181 public List<T> query(Map<String, String> queryParams) throws ExtsysException {
182 List<T> result = null;
185 Criteria criteria = this.session.createCriteria(entityClass);
186 for (String key : queryParams.keySet()) {
187 criteria.add(Restrictions.eq(key, queryParams.get(key)));
189 result = (List<T>) criteria.list();
191 } catch (HibernateException error) {
192 throw new ExtsysException("", "error while querying data.errorMsg:" + error.getMessage());
199 protected void beginTransaction() {
200 this.session = this.sessionFactory.openSession();
201 this.session.beginTransaction();
204 protected void closeTransaction() {
205 this.session.getTransaction().commit();
208 protected void closeSession() {
209 this.session.close();
212 protected void transactionRollBack() {
213 this.session.getTransaction().rollback();