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) ;
}
}