org.antbear.jee.wicket.persistence.PersonService.java Source code

Java tutorial

Introduction

Here is the source code for org.antbear.jee.wicket.persistence.PersonService.java

Source

/*
 * Copyright 2011 Marcus Geiger.
 *
 * 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 org.antbear.jee.wicket.persistence;

import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.antbear.jee.wicket.model.Person;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * DAO for accessing Persons via JPA.
 *
 * @author Marcus Geiger
 */
@Repository
@Transactional(readOnly = true)
public class PersonService {

    @PersistenceContext
    private EntityManager em;

    public int size() {
        Long ll = (Long) em.createQuery("select count(*) from Person").getSingleResult();
        return ll.intValue();
    }

    public Person find(Long id) {
        return em.find(Person.class, id);
    }

    public List<Person> findAll() {
        return em.createNamedQuery("person.findall", Person.class).getResultList();
    }

    public Iterator<Person> iterator(int first, int count) {
        TypedQuery<Person> query = em.createNamedQuery("person.findall", Person.class);
        query.setFirstResult(first);
        query.setMaxResults(count);
        return query.getResultList().iterator();
    }

    @Transactional // Overrides readOnly flag at class level
    public void persist(Person person) {
        em.persist(person);
    }

    @Transactional // Overrides readOnly flag at class level
    public void merge(Person person) {
        em.merge(person);
    }

}