Hibernate的@GeneratedValue注解

1.新建一个java project项目,里面新建一个lib文件夹,lib文件夹里面放置要用的一些jar文件,然后全部选中导入到项目中去。整体的框架如下图所示:

Hibernate的@GeneratedValue注解_第1张图片


2.Students.java里面的代码如下图所示:

package entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "t_students", schema = "hibernate")
public class Students implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int sid;
	private String sname;
	private String gender;
	private Date birthday;
	private String major;

	private Address add;

	public Students() {

	}

	public Students(int sid, String sname, String gender, Date birthday,
			String major, Address add) {
		this.sid = sid;
		this.sname = sname;
		this.gender = gender;
		this.birthday = birthday;
		this.major = major;
		this.add = add;
	}

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public Address getAdd() {
		return add;
	}

	public void setAdd(Address add) {
		this.add = add;
	}

}


3.Address.java里面的代码:

[java]  view plain  copy
 
  print ?
  1. package entity;  
  2.   
  3. import javax.persistence.Embeddable;  
  4.   
  5. @Embeddable  
  6. public class Address {  
  7.   
  8.     private String postCode;  
  9.     private String address;  
  10.     private String phone;  
  11.   
  12.     public Address() {  
  13.   
  14.     }  
  15.   
  16.     public String getPostCode() {  
  17.         return postCode;  
  18.     }  
  19.   
  20.     public void setPostCode(String postCode) {  
  21.         this.postCode = postCode;  
  22.     }  
  23.   
  24.     public String getAddress() {  
  25.         return address;  
  26.     }  
  27.   
  28.     public void setAddress(String address) {  
  29.         this.address = address;  
  30.     }  
  31.   
  32.     public String getPhone() {  
  33.         return phone;  
  34.     }  
  35.   
  36.     public void setPhone(String phone) {  
  37.         this.phone = phone;  
  38.     }  
  39.   
  40. }  

4.hibernate.cfg.xml里面的代码如下图所示:

[html]  view plain  copy  
   
  print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4. <hibernate-configuration>  
  5.     <session-factory>  
  6.         <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>  
  7.         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8property>  
  8.         <property name="dialect">org.hibernate.dialect.MySQLDialectproperty>  
  9.         <property name="connection.username">rootproperty>  
  10.         <property name="connection.password">rootproperty>  
  11.         <property name="show_sql">trueproperty>  
  12.         <property name="format_sql">trueproperty>  
  13.         <property name="hbm2ddl.auto">createproperty>  
  14.         <property name="hibernate.current_session_context_clss">threadproperty>  
  15.   
  16.         <mapping class="entity.Students" />  
  17.     session-factory>  
  18. hibernate-configuration>  

5.TestStudents.java里面的代码如下图所示:

[java]  view plain  copy  
   
  print ?
  1. package entity;  
  2.   
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.cfg.Configuration;  
  5. import org.hibernate.service.ServiceRegistry;  
  6. import org.hibernate.service.ServiceRegistryBuilder;  
  7. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  8. import org.junit.Test;  
  9.   
  10. public class TestStudents {  
  11.   
  12.     @Test  
  13.     public void testShemaExport() {  
  14.         Configuration config = new Configuration().configure();  
  15.         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()  
  16.                 .applySettings(config.getProperties()).buildServiceRegistry();  
  17.         SessionFactory sessionFactory = config  
  18.                 .buildSessionFactory(serviceRegistry);  
  19.         SchemaExport export = new SchemaExport(config);  
  20.         export.create(truetrue);  
  21.     }  
  22.   
  23. }  

6.在Navicat数据库里面新建一个数据库,数据库的名称要与上面 的数据库的名称相同。


7.运行testShemaExport类,数据库里面会自动创建一张表。
Hibernate的@GeneratedValue注解_第2张图片

你可能感兴趣的:(Hibernate的@GeneratedValue注解)