package DAO;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.PersistenceException;
import jpa.EntityManagerHelper;

import java.io.Serializable;

public abstract class TousDAOImpl<T, Id extends Serializable> implements Tous<T, Id> {
    protected EntityManager em;
    private Class<T> entityClass;

    public TousDAOImpl() {
        this.em = EntityManagerHelper.getEntityManager();
    }

    @Override
    public T save(T t) {
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            em.persist(t);
            tx.commit();
            return t;
        } catch (PersistenceException e) {
            throw new PersistenceException(e.getMessage());
        }
    }
}

