Java tutorial
/* * Copyright (c) 2016. Sunghyouk Bae <sunghyouk.bae@gmail.com> * 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 debop4k.data.orm.hibernate.dao; import debop4k.core.asyncs.Asyncx; import lombok.Getter; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import nl.komponents.kovenant.Promise; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Projections; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.concurrent.Callable; /** * Hibernate ? Thread-safe , ? Session? ? ?? . * * @author sunghyouk.bae@gmail.com */ @Slf4j @Getter @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Transactional @SuppressWarnings("unchecked") public class HibernateAsyncDao extends HibernateDao { /** * ? ? {@link DetachedCriteria} ?? ROW COUNT . */ public Promise<Long, Exception> countAsync(@NonNull DetachedCriteria dc) { return uniqueResultAsync(dc.setProjection(Projections.rowCount())); } /** * ? ? Criteria#uniqueResult . */ public <T> Promise<T, Exception> uniqueResultAsync(@NonNull final DetachedCriteria dc) { return Asyncx.future(new Callable<T>() { @Override public T call() throws Exception { Session sess = getSf().openSession(); try { return (T) dc.getExecutableCriteria(sess).uniqueResult(); } finally { sess.close(); } } }); } }