de.lava.marvin.whaosleaguepersistence.dao.impl.BaseDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for de.lava.marvin.whaosleaguepersistence.dao.impl.BaseDaoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.lava.marvin.whaosleaguepersistence.dao.impl;

import de.lava.marvin.whaosleaguepersistence.dao.BaseDao;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author mlava
 */
@Repository
@Transactional(propagation = Propagation.REQUIRED)
public class BaseDaoImpl<T> implements BaseDao<T> {

    @PersistenceContext(unitName = "aosleaguePU")
    public EntityManager entityManager;

    @Override
    public void create(T object) {
        entityManager.persist(object);
    }

    @Override
    public T read(Class<T> entityClass, String id) {
        return entityManager.find(entityClass, Long.parseLong(id));
    }

    @Override
    public T update(T object) {
        entityManager.persist(object);
        entityManager.refresh(object);
        return (T) object;
    }

    @Override
    public void delete(T object) {
        entityManager.remove(object);
    }

}