All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.osgiliath.feature.itest.persistence.daos.impl.HelloJpaRepository Maven / Gradle / Ivy

package net.osgiliath.feature.itest.persistence.daos.impl;

/*
 * #%L
 * net.osgiliath.hello.model.jpa
 * %%
 * Copyright (C) 2013 Osgiliath
 * %%
 * 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.
 * #L%
 */

import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.ops4j.pax.cdi.api.OsgiServiceProvider;

import net.osgiliath.feature.itest.persistence.daos.HelloRepository;
import net.osgiliath.feature.itest.persistence.entities.HelloEntity;
import net.osgiliath.feature.itest.persistence.entities.HelloEntity_;
import net.osgiliath.module.spring.data.jpa.DelegatingSimpleJpaRepository;

/**
 * Spring data jpa repository declaration.
 * 
 * @author charliemordant
 */
@OsgiServiceProvider(classes = { HelloRepository.class })
@Singleton
@Transactional
public class HelloJpaRepository extends DelegatingSimpleJpaRepository implements HelloRepository {
	/**
	 * Entity manager.
	 */

	@PersistenceContext(unitName = "myTestPu")
	private EntityManager em;
	@PersistenceUnit( unitName = "myTestPu")
	private EntityManagerFactory emf;

	/**
	 * Finds by helloMessage.
	 * 
	 * @param message
	 *            the message to find elements from
	 * @return all corresponding entities
	 */
	@Override
	public final Collection findByHelloObjectMessage(final String message) {
		final CriteriaBuilder builder = this.em.getCriteriaBuilder();
		final CriteriaQuery criteria = builder.createQuery(HelloEntity.class);
		final Root helloObject = criteria.from(HelloEntity.class);
		criteria.select(helloObject);
		final Predicate where = builder.equal(helloObject.get(HelloEntity_.helloMessage), message);
		criteria.where(where);
		final TypedQuery query = this.em.createQuery(criteria);
		return query.getResultList();
	}

	@Override
	@PostConstruct
	public void postConstruct() {
		instanciateDelegateRepository(emf, em, HelloEntity.class);
	}

	@Override
	public List findAll() {
		final CriteriaBuilder builder = this.em.getCriteriaBuilder();
		final CriteriaQuery criteria = builder.createQuery(HelloEntity.class);
		final Root helloObject = criteria.from(HelloEntity.class);
		criteria.select(helloObject);
		final TypedQuery query = this.em.createQuery(criteria);
		return query.getResultList();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy