com.siriusit.spezg.multilib.storage.jpa.JpaLoanRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.siriusit.spezg.multilib.storage.jpa.JpaLoanRepository.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.siriusit.spezg.multilib.storage.jpa;

import java.util.Date;
import java.util.List;

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

import org.springframework.stereotype.Repository;

import com.siriusit.spezg.multilib.domain.Loan;
import com.siriusit.spezg.multilib.domain.Person;
import com.siriusit.spezg.multilib.domain.Unit;
import com.siriusit.spezg.multilib.domain.jpa.LoanDomainObject;
import com.siriusit.spezg.multilib.storage.LoanRepository;

/**
 * 
 * @author Thao Nguyen <thao.nguyen@SiriusIT.com>
 */

@Repository("loanRepository")
public class JpaLoanRepository implements LoanRepository {

    @PersistenceContext(unitName = "multilib-db")
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

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

    @Override
    public Loan createLoan(Person loaner, Unit unit, Date dateOut, Date dateIn, Date deadline) {
        Loan loan = new LoanDomainObject(loaner, unit, dateOut, dateIn, deadline);
        entityManager.persist(loan);
        return loan;
    }

    @Override
    public void updateLoan(Loan loan) {
        entityManager.persist(loan);
    }

    @Override
    public void deleteLoan(Loan loan) {
        entityManager.remove(loan);
    }

    @Override
    public List<? extends Loan> findLoanUnitsBy(Person loaner) {
        TypedQuery<LoanDomainObject> query = entityManager.createNamedQuery("findLoanUnitsByPerson",
                LoanDomainObject.class);
        query.setParameter("loaner", loaner);
        return query.getResultList();
    }

    @Override
    public List<? extends Loan> findLoansByUnit(Unit unit) {
        TypedQuery<LoanDomainObject> query = entityManager.createNamedQuery("findLoanByUnit",
                LoanDomainObject.class);
        query.setParameter("unit", unit);
        return query.getResultList();
    }

    @Override
    public List<? extends Loan> findLoanByOwner(Person owner) {
        TypedQuery<LoanDomainObject> query = entityManager.createNamedQuery("findLoanByOwner",
                LoanDomainObject.class);
        query.setParameter("owner", owner);
        return query.getResultList();
    }

}