在Hibernate中没有调用getter和setter

I am using Hibernate 4.3.6 and while persisting and retrieving the entities from database I could see only the 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 and setters are not called

老师

@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;
    }

主要

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();
评论