top.knos.user.UserRepositoryImpl.java Source code

Java tutorial

Introduction

Here is the source code for top.knos.user.UserRepositoryImpl.java

Source

/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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 top.knos.user;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;

/**
 * Implementation fo the custom repository functionality declared in {@link UserRepositoryCustom} based on JPA. To use
 * this implementation in combination with Spring Data JPA you can either register it programatically:
 * 
 * <pre>
 * EntityManager em = ... // Obtain EntityManager
 * 
 * UserRepositoryCustom custom = new UserRepositoryImpl();
 * custom.setEntityManager(em);
 * 
 * RepositoryFactorySupport factory = new JpaRepositoryFactory(em);
 * UserRepository repository = factory.getRepository(UserRepository.class, custom);
 * </pre>
 * 
 * Using the Spring namespace the implementation will just get picked up due to the classpath scanning for
 * implementations with the {@code Impl} postfix.
 * 
 * <pre>
 * &lt;jpa:repositories base-package=&quot;com.acme.repository&quot; /&gt;
 * </pre>
 * 
 * If you need to manually configure the custom instance see {@link UserRepositoryImplJdbc} for an example.
 * 
 * @author Oliver Gierke
 * @author Thomas Darimont
 */
class UserRepositoryImpl implements UserRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    /**
     * Configure the entity manager to be used.
     * 
     * @param em the {@link EntityManager} to set.
     */
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.jpa.example.repository.UserRepositoryCustom#myCustomBatchOperation()
     */
    public List<User> myCustomBatchOperation() {

        CriteriaQuery<User> criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
        criteriaQuery.select(criteriaQuery.from(User.class));
        return em.createQuery(criteriaQuery).getResultList();
    }

    @Override
    public List<User> findUsersByNameQuery(String value) {
        TypedQuery<User> tq = em.createQuery("select u from User u where u.username = ?1", User.class);
        tq.setParameter(1, value);
        return tq.getResultList();
    }

    @Override
    public User findUserByNameQuery(String value) {
        TypedQuery<User> tq = em.createQuery("select u from User u where u.username = ?1", User.class);
        tq.setParameter(1, value);
        return tq.getSingleResult();
    }
}