星期日, 一月 03, 2016 17:20:10
文档
\zhuhw\hibernate\jar\hibernate-annotations-3.4.0.GA\doc\reference\zh_cn\html_single
2.2.. 映射主键属性
下面是定义组合主键的几种语法:
1.将组件类注解为@Embeddable,并将组件的属性注解为@Id ok
2.将组件的属性注解为@EmbeddedId ok
3.将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
对于Teacher这个类进行的联合组件方法:
1.生成一个TeacherPK;
在这个类上面@Embeddable
2.在Teacher进行的@Id
3.再写HibernateIDTest.java
3.将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
将TeacherPK不用了,直接在Teacher中进行setID getID...
加注解@IdClass指明就可以,更符合编程的习惯。 (两个联合起来正好是一个TeacherPK.class)
代码案例:
/hibernate_0400_ID/src/com/zhuhw/hibernate/model/TeacherPK.java
package com.zhuhw.hibernate.model; import javax.persistence.Embeddable; /*@Embeddable// 1.将组件类注解为@Embeddable,并将组件的属性注解为@Id */public class TeacherPK implements java.io.Serializable{ @Override public boolean equals(Object o) { if(o instanceof TeacherPK) { TeacherPK spk = (TeacherPK)o; if(this.id == spk.getId()&& this.name == spk.getName()){ return true; } }return false; } @Override public int hashCode() { // TODO Auto-generated method stub return this.name.hashCode(); } private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; }
/hibernate_0400_ID/src/com/zhuhw/hibernate/model/Teacher.java
package com.zhuhw.hibernate.model; import java.util.Date; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; /*1.实体类(使用annotation进行实现,告诉hibernate是实体类, * 不需要再建立Teacher.hbm.xml文件进行映射) * 2.hibernate.cfg.xml进行配置即可 * 3.建立一个TeacherTest */ @Entity //表名和类名不同,对表名进行配置 //@Table(name = "_teacher") @IdClass(value=TeacherPK.class)//两个联合起来正好是一个TeacherPK.class public class Teacher { private int id; private String name; /* 3.将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id 将TeacherPK不用了,直接在Teacher中进行setID getID... 加注解@IdClass指明就可以,更符合编程的习惯。*/ /*private TeacherPK tpk;*/ /*@Id// 1.将组件类注解为@Embeddable,并将组件的属性注解为@Id */ @EmbeddedId//2.将组件的属性注解为@EmbeddedId /*public TeacherPK getTpk() { return tpk; } public void setTpk(TeacherPK tpk) { this.tpk = tpk; }*/ private String title; private Date birthdate; //ZhiCheng enum private ZhiCheng zhicheng; /*@Enumerated(EnumType.STRING) * 数据库存储String `zhicheng` varchar(255) DEFAULT NULL, */ /*@Enumerated(EnumType.ORDINAL) * 数据库存储下标值 `zhicheng` varchar(255) DEFAULT NULL,*/ @Enumerated(EnumType.ORDINAL) public ZhiCheng getZhicheng() { return zhicheng; } public void setZhicheng(ZhiCheng zhicheng) { this.zhicheng = zhicheng; } //映射日期与时间类型,指定时间精度 //通过@Temporal可以指定时间的精度 @Temporal(value=TemporalType.DATE) public Date getBirthdate() { return birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } private String yourWifeName; //不要存到数据库中 /*@Transient*/ public String getYourWifeName() { return yourWifeName; } public void setYourWifeName(String yourWifeName) { this.yourWifeName = yourWifeName; } //主键 @Id @GeneratedValue public int getId() { return id; } @Basic//对数据库中,字段名和属性相同 public void setId(int id) { this.id = id; } //字段名属性不同a)Annotation:@Column @Column(name = "_name") @Id public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
/hibernate_0400_ID/test/com/zhuhw/hibernate/model/HibernateIDTest.java
package com.zhuhw.hibernate.model; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class HibernateIDTest { public static SessionFactory sf = null; @BeforeClass public static void beforeClass(){ sf = new AnnotationConfiguration().configure().buildSessionFactory(); } @Test public void TestStudentID(){ Student s = new Student(); /*配置文件中使用generator * s.setId(9); * */ StudentPK spk = new StudentPK(); spk.setId(11); spk.setName("waxun"); s.setAge(8); s.setSpk(spk); Session session = sf.openSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); } @Test public void TestTeacherID(){ Teacher t = new Teacher(); /* TeacherPK tpk = new TeacherPK(); tpk.setId(1); tpk.setName("waxun");*/ /*t.setName("yuzhou1");*/ /*t.setTpk(tpk);*/ t.setId(1); t.setName("waxun"); t.setTitle("ccc"); t.setBirthdate(new Date()); t.setYourWifeName("yourWifeName1"); t.setZhicheng(ZhiCheng.A); //因为使用的annotation,所以Configuration要使用AnnotationConfiguration /*Configuration cf = new AnnotationConfiguration(); SessionFactory sf = cf.configure().buildSessionFactory();*/ Session session = sf.openSession(); //在hibernate中执行操作要在一个事务里面 session.beginTransaction(); session.save(t); session.getTransaction().commit(); session.close(); } @AfterClass public static void afterClass(){ sf.close(); } }
/hibernate_0400_ID/src/hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">scoot</property> <property name="connection.password">tiger</property>--> <!-- JDBC connection pool (use the built-in) --> <!--<property name="connection.pool_size">1</property>--> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!--<property name="current_session_context_class">thread</property>--> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/zhuhw/hibernate/model/Student.hbm.xml"/> <mapping class="com.zhuhw.hibernate.model.Teacher"/> </session-factory> </hibernate-configuration>
运行结果:
com.zhuhw.hibernate.model.Teacher{zhicheng=0, title=ccc, tpk=component[id,name]{id=1, name=waxun}