我的例子:
用户.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
private int id;
private String userName;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
TraceLog.java
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class TraceLog {
private int id;
private User user;
private String tokenId;
private String variable;
private String value;
private Date traceTime;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(cascade = CascadeType.ALL)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Column
public String getTokenId() {
return tokenId;
}
public void setTokenId(String tokenId) {
this.tokenId = tokenId;
}
@Column
public String getVariable() {
return variable;
}
public void setVariable(String variable) {
this.variable = variable;
}
@Column
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Column
public Date getTraceTime() {
return traceTime;
}
public void setTraceTime(Date traceTime) {
this.traceTime = traceTime;
}
}
休眠.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sessiontest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysql</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.cpviet.example.session.model.User" />
<mapping class="com.cpviet.example.session.model.TraceLog" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private SessionFactory sessionFactory = null;
private static HibernateUtil instance = null;
private HibernateUtil() {
}
public static HibernateUtil getInstance() {
if (instance == null) {
instance = new HibernateUtil();
}
return instance;
}
public SessionFactory getSessionFactory() {
if (sessionFactory == null) {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
}
如何使用:
Session session = HibernateUtil.getInstance().getSessionFactory().openSession();
user = (User) session.get(User.class, (Integer)1);
session.close();
或者
Session session = HibernateUtil.getInstance().getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
TraceLog traceLog = new TraceLog();
traceLog.setTokenId(tokenId);
traceLog.setVariable("var1");
traceLog.setValue("val1");
traceLog.setUser(user);
traceLog.setTraceTime(new Date());
session.save(traceLog);
transaction.commit();
session.close();