samedi 23 mai 2020

No getters and setters are called in JPA

I am using JPA 2.1 and while persisting and retrieving the entities from database I could see no constructor of the entity is called and not getters and setters. How does the serialization and deserialization take place then from DB object to JAVA object, if getters, setters and constructor are not called

Teacher

@Entity
@Table(name="Teacher")
public class Teacher {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    int id;

    @Column
    String name;



    public Teacher(String name) {
        super();

        this.name = name;
    }

    public Teacher()
    {
        System.out.println("const");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column
    public String getName() {
        System.out.println("get Name");
        return name;
    }

    public void setName(String name) {
        System.out.println("set Name");
        this.name = name;
    }

Main

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence");
        EntityManager em1 = entityManagerFactory.createEntityManager();

        em1.getTransaction().begin();

        Teacher t = new Teacher("wick");

        em1.persist(t);

        Teacher t1= em1.find(Teacher.class, 21);


        em1.getTransaction().commit();
        em1.close();




Aucun commentaire:

Enregistrer un commentaire