PostInsertEventListener does not work when I insert a new element using Main class

0

I have a problem with the EventListeners of Hibernate. First I will tell you what my application is, I will leave the class code and then I will explain the problem.

I am developing in Spring in its version 4, and I use Hibernate in its version 5. First I made an entity called Person to store data of people in a database.

@Entity
@Table(name="Person")
public class Person implements Serializable{

    private static final long serialVersionUID = 1072254302801682898L;

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String name;

    private String country;
    //Getters y setters

Following the examples of the tutorials, create the DAO interfaces and the services that would allow me to save the data.

public interface PersonService {

    public void addPerson(Person p);
    public void updatePerson(Person p);
    public void removePerson(int id);

}

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonDAO personDAO;

    @Autowired
    public void setPersonDAO(PersonDAO personDAO) {
        this.personDAO = personDAO;
    }

    @Override
    @Transactional
    public void addPerson(Person p) {
        this.personDAO.addPerson(p);
    }
    //EL update y el delete ya estan implementados

DAO classes

public interface PersonDAO {

    public void addPerson(Person p);
    public void updatePerson(Person p);
    public void removePerson(int id);
}

@Repository
public class PersonDAOImpl implements PersonDAO {

    private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional
    public void addPerson(Person p) {
        Session session = this.sessionFactory.getCurrentSession();
        session.persist(p);
        logger.info("Person saved successfully, Person Details=" + p);
    }

And with the PostInserteventListener, every time I insert a person I keep a record of the executed operation:

public class InsertarEvento implements PostInsertEventListener {
    private static final long serialVersionUID = 1L;
    Registro reg;
    Salvador sav;
    private Date date;
    private String operation;

    @Override
    public void onPostInsert(PostInsertEvent event) {
        if (event.getEntity() instanceof Person) {
            Object p = event.getEntity();           
            System.out.println("+++Person inserted+++" + p);
            operation = "Insertada una persona";
            date = Calendar.getInstance().getTime();
            reg = new Registro(operation, date);
            sav = new Salvador();
            sav.saveData(reg);
            }       
    }

So far so good, every time I insert a person (through a view that contains a form) the listener is activated. However now I have many more entities to insert and not to see more. nor classes DAO again, I made a class Main where I create the objects and persist them. Here is an example of how one person is now saved:

public class Main {
    public static void main(String[] args){

        System.out.println("New person");
        Person p = new Person();
        p.setName("pepe");
        p.setCountry("mexico");
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        ses.beginTransaction();
        ses.save(p);
        ses.getTransaction().commit();
        ses.close();
        System.out.println(p.getId());
    }
}

With this I am able to insert people in the database, but the PostInsertEventListener that I implemented is not activated and I can not register which operation was executed on which entity.

My question is: Why is not the PostInsert running in the case of using a Main class?

    
asked by Ethan 29.03.2017 в 17:07
source

0 answers