当前位置:文档之家› hibernate_annotation

hibernate_annotation

Hibernate Annotation 使用hibernate Annotation来映射实体准备工作下载hibernate-distribution-3.3.2.GAhibernate-annotations-3.4.0.GAslf4j导入相关依赖包Hibernate HOME:\hibernate3.jar\lib\bytecode(二进制)\lib\optional(可选的)\lib\required(必须的)导入required下的所有jar包antlr-2.7.6.jarcommons-collections-3.1.jardom4j-1.6.1.jarhibernate3.jarjavassist-3.9.0.GA.jarjta-1.1.jarslf4j-api-1.5.10.jarslf4j-log4j12-1.5.10.jarlog4j-1.2.14.jarmysql.jar---Annotation包ejb3-persistence.jarhibernate-annotations.jarhibernate-commons-annotations.jar简单的例子,通过annotation注解来映射实体PO1、建立(Java Project)项目:hibernate_0100_annotation_HelloWorld_default2、在项目根下建立lib目录a)导入相关依赖jar包antlr-2.7.6.jarcommons-collections-3.1.jardom4j-1.6.1.jarejb3-persistence.jarhibernate-annotations.jarhibernate-commons-annotations.jarhibernate3.jarjavassist-3.9.0.GA.jarjta-1.1.jarlog4j-1.2.14.jarmysql.jarslf4j-api-1.5.10.jarslf4j-log4j12-1.5.10.jar3、建立PO持久化类cn.serup.model.Teacher 内容如下package cn.serup.model;import javax.persistence.Entity;import javax.persistence.Id;//@Entity表示该是实体类@Entitypublic class Teacher {private int id ;private String username ;private String password ;//ID为主键,主键手动分配@Idpublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {ername = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}4、在src目录中建立hibernate.cfg.xml(hibernate配置文件)文件,内容如下<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="ername">root</property><property name="connection.password">saber</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><!-- 独立线程运行,如果通过getCurrentSession()来获得Session,需要设置如下 --><property name="current_session_context_class">thread</property><!-- 映射持久化类 --><mapping class="cn.serup.model.Teacher"/></session-factory></hibernate-configuration>5.、建立新的源码包testa)编写单元测试,结构如下cn.serup.hibernate.test.TeacherTest 内容如下:package cn.serup.hibernate.test;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;import cn.serup.model.Teacher;public class TeacherTest {private static SessionFactory sessionFactory ;@Testpublic void testSaveTeacher() {Teacher t = new Teacher() ;t.setId(2) ;t.setUsername("努尔哈赤") ;t.setPassword("12345") ;Session session = sessionFactory.getCurrentSession() ;session.beginTransaction() ;session.save(t) ;session.beginTransaction().commit() ;}@BeforeClasspublic static void beforeClass() {new SchemaExport(new AnnotationConfiguration().configure()).create(true, true) ;sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory() ;}@AfterClasspublic static void afterClass() {sessionFactory.close() ;}}Annotation表名称与字段映射,长度1、项目:hibernate_0200_annotation_column2、表名称映射,在默认情况下使用@Entity,则使用类名做为表名3、所有的注解写在get()方法上面4、表名映射a)@Table(name="t_teacher")3、字段映射a)默认情况下,则使用属性名称来作为列名(字段名)b)在get()方法上写注解i.@Column(name="t_username")ii.长度iii.@Column(name="t_username",length=35)c)默认情况下会将所有的属性映射到表中,如果不想某个属性映射到表中,如下编写i.@Transient(瞬时、临时(或者透明))Annotation映射主键属性(ID生成策略)1、项目:hibernate_0300_annotation_ID2、使用@Id注解可以将实体bean中的某个属性定义为标识符(identifier),该属性的值可以通过应用自身进行设置(ID手动分配,但不能重复)3、(EJB3规范)也可以通过Hiberante生成(推荐). 使用@GeneratedValue注解可以定义该标识符的生成策略:∙AUTO - 可以是identity column类型,或者sequence类型或者table类型,取决于不同的底层数据库.∙TABLE - 使用表保存id值∙IDENTITY - identity column∙SEQUENCE - sequenceAUTO相当于XML映射文件中的Native4、AUTO ,写上@GeneratedValue则会默认选择AUTO来生成主键,会根据你的方言来选择数据库提供的自增机制,作为主键的生产策略,如果数据库是MySQL,则使用auto_increment@Id@GeneratedValue//或者@GeneratedValue(strategy=GenerationType.AUTO)public int getId() { .... }5、IDENTITY 取得最大的ID值+1,作为主键@Id@GeneratedValue(strategy=GenerationType.IDENTITY)public int getId() { .... }6、TABLE 使用hilo(高低位算法,来产生主键)@Id@GeneratedValue(strategy=GenerationType.TABLE)public int getId() { .... }7、SEQUENCE Oracle的自增机制@Id@GeneratedValue(strategy=GenerationType.SEQUENCE)public int getId() { .... }使用Oracle的SEQUENCE 的话。

相关主题