// model
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class Teacher {
private int id;
private String name;
private String title;
private String yourWifeName;
private Date birthDate;
private boolean good;
private Gender gender;
@Enumerated(EnumType.STRING)
@Column(length = 10)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
@Transient
@Column(length = 10)
public String getYourWifeName() {
return yourWifeName;
}
public void setYourWifeName(String yourWifeName) {
this.yourWifeName = yourWifeName;
}
@Id
@GeneratedValue // 主键生成策略
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length = 10)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(length = 10)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
// test
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class HibernateCoreAPITest {
private static SessionFactory sessionFactory;
@BeforeClass
public static void beforeClass() { // 全局只需要一个 SessionFactory
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sessionFactory.close();
}
@Test
public void testSchemaExport() { // 导出表
new SchemaExport(new AnnotationConfiguration().configure()).create(
true, true);
// 第一个输出SQL语句 第二个导出表
}
@Test
public void testTeacherSave() {
Teacher t = new Teacher();
t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date());
// Session session = sessionFactory.openSession(); //
// openSession每次new新的session
Session session = sessionFactory.getCurrentSession();
// getCurrentSession从上下文找,由<property
// name="current_session_context_class">thread</property>控制
session.beginTransaction();
session.save(t);
Session session2 = sessionFactory.getCurrentSession();
System.out.println(session == session2); // true
session.getTransaction().commit(); // 提交关闭session
Session session3 = sessionFactory.getCurrentSession();
System.out.println(session == session3); // false
}
@Test
public void testSaveWith3State() {
Teacher t = new Teacher(); // transient 内存中一个对象,没有持久化标识,缓存和数据库中没有
t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date());
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
System.out.println(t.getId()); // persistent 拥有持久化标识,缓存和数据库中有
session.getTransaction().commit(); // detached 上下文被关闭,缓存没有,数据库中有
System.out.println(t.getId());
}
@Test
public void testDelete() { // 只要有ID号就可以删除,transient状态没ID
Teacher t = new Teacher();
t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date());
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
System.out.println(t.getId());
session.getTransaction().commit();
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.delete(t); // 删除detached状态对象
session2.getTransaction().commit();
}
@Test
public void testDelete2() {
Teacher t = new Teacher();
t.setId(2); // 只要有ID号就可以删除,手动设置ID
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.delete(t);
session2.getTransaction().commit();
}
@Test
public void testLoad() { // 先从缓存查找
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.load(Teacher.class, 24); // 默认支持lazy,生成代理
System.out.println(t.getName()); // 要用到对象时才真正加载对象
session.getTransaction().commit(); // 关闭session
System.out.println(t.getClass());
// class com.bjsxt.hibernate.Teacher_$$_javassist_1(代理类),为teacher的一个子类
}
@Test
public void testGet() { // 先从缓存查找
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 24); // 直接返回加载对象
session.getTransaction().commit();
System.out.println(t.getName());
System.out.println(t.getClass()); // class com.bjsxt.hibernate.Teacher
}
@Test
public void testUpdate1() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 24); // persistent
session.getTransaction().commit(); // detached
t.setName("zhangoshi");
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.update(t);
session2.getTransaction().commit();
}
@Test
public void testUpdate2() {
Teacher t = new Teacher();
t.setName("zhanaoshi");
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.update(t); // 不能更新transient状态对象
session2.getTransaction().commit();
}
@Test
public void testUpdate3() {
Teacher t = new Teacher();
t.setId(1);
t.setName("zhanaoshi");
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.update(t);
session2.getTransaction().commit();
}
@Test
public void testUpdate4() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 41);
t.setName("zhangsan2"); // 修改persistent状态
session.getTransaction().commit();
}
@Test
public void testUpdate5() { // xml配置dynamic-update 用于控制修改指定属性,还是修改所有属性
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s = (Student) session.get(Student.class, 61);
s.setName("zhangsan5");
session.getTransaction().commit();
s.setName("z4");
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.update(s); // session间的save
session2.getTransaction().commit();
}
@Test
public void testUpdate6() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s = (Student) session.get(Student.class, 62);
s.setName("zhangsan6");
session.getTransaction().commit();
s.setName("z4");
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.merge(s);
session2.getTransaction().commit();
}
@Test
public void testUpdate7() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Query q = session
.createQuery("update Student s set s.name='z5' where s.id = 1");
q.executeUpdate(); // HQL
session.getTransaction().commit();
}
@Test
public void testSaveOrUpdate() {
Teacher t = new Teacher();
t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date());
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.saveOrUpdate(t);
session.getTransaction().commit();
t.setName("t2");
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.saveOrUpdate(t);
session2.getTransaction().commit();
}
@Test
public void testClear() { // 实验
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.load(Teacher.class, 62);
System.out.println(t.getName()); // 从缓存中获取ID为62的name
session.clear(); // 清除缓存
Teacher t2 = (Teacher) session.load(Teacher.class, 62);
System.out.println(t2.getName());
session.getTransaction().commit();
}
@Test
public void testFlush() {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.load(Teacher.class, 2);
t.setName("tttt");
session.flush();
/**
*
*/
t.setName("ttttt");
session.getTransaction().commit();
}
public static void main(String[] args) {
beforeClass();
}
}
分享到:
相关推荐
Hibernate 3.3 core API(官方原版),很难弄的哦
hibernate 4.1 core API 英文版
hibernate 3.3 core API chm英文版
hibernate核心包API, 版本:3.3.1, 格式:html。
Hibernate Core 3.3.2.GA API (html官方英文版)
hibernaet 3.3 core API (网页版)英文版
Hibernate Core 3.3.2.GA API.
这是hibernate真正的api,不是hibernate参考书哦!
Hibernate Core 3.3.2.GA API.CHM
Hibernate Core 3.3.2.GA API+Hibernate_3.2.0_符合Java习惯的关系数据库持久化_zh_CN
Hibernate Core 3.3.2.GA-API
这是Hibernate Core 3.3.2.GA的英文原版完整API文档,格式为CHM电子文档,可定位和搜索,使用非常方便。如果在使用中出现问题,可发邮件到xiejinwei1986@126.com,我会尽快回复!Hibernate Core 3.3.2.GA Hibernate ...
Core API org.hibernate This package defines the central Hibernate APIs. org.hibernate.cfg This package defines APIs for configuring Hibernate, and classes for building the Hibernate configuration-...
Aggregated Hibernate Core JavaDocs Hibernate provides both a native API comprised mainly of SessionFactory and Session an implementation of the JSR-317 Java Persistence API (JPA) specification ...
Hibernate3.6.5 jar 包 帮助文档
hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate-search-4.2.0.Final.jar hibernate-search-...
hibernate-api-chm英文最新版
hibernate-core-5.3.0.Final-sources/ hibernate-entitymanager-5.3.0.Final/ hibernate-jpa-2.0-api-1.0.1.Final/ hibernate-release-5.0.7.Final/ jandex-2.0.5.Final/ javassist-3.12.1.GA/ jboss-logging-3.3.2...
模糊查询,排序等6,支持Clustering7,支持直接访问Lucene API8,对Lucene索引,API的高效管理Hibernate Search运行的环境如下:1、JDK或JRE 5.0以上2、Hibernate-Search以及相应的依赖包3、Hibernate Core 3.2.X4、...
列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0.Final.jar hibernate-c3p0-5.1.0.Final.jar hibernate-jpa-2.1-api-...