我编写了一个简单的 Hibernate 程序,但我对流程和构造函数的使用感到困惑。
这是主要的 POJO 类:
package mypack;
import org.apache.log4j.Logger;
public class Emp {
String name,job;
int id,salary;
public Emp()
{
super();
System.out.println("Inside Default constructor"+this);
}
public Emp(String name, String job, int salary) {
super();
System.out.println("Inside parameterised constructor"+this);
this.name = name;
this.job = job;
this.salary = salary;
}
public String getName() {
System.out.println("Inside Get name"+this);
return name;
}
public void setName(String name) {
System.out.println("Inside set name"+this);
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
这是ObjectSaver
类:
package mypack;
import org.hibernate.*;
public class ObjSaver {
public static void main(String[] args) {
Emp e1=new Emp("A","job1",45000);
Emp e2=new Emp("B","Job2",45035);
Emp e3=new Emp("C","Job3",44585);
Emp e4=new Emp("4","Job4",44582545);
System.out.println("Saving Emp Objects");
Session session=Myfactory.getSession();
System.out.println("session obtained");
Transaction t1=session.beginTransaction();
System.out.println("Transaction begin");
session.save(e1);
session.save(e2);
session.save(e3);
session.save(e4);
t1.commit();
session.close();
System.out.println("Successfully Saved");
}
}
MyFactory
类:
package mypack;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class Myfactory
{
private static SessionFactory factory;
static
{
System.out.println("Loading configuration...");
Configuration cfg=new Configuration().configure();
//Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
System.out.println("Configuration loaded");
factory = cfg.buildSessionFactory();
System.out.println("SessionFactory Created");
}
public static Session getSession()
{
System.out.println("returning session");
return factory.openSession();
}
}
映射文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="mypack.Emp" table="Employee">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="job"/>
<property name="salary"/>
</class>
</hibernate-mapping>
输出:
Inside parameterised constructormypack.Emp@1bab50a
Inside parameterised constructormypack.Emp@c3c749
Inside parameterised constructormypack.Emp@150bd4d
Inside parameterised constructormypack.Emp@1bc4459
Saving Emp Objects
Loading configuration...
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Configuration loaded
Inside Default constructormypack.Emp@fdb00d
Inside Default constructormypack.Emp$$EnhancerByCGLIB$$aa36f81a@17a4989
Inside Default constructormypack.Emp@659db7
Inside Get namemypack.Emp@659db7
Inside set namemypack.Emp@659db7
SessionFactory Created
returning session
session obtained
Transaction begin
Inside Get namemypack.Emp@1bab50a
Inside Get namemypack.Emp@c3c749
Inside Get namemypack.Emp@150bd4d
Inside Get namemypack.Emp@1bc4459
Inside Get namemypack.Emp@1bab50a
Inside Get namemypack.Emp@c3c749
Inside Get namemypack.Emp@150bd4d
Inside Get namemypack.Emp@1bc4459
Inside Get namemypack.Emp@150bd4d
Inside Get namemypack.Emp@c3c749
Inside Get namemypack.Emp@1bc4459
Inside Get namemypack.Emp@1bab50a
Hibernate: insert into Employee (name, job, salary, id) values (?, ?, ?, ?)
Hibernate: insert into Employee (name, job, salary, id) values (?, ?, ?, ?)
Hibernate: insert into Employee (name, job, salary, id) values (?, ?, ?, ?)
Hibernate: insert into Employee (name, job, salary, id) values (?, ?, ?, ?)
Successfully Saved
为什么默认构造函数运行了3次?为什么 setter 和 getter 在默认构造函数之后运行?谁能解释一下流程?
最佳答案
Hibernate
需要所有持久类的无参构造函数。
因为 Hibernate
使用 Java Reflection 为您创建对象,而 Java Reflection 会调用默认构造函数,因此您使用 save 方法保存的每个对象都会调用您的默认构造函数。
Hibernate
每当您从数据库读取数据时,Hibernate 都会使用 setter 方法来初始化实体,并在您持久化到数据库时,使用 getter 方法来获取要持久化的实际值。
由于您只是坚持,所以应该只调用 getter。
关于java - Hibernate中的操作流程是怎样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31573507/