`
laodaobazi
  • 浏览: 273202 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate 使用 Annotation 2(主键生成方式)

阅读更多

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>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">100</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">create</property>-->
	<mapping class="com.jlee02.createId.Jlee"/>

    </session-factory>

</hibernate-configuration>

 

Jlee.java代码:

 

package com.jlee02.createId;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
@Entity
@Table(name="T_JLEE")
@TableGenerator(
		name = "myKey" ,
		table = "KEY_TABLE" ,
		pkColumnName = "pk_key" ,
		valueColumnName = "pk_value" ,
		pkColumnValue = "T_JLEE" ,
		allocationSize = 1 ,//取完数值后数值步进的数值
		initialValue = 1
		)
//@SequenceGenerator(name="mySeq" , sequenceName="mySeq_DB")适用于Oracle 自定义sequence在数据库中的名字
public class Jlee {

	private long id ;
	private String name ;
	private String phone ;
	private int age ;
	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE , generator="myKey")
//	@GeneratedValue(strategy=GenerationType.SEQUENCE , generator="mySeq")主键生成方式为自定义 Sequence
	public long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	@Column(name="name" , length=32)
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the phone
	 */
	@Column(name="phone" , length=18)
	public String getPhone() {
		return phone;
	}
	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}
	/**
	 * @return the age
	 */
	@Column(name="age")
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	
}

 

JleeTest.java代码:

package com.jlee02.createId;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class JleeTest extends TestCase {

	public void testJlee(){
		SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory() ;
		Session session = sf.getCurrentSession() ;
		session.beginTransaction() ;
		
		Jlee jlee = new Jlee() ;
		jlee.setName("JLee") ;
		jlee.setAge(23) ;
		jlee.setPhone("12123123123") ;
		
		session.save(jlee) ;
		
		session.getTransaction().commit() ;
		
	}
	
	//相当于hbm2ddl 设为 create
	public void testExport(){
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true) ;
	}
	
}

 

分享到:
评论

相关推荐

    hibernate-主键生成方式[自动、手动]-annotation

    本文是讲解的是hibernate3.2的主键生成方式,通过annotation来实现,详细的分析了主键在hibernate的生成以及在真实项目的运用。。。。

    hibernate annotation 中文文档

    生成的属性 2.4.4. 继承 2.4.5. 关于单个关联关系的注解 2.4.5.1. 延迟选项和获取模式 2.4.6. 关于集合类型的注解 2.4.6.1. 参数注解 2.4.6.2. 更多的集合类型 2.4.7. 缓存 2.4.8. 过滤器 2.4.9. 查询 3. 通过XML...

    Hibernate_Annotation关联映射

    在多对多关联中很多值是自动生成,党双向多对多关联中没有定义任何物理映射时,Hibernate根据以下规则生成相应的值,关联表名:主表表名+下划线+从表表名,关联到主表的外键名:主表名+下划线+主表中的主键列名,...

    Hibernate笔记 马士兵

    第1课 课程内容 6 第2课 Hibernate UML图 6 第3课 风格 7 第4课 资源 7 第5课 环境准备 7 第6课 第一个示例Hibernate HelloWorld 7 ...2、annotation方式 27 第14课 Hibernate核心开发接口(重点) 29 ........

    hibernate annotation帮助文档

    2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4.3.1. 访问类型 2.4.3.2. 公式 2.4.3.3. 类型 2.4.3.4. 索引 2.4.3.5. @Parent 2.4.3.6. 生成的属性 2.4.4. 继承 2.4.5. 关于...

    JAVA 的ID生成策略

    主要描述hibernate在Annotation情况下的主键生成策略

    最全Hibernate 参考文档

    触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (optional) 5.1.9. property 5.1.10. 多对一...

    Hibernate注释大全收藏

    @Id 注解可将实体Bean中某个属性定义为主键,使用@GenerateValue注解可以定义该标识符的生成策略。 • AUTO - 可以是 identity column, sequence 或者 table 类型,取决于不同底层的数据库 • TABLE - 使用table...

    AHibernate1.1

    //使用insert(entity,false)这样方式可以插入有固定Id的数据 5.支持代码混淆处理,当设置了proguard.config=proguard.cfg后发布程序时,程序会自动混淆处理.使用老接口:public BaseDaoImpl(SQLiteOpenHelper dbHelper)...

    Hibernate 中文 html 帮助文档

    触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一...

    hibernate 体系结构与配置 参考文档(html)

    触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对...

    Hibernate教程

    触发器实现的主键生成器(Primary keys assigned by triggers) 6.1.5. composite-id 6.1.6. 鉴别器(discriminator) 6.1.7. 版本(version)(可选) 6.1.8. timestamp (optional) 6.1.9. property 6.1.10. 多...

    hibernate3.04中文文档.chm

    触发器实现的主键生成器(Primary keys assigned by triggers) 6.1.5. composite-id 6.1.6. 鉴别器(discriminator) 6.1.7. 版本(version)(可选) 6.1.8. timestamp (optional) 6.1.9. property 6.1.10. ...

    Hibernate3的帮助文档

    触发器实现的主键生成器(Primary keys assigned by triggers) 6.1.5. composite-id 6.1.6. 鉴别器(discriminator) 6.1.7. 版本(version)(可选) 6.1.8. timestamp (optional) 6.1.9. property 6.1.10. 多...

    Hibernate3+中文参考文档

    触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (optional) 5.1.9. property 5.1.10. 多对一...

    hibernate 框架详解

    触发器实现的主键生成器(Primary keys assigned by triggers) 6.1.5. composite-id 6.1.6. 鉴别器(discriminator) 6.1.7. 版本(version)(可选) 6.1.8. timestamp (optional) 6.1.9. property 6.1.10. ...

    Hibernate参考文档

    触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一...

    Spring API

    11.5.2. 使用SimpleJdbcInsert来获取自动生成的主键 11.5.3. 指定SimpleJdbcInsert所使用的字段 11.5.4. 使用SqlParameterSource提供参数值 11.5.5. 使用SimpleJdbcCall调用存储过程 11.5.6. 声明SimpleJdbcCall...

    Spring中文帮助文档

    11.5.2. 使用SimpleJdbcInsert来获取自动生成的主键 11.5.3. 指定SimpleJdbcInsert所使用的字段 11.5.4. 使用SqlParameterSource提供参数值 11.5.5. 使用SimpleJdbcCall调用存储过程 11.5.6. 声明SimpleJdbcCall...

Global site tag (gtag.js) - Google Analytics