net.osgiliath.jpa.repository.impl.HelloJpaRepository.java Source code

Java tutorial

Introduction

Here is the source code for net.osgiliath.jpa.repository.impl.HelloJpaRepository.java

Source

package net.osgiliath.jpa.repository.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.persistence.EntityManager;
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 net.osgiliath.jpa.model.HelloEntity;
import net.osgiliath.jpa.repository.HelloRepository;

import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

//TODO Spring data jpa repository declaration
public class HelloJpaRepository extends SimpleJpaRepository<HelloEntity, Long> implements HelloRepository {

    private EntityManager entityManager;

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public HelloJpaRepository(Class<HelloEntity> domainClass, EntityManager em) {
        super(domainClass, em);
        setEntityManager(em);
    }

    @Override
    public Collection<? extends HelloEntity> findByHelloObjectMessage(String message_p) {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<HelloEntity> cq = cb.createQuery(HelloEntity.class);
        Root<HelloEntity> helloObject = cq.from(HelloEntity.class);
        cq.select(helloObject);
        Predicate where = cb.equal(helloObject.get("helloMessage"), message_p);
        cq.where(where);
        TypedQuery<HelloEntity> q = entityManager.createQuery(cq);
        List<HelloEntity> result = q.getResultList();
        return result;
    }

    @Override
    public <S extends HelloEntity> S save(S entity) {
        return super.save(entity);
    }

    @Override
    public List<HelloEntity> findAll() {
        return super.findAll();
    }

    @Override
    public void deleteAll() {
        super.deleteAll();
    }

}