Java 理解Hibernate代码的输出

Java 理解Hibernate代码的输出,java,hibernate,Java,Hibernate,我是hibernate新手我已经编写了我的第一个hibernate代码,但我无法理解与之相关的输出代码是: 持久性类: package com.andoi.hibernate; public class Customers { public int cid; //Primary key public String cname; public String email; public long phone; public Customers(){

我是hibernate新手我已经编写了我的第一个hibernate代码,但我无法理解与之相关的输出代码是:

持久性类:

package com.andoi.hibernate;

public class Customers {
    public int cid; //Primary key
    public String cname;
    public String email;
    public long phone;


    public Customers(){
        System.out.println("Customers->dc");
    }
    public Customers(String cname,String email,long phone){
        System.out.println("Customers->three arg");
        this.cname=cname;
        this.email=email;
        this.phone=phone;
    }
    public int getCid() {
        System.out.println("getCid()");
        return cid;
    }
    public void setCid(int cid) {
        System.out.println("setCid()");
        this.cid = cid;
    }
    public String getCname() {
        System.out.println("getCname()");
        return cname;
    }
    public void setCname(String cname) {
        System.out.println("setCname()");
        this.cname = cname;
    }
    public String getEmail() {
        System.out.println("getEmail()");
        return email;
    }
    public void setEmail(String email) {
        System.out.println("setEmail()");
        this.email = email;
    }
    public long getPhone() {
        System.out.println("getPhone()");
        return phone;
    }
    public void setPhone(long phone) {
        System.out.println("setPhone()");
        this.phone = phone;
    }
}
hibernate映射文档:

<hibernate-mapping package="com.andoi.hibernate">
<class name="Customers" table="jlccustomers">
<id name="cid" column="cid"  type="int">
<generator class="increment"/>
</id>
<property name="cname" column="cname" type="string"/>
<property name="email" column="email" type="string"/>
<property name="phone" column="phone" type="long"/>
</class>

</hibernate-mapping>
输出为:

Customers->dc
getCid()
Customers->dc
getCname()
getEmail()
getPhone()
setCname()
setEmail()
setPhone()

我的问题是为什么要创建持久性类对象并调用getter和setter。

当您创建会话工厂时,hibernate会加载配置文件hibernate.cfg.xml并解析其中的每一行

现在,您将在该配置文件中获得映射文件的详细信息,因此hibernate将解析每个映射文件*.hbm.xml文件,并尝试验证映射信息是否正确

因此,基于hbm文件中的映射信息,它会检查Java类是否存在并加载该类的实例,然后使用Java类中可用的字段验证hibernate映射文件中映射的每个属性,并确保每个字段都有适当的setter和getter方法。这就是您看到Java类的默认构造函数、getter和setter方法调用的原因。假设您在Java类中有一个字段,其映射在hbm文件中不存在,那么hibernate将不会查找该字段及其相应的getter和setter方法

Customers->dc
getCid()
Customers->dc
getCname()
getEmail()
getPhone()
setCname()
setEmail()
setPhone()