演示场景
演示场景
类图 > Gender female +course Course Teacher +name String +totalStudent s > Address +addrInfo NormalCourse OnlineCourse +classroomName +URL
类图
配置 HibernateApplication Context xml hibernate. cfg. xml 创建数据库 ooad/ooad 关于 Library/ Project模板
配置 • HibernateApplicationContext.xml • hibernate.cfg.xml • 创建数据库 – ooad/ooad • 关于Library/Project模板
Class mapping 创建类 Teacher及属性name/ birthday @Test public void create Teacher Test(t 基类的用途 Teacher teacher= new Teacher o; teacher setName student1") 修改 Hibernate.cg, xml teacher. setBirthday(new Date) getPersistence Manager().save(teacher); 第一次运行: 看 DB Schema assertObjectPersisted(teacher) 修改 Rollback属性,再次运行 看DB数据 SQL输出日志说明
Class mapping • 创建类Teacher及属性name/birthday • 基类的用途 • 修改Hibernate.cfg.xml • 第一次运行: – 看DB Schema • 修改Rollback属性,再次运行 – 看DB数据 • SQL输出日志说明 @Test public void createTeacherTest() { Teacher teacher = new Teacher(); teacher.setName("student1"); teacher.setBirthday(new Date()); getPersistenceManager().save(teacher); assertObjectPersisted(teacher); }
Datatype/enum Mapping 自定义属性的映射: Addresspostcode, addrinfo},观察 Schema自 动更新 public void create Teacher WithAttribute ( t Teacher teacher new Teacher (; teacher setName teacher1") teacher. setBirthday(new Date O) teacher. getAddress(). setPostCode( 200433") teacher. setGender gender female) getPersistence Manager(). save(teacher); assertObjectPersisted(teacher) Teacher savedTeacher= getPersistence Manager( get(Teacher class, teacher.getlo) assertEquals(teacher, savedTeacher) assertEquals(teacher getGender(, saved Teacher get Gender(;
Datatype/enum Mapping • 自定义属性的映射: Address{postcode,addrinfo},观察Schema自 动更新 @Test public void createTeacherWithAttribute() { Teacher teacher = new Teacher(); teacher.setName("teacher1"); teacher.setBirthday(new Date()); teacher.getAddress().setPostCode("200433"); teacher.setGender(Gender.female); getPersistenceManager().save(teacher); assertObjectPersisted(teacher); Teacher savedTeacher= getPersistenceManager().get(Teacher.class, teacher.getId()); assertEquals(teacher, savedTeacher); assertEquals(teacher.getGender(), savedTeacher.getGender()); }
@Enumerated @Enumerated (Enum Type STRING Delete all tables:因为 Schema有时无法自动更 新 Check data base schema
• @Enumerated • @Enumerated(EnumType.STRING) – Delete all tables:因为Schema有时无法自动更 新 • Check database schema
Simple HQl public void find Teacher ByHQLO t Teacher teacher= new Teacher teachersetName teacher1") teacher. set Birthday new Date O); teacher.getAddress( setPostCode 200433 ) teacher set Gender( gender female) getPersistence Manager().save(teacher) String findTeacherHQL ="from Teacher t where t name like t% +and t address. postcode= 200433 Query hql getPersistence Manager(). createQuery(find TeacherHQL) assertEquals(1, hql. list().size(;
Simple HQL @Test public void findTeacherByHQL() { Teacher teacher = new Teacher(); teacher.setName("teacher1"); teacher.setBirthday(new Date()); teacher.getAddress().setPostCode("200433"); teacher.setGender(Gender.female); getPersistenceManager().save(teacher); String findTeacherHQL = "from Teacher t where t.name like 't%'" + " and t.address.postCode = '200433'"; Query hql = getPersistenceManager().createQuery(findTeacherHQL); assertEquals(1, hql.list().size()); }
Inheritance Mapping 继承的映射方式:单表映射 Create abstract course class and it's subclasses @Entity @Inheritance strategy= Inheritance Type SINGLE TABLE @DiscriminatorColumn(name="COURSETYPE") public abstract class Course extends Base Modelobject t @Basic String name; aBasic Integer totalStudents @Entity @DiscriminatorValue( NORMAL") public class NormalCourse extends Course i
Inheritance Mapping • 继承的映射方式:单表映射 • Create abstract course class, and it’s subclasses; @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "COURSETYPE") public abstract class Course extends BaseModelObject { @Basic String name; @Basic Integer totalStudents; @Entity @DiscriminatorValue("NORMAL") public class NormalCourseextends Course {
Inheritance Mapping 测试代码:创建两个不同类型的 Course,查看DB Schema, Rollback(false),运行后查看数据 Normal Course normal Course= new Normalcourse normalCourse setName "normal course") normal Course. setTotalStudents(10) normal Course. setclassroomName(S209") getPersistence Manager().save(normalCourse); assertObjectPersisted(normal Course) Online course online course new Online Course online Course. setName online course online Course. setTotalStudents (200 onlineCourseseturlhtTp://www.course.com/test") getPersistence Manager( save online Course ); assertobjectPersisted(online Course)
Inheritance Mapping • 测试代码:创建两个不同类型的Course,查看DB Schema,Rollback(false),运行后查看数据 NormalCourse normalCourse = new NormalCourse(); normalCourse.setName("normal course"); normalCourse.setTotalStudents(10); normalCourse.setClassroomName("S209"); getPersistenceManager().save(normalCourse); assertObjectPersisted(normalCourse); OnlineCourseonlineCourse = new OnlineCourse(); onlineCourse.setName("online course"); onlineCourse.setTotalStudents(200); onlineCourse.setURL("http://www.course.com/test"); getPersistenceManager().save(onlineCourse); assertObjectPersisted(onlineCourse);
Inheritance Mapping 测试代码如下,在运行前分析是否能通过: 测试代码的副作用的危害 String hsql1=from Course List hsql1Result= getPersistence Manager(). createQuery(hsql1) list( assertEquals(2, hsg/rEsult size ll String hsql2="from NormalCourse hsql1Result= get PersistenceManager( createQuery(hsql2) listo assertEquals(1, hsq/lResult size(l
Inheritance Mapping • 测试代码如下,在运行前分析是否能通过: 测试代码的副作用的危害。 String hsql1 = "from Course"; List hsql1Result = getPersistenceManager().createQuery(hsql1).list(); assertEquals(2, hsql1Result.size()); String hsql2 = "from NormalCourse"; hsql1Result = getPersistenceManager().createQuery(hsql2).list(); assertEquals(1, hsql1Result.size());