0
我是新來冬眠我寫我的第一個休眠代碼,我無法理解的輸出與它的代碼是:的Hibernate代碼Understandig輸出
持久類:
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>
客戶端代碼:
package com.andoi.hibernate;
import org.hibernate.cfg.*;
import org.hibernate.*;
public class SaveInTable {
public static void main(String[] args){
Transaction tx=null;
try{
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
tx=session.beginTransaction();
}catch(HibernateException e){
if(tx!=null)
tx.rollback();
e.printStackTrace();
}
}
}
,並輸出爲:
Customers->dc
getCid()
Customers->dc
getCname()
getEmail()
getPhone()
setCname()
setEmail()
setPhone()
我的問題是,爲什麼在創建我的執着類對象和getter和setter方法被調用。