eclips怎么导入项目到入hibarnate

hibarnate中ID主键生成方式 -
- ITeye博客
博客分类:
被映射的实体类必须声明一个对应数据库表主键列的属性。大多数类有一个JavaBeans风格的代表此实例唯一标识的属性。&id& 元素定义了该属性到数据库表主键字段的映射。
&id& name="propertyName"&
& (1)& type="typename"&
& (2)& column="column_name"&
& (3)& unsaved-value="null|any|none|undefined|id_value"&
& (4)& access="field|property|ClassName"&&&
& (5)& node="element-name|@attribute-name|element/@attribute|."&
&&&&& &generator class="generatorClass"/&&&
&id& name="propertyName"
& (1)& type="typename"
& (2)& column="column_name"
& (3)& unsaved-value="null|any|none|undefined|id_value"
& (4)& access="field|property|ClassName"&
& (5)& node="element-name|@attribute-name|element/@attribute|."
&&&&& &generator class="generatorClass"/&
&/id&
(1) name (可选): 标识属性的名字。
(2) type (可选): 标识Hibernate类型的名字。
(3) column (可选 - 默认为实体类的属性名): 数据库表中主键字段的名字。
(4) unsaved-value (可选 - 默认为一个合情理的值): 一个特定的标识属性值,用来标志该实例是刚刚创建的,尚未保存(unsaved),其实就是一个瞬时对象。根据这个值,可以把这种瞬时对象和那种已调用session的save方法保存过以及装载过(loaded)的脱管(detached)对象明确区分开来。
(5) access (可选 - 默认为property): Hibernate用来访问属性值的策略。
如果 name属性缺失的话,就可以假定这个类是没有标识属性的。
unsaved-value属性对Hibernate3来说基本上是不必要的。
还有一个供选择的&composite-id&声明可以访问旧式的多主键数据。我们强烈不鼓励使用这种方式。
二、Generator
可选的&generator&子元素是 一个Java类的名称,用来生成该持久化类实例的唯一标识符。如果这个生成器实例需要某些配置值或者初始化参数,可以使用&param&元素来传递这些参数。
&id name="id" type="long" column="cat_id"&&&
&generator class="org.hibernate.id.TableHiLoGenerator"&&&
&param name="table"&uid_table&/param&&&
&param name="column"&next_hi_value_column&/param&&&
&/generator&&&
&id name="id" type="long" column="cat_id"&
&generator class="org.hibernate.id.TableHiLoGenerator"&
&param name="table"&uid_table&/param&
&param name="column"&next_hi_value_column&/param&
&/generator&
&/id&
所有的生成器都实现了org.hibernate.id.IdentifierGenerator接口。这是一个非常简单的接口;某些应用程序可以选择提供他们自己特定的实现。当然,Hibernate提供了很多内置的实现。下面是一些内置生成器的快捷名字:
increment(递增)
用于为long, short或者int类型生成唯一标识。只有在没有其他进程往同一张表中插入数据时才能使用。 在集群下不要使用。
identity (标识)
对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。返回的标识符是long, short 或者int类型的。
sequence (序列)
在DB2,PostgreSQL, Oracle, SAP DB, McKoi中使用序列(sequence),而在Interbase中使用生成器(generator)。返回的标识符是long, short或者 int类型的。
hilo (高低位)
使用一个高/低位算法来高效的生成long, short或者 int类型的标识符。给定一个表和字段(默认分别是hibernate_unique_key 和next_hi)作为高位值的来源。高/低位算法生成的标识符只在一个特定的数据库中是唯一的。
seqhilo(使用序列的高低位)
使用一个高/低位算法来高效的生成long, short或者 int类型的标识符,给定一个数据库序列(sequence)的名字。
uuid
用一个128位的UUID算法生成字符串类型的标识符。在一个网络中唯一(使用了IP地址)。UUID被编码为一个32位16进制数字的字符串。
在MS SQL Server 和 MySQL 中使用数据库生成的GUID字符串。
native(本地)
根据底层数据库的能力选择identity, sequence 或者hilo中的一个。
assigned (自行分配)
主键由外部程序负责生成,无需Hibernate参与 让应用程序在save()方法调用之前为对象分配一个标示符。这是 &generator&元素没有指定时的默认生成策略。
通过数据库触发器选择一些唯一主键的行并返回主键值来分配一个主键。
foreign(外部引用)
使用另外一个相关联的对象的标识符。常常和&one-to-one&联合一起使用。
以下内容摘自:/blog/511019
一:主键生成策略大体分类:
1:hibernate 负责对主键ID赋值
2:应用程序自己为主键ID赋值(不推荐使用)
3:底层数据库为主键ID赋值
二:具体用法
1:increment:(跨平台的)
以递增的方式为代理主键赋值,每次维护的当前实例中取出一个最大值,在最大值的基础之上再进行加法,适合只有单个应用进程访问数据的情况,否则会产生并发冲突问题(主键重复)。并且oid 的类型必须是long ,int ,short类型的。
2:assigned :由程序员给赋值,(不建议使用)
3:identity:是由底层的数据库来生成主键,他要求底层数据库支持自动增长,并且把主键定义成自动增长类型的 ,oid 必须是long ,int ,short 类型的。
Oracle 不支持。Mysql 支持
4:sequence:由底层的数据根据序列来生成主键。oracle ,db2数据库支持,mysql不支持
5:native:(跨平台的)
由hibernate 自己跟据不同的数据库的主键生成方式,自己选择生成方式(可以跨平台)
6:hilo :(跨平台的),通过hi/lo算法实现主键的生成机制,但是需要额外的表来保存主键的生成历史,使用时要先插入一个值。
使用规则: 通用性和安全性:
7:seqhilo:取得sequence的值,来生成高位,产生id。
但是只能使用于有sequence的数据库(oracle , db2);
8:uuid.hex:
用系统时间和ip地址等具有天然唯一性的资源进行计算,生成id,
全球唯一。生成的是一个128位的二进制数。然后把这个二进制数,转化成一个16进制的数(32位),再转化为字符串付给我们的id。那么我们的实体类的oid以及数据库的主键都需要随之改为字符串类型的。
9:foreign
用于共享主键的时候。(一对一主键关联);
jiaguwen123
浏览: 237930 次
来自: 深圳
关于这个心跳的发送和接收 晚上全是the fuccking s ...
你的想法在大的物流公司已经实现了,只不过他们使用是GPS定位
2,AuthenticationHandler类的写法
pac ...
总结的不错哟!SpringMVC Hibernate CRUD Tutorial using Eclipse - TechZoo - Technology BlogThis Hibernate tutorial demonstrates how to use JPA annotations in order to implement a unidirectional one-to-one association on a foreign key. This is similar to the tutorial
but using annotations instead of XML descriptor. The following diagram recalls the one-to-one entity relationship:
A book belongs to only one author, and we can only know the author from the book, not vice-versa. Thus the association is called unidirectional (one-way). We are going to develop a sample Hibernate application using the following technologies/software programs: Note that we use the latest stuffs to date, you can use little bit lower or higher versions. Now, let’s following the steps below: Table of content: & Execute the following MySQL script to create the database booksdb with two tables author and book:cre
CREATE TABLE `author` (
`author_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`author_id`)
CREATE TABLE `book` (
`book_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL,
`description` varchar(512) NOT NULL,
`published` date NOT NULL,
`author_id` int(11) NOT NULL,
PRIMARY KEY (`book_id`),
KEY `author_fk` (`author_id`),
CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `author` (`author_id`)
);&The database structure looks like this:
& If you want to learn more about Hibernate, find and read this book:
& & Create a Maven project in Eclipse with the following structure:
The project consists of the following files:Model classes: Author.java and Book.javaHibernate XML configuration file: hibernate.cfg.xmlTest program: BooksManager.javaMaven project: pom.xml Here’s content of the pom.xml file:&project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"&
&modelVersion&4.0.0&/modelVersion&
&groupId&HibernateOne2OneAnnnotationsExample&/groupId&
&artifactId&HibernateOne2OneAnnnotationsExample&/artifactId&
&version&0.0.1-SNAPSHOT&/version&
&description&Example of a Hibernate one-to-one mapping with
foreign key using annotations&/description&
&dependencies&
&dependency&
&groupId&org.hibernate&/groupId&
&artifactId&hibernate-core&/artifactId&
&version&4.2.7.SP1&/version&
&/dependency&
&dependency&
&groupId&mysql&/groupId&
&artifactId&mysql-connector-java&/artifactId&
&version&5.1.26&/version&
&/dependency&
&/dependencies&
&/project&Here, we specify two main dependencies: hibernate-core and mysql-connector-java. Maven will resolve other related dependencies automatically. & The followings are code of the two model classes: Author.java and Book.java. File net\codejava\hibernate\Author.java:package net.codejava.
import javax.persistence.C
import javax.persistence.E
import javax.persistence.GeneratedV
import javax.persistence.Id;
import javax.persistence.T
@Table(name = "AUTHOR")
public class Author {
public Author() {
public Author(String name, String email) {
this.name =
this.email =
@Column(name = "AUTHOR_ID")
@GeneratedValue
public long getId() {
public void setId(long id) {
public String getName() {
public void setName(String name) {
this.name =
public String getEmail() {
public void setEmail(String email) {
this.email =
& File net\codejava\hibernate\Book.java:package net.codejava.
import java.util.D
import javax.persistence.CascadeT
import javax.persistence.C
import javax.persistence.E
import javax.persistence.GeneratedV
import javax.persistence.Id;
import javax.persistence.JoinC
import javax.persistence.OneToO
import javax.persistence.T
import javax.persistence.T
import javax.persistence.TemporalT
@Table(name = "BOOK")
public class Book {
private Date publishedD
public Book() {
@Column(name = "BOOK_ID")
@GeneratedValue
public long getId() {
public void setId(long id) {
public String getTitle() {
public void setTitle(String title) {
this.title =
public String getDescription() {
public void setDescription(String description) {
this.description =
@Temporal(TemporalType.DATE)
@Column(name = "PUBLISHED")
public Date getPublishedDate() {
return publishedD
public void setPublishedDate(Date publishedDate) {
this.publishedDate = publishedD
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "AUTHOR_ID")
public Author getAuthor() {
public void setAuthor(Author author) {
this.author =
}&As you notice, we use the following JPA annotations:@Entity: is required for every model class.@Table: maps the class with the corresponding database table. If omitted, Hibernate will use the class name.@Column: maps the field with the corresponding table column. If omitted, Hibernate will infer the column name and type based on signatures of the getter/setter.@Id and @GeneratedValue: are used in conjunction for a field that maps to the primary key. The values for this field are auto generated.@Temporal: must be used with a java.util.Date field to specify the actual SQL type of the column.@OneToOne and @JoinColumn: are used together to specify a one-to-one association and the join column. Using annotations is usually preferred over XML descriptor because it’s simple and straightforward in the code. & Write XML configuration for database settings and mapping classes in the hibernate.cfg.xml file as follows:&?xml version='1.0' encoding='utf-8'?&
&!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/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:3306/booksdb&/property&
&property name="connection.username"&root&/property&
&property name="connection.password"&P@ssw0rd&/property&
&property name="dialect"&org.hibernate.dialect.MySQLDialect&/property&
&property name="show_sql"&true&/property&
&mapping class="net.codejava.hibernate.Book"/&
&mapping class="net.codejava.hibernate.Author"/&
&/session-factory&
&/hibernate-configuration&& Write code for the test program (BooksManager.java) as follows:package net.codejava.
import java.util.D
import org.hibernate.S
import org.hibernate.SessionF
import org.hibernate.cfg.C
import org.hibernate.service.ServiceR
import org.hibernate.service.ServiceRegistryB
* This program demonstrates using Hibernate framework to manage
* a one-to-one mapping with foreign key using annotations.
* @author www.codejava.net
public class BooksManager {
public static void main(String[] args) {
// loads configuration and mappings
Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
// builds a session factory from the service registry
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// obtains the session
Session session = sessionFactory.openSession();
session.beginTransaction();
// creates a Book entity
Book newBook = new Book();
newBook.setTitle("Hibernate Made Easy");
newBook.setDescription("Simplified Data Persistence with Hibernate and JPA");
newBook.setPublishedDate(new Date());
newBook.setAuthor(new Author("Cameron Wallace McKenzie", ""));
// persists the book entity
Long bookId = (Long) session.save(newBook);
// gets the book entity back
Book book = (Book) session.get(Book.class, bookId);
System.out.println("Book's Title: " + book.getTitle());
System.out.println("Book's Description: " + book.getTitle());
Author author = book.getAuthor();
System.out.println("Author's Name: " + author.getName());
System.out.println("Author's Email: " + author.getEmail());
session.getTransaction().commit();
session.close();
}Output of the program:Hibernate: insert into AUTHOR (email, name) values (?, ?)
Hibernate: insert into BOOK (AUTHOR_ID, description, PUBLISHED, title) values (?, ?, ?, ?)
Book's Title: Hibernate Made Easy
Book's Description: Hibernate Made Easy
Author's Name: Cameron Wallace McKenzieResult in the Book table:
Result in the Author table:
& You may be also interested in:Share this article:
Attachments:[Eclipse-Maven project]15 kB
Add comment
JavaScript is currently disabled.Please enable it for a better experience of .一、id被映射的实体类必须声明一个对应数据库表主键列的属性。大多数类有一个JavaBeans风格的代表此实例唯一标识的属性。&id& 元素定义了该属性到数据库表主键字段的映射。Java代码 &id
name="propertyName"
type="typename"
column="column_name"
unsaved-value="null|any|none|undefined|id_value"
access="field|property|ClassName"&
node="element-name|@attribute-name|element/@attribute|."
&generator class="generatorClass"/&
name="propertyName"(1)
type="typename"(2)
column="column_name"(3)
unsaved-value="null|any|none|undefined|id_value"(4)
access="field|property|ClassName"&(5)
node="element-name|@attribute-name|element/@attribute|."&generator class="generatorClass"/&&/id&(1) name (可选): 标识属性的名字。 (2) type (可选): 标识Hibernate类型的名字。 (3) column (可选 - 默认为实体类的属性名): 数据库表中主键字段的名字。 (4) unsaved-value (可选 - 默认为一个合情理的值): 一个特定的标识属性值,用来标志该实例是刚刚创建的,尚未保存(unsaved),其实就是一个瞬时对象。根据这个值,可以把这种瞬时对象和那种已调用session的save方法保存过以及装载过(loaded)的脱管(detached)对象明确区分开来。(5) access (可选 - 默认为property): Hibernate用来访问属性值的策略。 如果 name属性缺失的话,就可以假定这个类是没有标识属性的。unsaved-value属性对Hibernate3来说基本上是不必要的。还有一个供选择的&composite-id&声明可以访问旧式的多主键数据。我们强烈不鼓励使用这种方式。二、Generator可选的&generator&子元素是 一个Java类的名称,用来生成该持久化类实例的唯一标识符。如果这个生成器实例需要某些配置值或者初始化参数,可以使用&param&元素来传递这些参数。Java代码 &id name="id" type="long" column="cat_id"&
&generator class="org.hibernate.id.TableHiLoGenerator"&
&param name="table"&uid_table&/param&
&param name="column"&next_hi_value_column&/param&
&/generator&
&id name="id" type="long" column="cat_id"&&generator class="org.hibernate.id.TableHiLoGenerator"&&param name="table"&uid_table&/param&&param name="column"&next_hi_value_column&/param&&/generator&&/id& 所有的生成器都实现了org.hibernate.id.IdentifierGenerator接口。这是一个非常简单的接口;某些应用程序可以选择提供他们自己特定的实现。当然,Hibernate提供了很多内置的实现。下面是一些内置生成器的快捷名字:increment(递增) 用于为long, short或者int类型生成唯一标识。只有在没有其他进程往同一张表中插入数据时才能使用。 在集群下不要使用。identity (标识)对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。返回的标识符是long, short 或者int类型的。sequence (序列) 在DB2,PostgreSQL, Oracle, SAP DB, McKoi中使用序列(sequence),而在Interbase中使用生成器(generator)。返回的标识符是long, short或者 int类型的。hilo (高低位) 使用一个高/低位算法来高效的生成long, short或者 int类型的标识符。给定一个表和字段(默认分别是hibernate_unique_key 和next_hi)作为高位值的来源。高/低位算法生成的标识符只在一个特定的数据库中是唯一的。seqhilo(使用序列的高低位) 使用一个高/低位算法来高效的生成long, short或者 int类型的标识符,给定一个数据库序列(sequence)的名字。uuid用一个128位的UUID算法生成字符串类型的标识符。在一个网络中唯一(使用了IP地址)。UUID被编码为一个32位16进制数字的字符串。guid 在MS SQL Server 和 MySQL 中使用数据库生成的GUID字符串。native(本地) 根据底层数据库的能力选择identity, sequence 或者hilo中的一个。assigned (自行分配)主键由外部程序负责生成,无需Hibernate参与 让应用程序在save()方法调用之前为对象分配一个标示符。这是 &generator&元素没有指定时的默认生成策略。select 通过数据库触发器选择一些唯一主键的行并返回主键值来分配一个主键。foreign(外部引用) 使用另外一个相关联的对象的标识符。常常和&one-to-one&联合一起使用。以下内容摘自:/blog/511019一:主键生成策略大体分类: 1:hibernate 负责对主键ID赋值 2:应用程序自己为主键ID赋值(不推荐使用) 3:底层数据库为主键ID赋值 二:具体用法 1:increment:(跨平台的) 以递增的方式为代理主键赋值,每次维护的当前实例中取出一个最大值,在最大值的基础之上再进行加法,适合只有单个应用进程访问数据的情况,否则会产生并发冲突问题(主键重复)。并且oid 的类型必须是long ,int ,short类型的。 2:assigned :由程序员给赋值,(不建议使用) 3:identity:是由底层的数据库来生成主键,他要求底层数据库支持自动增长,并且把主键定义成自动增长类型的 ,oid 必须是long ,int ,short 类型的。 Oracle 不支持。Mysql 支持 4:sequence:由底层的数据根据序列来生成主键。oracle ,db2数据库支持,mysql不支持 5:native:(跨平台的) 由hibernate 自己跟据不同的数据库的主键生成方式,自己选择生成方式(可以跨平台) 6:hilo :(跨平台的),通过hi/lo算法实现主键的生成机制,但是需要额外的表来保存主键的生成历史,使用时要先插入一个值。 使用规则: 通用性和安全性: 7:seqhilo:取得sequence的值,来生成高位,产生id。 但是只能使用于有sequence的数据库(oracle , db2); 8:uuid.hex: 用系统时间和ip地址等具有天然唯一性的资源进行计算,生成id, 全球唯一。生成的是一个128位的二进制数。然后把这个二进制数,转化成一个16进制的数(32位),再转化为字符串付给我们的id。那么我们的实体类的oid以及数据库的主键都需要随之改为字符串类型的。 9:foreign 用于共享主键的时候。(一对一主键关联);

我要回帖

更多关于 eclips导入项目 的文章

 

随机推荐