JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

一、简介

1.You now have three methods for distinguishing references:

? Objects are identical if they occupy the same memory location in the JVM . This can be checked with the a == b operator. This concept is known as object identity.
? Objects are equal if they have the same state, as defined by the a.equals(Object b) method. Classes that don’t explicitly override this method inherit the implementation defined by java.lang.Object , which compares object identity with == . This concept is known as object equality.
? Objects stored in a relational database are identical if they share the same table and primary key value. This concept, mapped into the Java space, is known as database identity.

2.To become the primary key, a candidate key must satisfy the following requirements:

? The value of any candidate key column is never null. You can’t identify something with data that is unknown, and there are no nulls in the relational model.Some SQL products allow defining (composite) primary keys with nullable columns, so you must be careful.
? The value of the candidate key column(s) is a unique value for any row.
? The value of the candidate key column(s) never changes; it’s immutable.

3.JPA标准支持的生id成策略

Usually you want the system to generate a primary key value when you save an entity instance, so you write the @GeneratedValue annotation next to @Id . JPA standardizes several value-generation strategies with the  javax.persistence.GenerationType enum, which you select with @GeneratedValue(strategy = ...) :
? GenerationType.AUTO —Hibernate picks an appropriate strategy, asking the SQL dialect of your configured database what is best. This is equivalent to @GeneratedValue() without any settings.
? GenerationType.SEQUENCE —Hibernate expects (and creates, if you use the tools) a sequence named HIBERNATE_SEQUENCE in your database. The sequence will be called separately before every INSERT , producing sequential numeric values.
? GenerationType.IDENTITY —Hibernate expects (and creates in table DDL ) a special auto-incremented primary key column that automatically generates a numeric value on INSERT , in the database.
? GenerationType.TABLE —Hibernate will use an extra table in your database schema that holds the next numeric primary key value, one row for each entity class. This table will be read and updated accordingly, before INSERT s. The default table name is HIBERNATE_SEQUENCES with columns SEQUENCE_NAME and SEQUENCE_NEXT_HI_VALUE . (The internal implementation uses a more complex but efficient hi/lo generation algorithm; more on this later.)

4.用生成器生成id

JPA has two built-in annotations you can use to configure named generators: @javax .persistence.SequenceGenerator and @javax.persistence.TableGenerator .但Hibernate的更灵活,可以把生成器定义在与domainpackage-info.java文件中,且支持所有Hibernate提供的功能,可以自定义sequence表名,初始化值等。you can use the Hibernate annotation in a package-info.java file, typically in the same package as your domain model classes.Hibernate支持的生成器如下:

(1)native —Automatically selects other strategies, such as sequence or identity ,depending on the configured SQL dialect.

(2)sequence —Uses a native database sequence named  HIBERNATE_SEQUENCE .( org.hibernate.id.SequenceGenerator .)

(3)sequence-identity —Generates key values by calling a database sequence on insertion: for example, insert into ITEM(ID) values (HIBERNATE_SEQUENCE.nextval) .(org.hibernate.id.SequenceIdentityGenerator)

(4)enhanced-sequence —Uses a native database sequence when supported; otherwise falls back to an extra database table with a single column and row, emulating a sequence. Defaults to name HIBERNATE_SEQUENCE

,most likely your best option of the built-in strategies.( org.hibernate.id.enhanced.SequenceStyleGenerator )

(5)seqhilo —Uses a native database sequence named HIBERNATE_SEQUENCE , optimizing calls before INSERT by combining hi/lo values.(org.hibernate.id.SequenceHiLoGenerator)

(6)hilo —Uses an extra table named HIBERNATE_UNIQUE_KEY with the same algorithm as the seqhilo strategy.( org.hibernate.id.TableHiLoGenerator)

(7)enhanced-table —Uses an extra table named HIBERNATE_SEQUENCES , with one row by default representing the sequence, storing the next value.( org.hibernate.id.enhanced.TableGenerator )

(8)identity —Supports IDENTITY and auto-increment columns in DB2 , MySQL,MS SQL Server, and Sybase.

(9)increment —At Hibernate startup, reads the maximum (numeric) primary key column value of each entity’s table and increments the value by one each time a new row is inserted.

(10)select —Hibernate won’t generate a key value or include the primary key column in an INSERT statement. Hibernate expects the DBMS to assign a (default in schema or by trigger) value to the column on insertion. Hibernate then retrieves the primary key column with a SELECT query after insertion.

(11)uuid2 —Produces a unique 128-bit UUID in the application layer. Useful when you need globally unique identifiers across databases( org.hibernate.id.UUIDGenerator)

(12)guid —Uses a globally unique identifier produced by the database, with an SQL function available on Oracle, Ingres, MS SQL Server, and MySQL. Hibernate calls the database function before an INSERT .(  org.hibernate.id.IdentityGenerator)

总结:最好用pre-insert generation strategies,最好用enhanced-sequence

二、代码

1.结构

2.实体类

(1)

  1 package org.jpwh.model.simple;
  2
  3 import javax.persistence.Entity;
  4
  5 import javax.persistence.FetchType;
  6 import javax.persistence.GeneratedValue;
  7 import javax.persistence.Id;
  8 import javax.persistence.ManyToOne;
  9 import javax.persistence.Transient;
 10 import javax.persistence.Version;
 11 import javax.validation.constraints.Future;
 12 import javax.validation.constraints.NotNull;
 13 import javax.validation.constraints.Size;
 14
 15 import java.math.BigDecimal;
 16 import java.util.Date;
 17 import java.util.HashSet;
 18 import java.util.Set;
 19
 20 @Entity
 21 public class Item {
 22
 23     @Id
 24     @GeneratedValue(generator = "ID_GENERATOR")
 25     protected Long id;
 26
 27     public Long getId() { // Optional but useful
 28         return id;
 29     }
 30
 31     @Version
 32     protected long version;
 33
 34     @NotNull
 35     @Size(
 36         min = 2,
 37         max = 255,
 38         message = "Name is required, maximum 255 characters."
 39     )
 40     protected String name;
 41
 42     @Future
 43     protected Date auctionEnd;
 44
 45     public String getName() {
 46         return name;
 47     }
 48
 49     public void setName(String name) {
 50         this.name = name;
 51     }
 52
 53     public Date getAuctionEnd() {
 54         return auctionEnd;
 55     }
 56
 57     public void setAuctionEnd(Date auctionEnd) {
 58         this.auctionEnd = auctionEnd;
 59     }
 60
 61     protected BigDecimal buyNowPrice;
 62
 63     public BigDecimal getBuyNowPrice() {
 64         return buyNowPrice;
 65     }
 66
 67     public void setBuyNowPrice(BigDecimal buyNowPrice) {
 68         this.buyNowPrice = buyNowPrice;
 69     }
 70
 71     @Transient
 72     protected Set<Bid> bids = new HashSet<Bid>();
 73
 74     public Set<Bid> getBids() {
 75         return bids;
 76     }
 77
 78     public void setBids(Set<Bid> bids) {
 79         this.bids = bids;
 80     }
 81
 82     @ManyToOne(fetch = FetchType.LAZY)
 83     protected Category category;
 84
 85     public Category getCategory() {
 86         return category;
 87     }
 88
 89     public void setCategory(Category category) {
 90         this.category = category;
 91     }
 92
 93     public void addBid(Bid bid) {
 94         // Be defensive
 95         if (bid == null)
 96             throw new NullPointerException("Can‘t add null Bid");
 97         if (bid.getItem() != null)
 98             throw new IllegalStateException("Bid is already assigned to an Item");
 99
100         getBids().add(bid);
101         bid.setItem(this);
102     }
103
104     public Bid placeBid(Bid currentHighestBid, BigDecimal bidAmount) {
105         if (currentHighestBid == null ||
106                 bidAmount.compareTo(currentHighestBid.getAmount()) > 0) {
107             return new Bid(bidAmount, this);
108         }
109         return null;
110     }
111 }

3.辅助类

(1)

 1 package org.jpwh.model;
 2
 3 public interface Constants {
 4
 5     // These are not used in the simple.Item class and in the model/package-info.java,
 6     // as this would only confuse new users even more. So don‘t forget to rename them
 7     // there if you rename them here.
 8     public static final String ID_GENERATOR = "ID_GENERATOR";
 9     public static final String ID_GENERATOR_POOLED = "ID_GENERATOR_POOLED";
10
11 }

(2)package-info.java

 1 @org.hibernate.annotations.GenericGenerator(
 2   name = "ID_GENERATOR",
 3   strategy = "enhanced-sequence",
 4   parameters = {
 5      @org.hibernate.annotations.Parameter(
 6         name = "sequence_name",
 7         value = "JPWH_SEQUENCE"
 8      ),
 9      @org.hibernate.annotations.Parameter(
10         name = "initial_value",
11         value = "1000"
12      )
13 })
14 package org.jpwh.model;
时间: 2024-10-11 08:56:11

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解的相关文章

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(&lt;delimited-identifiers/&gt;、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)

一.自定义映射的表名 1. 1 @Entity 2 @Table(name = "USERS") 3 public class User implements Serializable { 4 // ... 5 } 2.用定界符 1 //@Table(name = "`USER`")的标准 2 @Table(name = "`USER`") 3 4 //JPA的标准 5 @Table(name = "\"USER\"

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-001区分entities and value types

一.介绍 1.这种引用方式不对,但删除时不能级联 要这种引用方式 2.The Bid class could be a problem. In object-oriented modeling, this is marked as a composition (the association between Item and Bid with the diamond). Thus, an Item is the owner of its Bid instances and holds a col

JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-004Table per class hierarchy(@Inheritance..SINGLE_TABLE)、@DiscriminatorColumn、@DiscriminatorValue、@DiscriminatorFormula)

一.结构 You can map an entire class hierarchy to a single table. This table includes columns for all properties of all classes in the hierarchy. The value of an extra type discriminator column or formula identifies the concrete subclass represented by a

JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-004嵌套组件的注解AttributeOverrides

一.数据库 二.代码 1. 1 package org.jpwh.model.advanced; 2 3 import javax.persistence.AttributeOverride; 4 import javax.persistence.AttributeOverrides; 5 import javax.persistence.Column; 6 import javax.persistence.Embeddable; 7 import javax.validation.constr

JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)

一.结构 二.代码 1. 1 package org.jpwh.model.collections.mapofstrings; 2 3 import org.jpwh.model.Constants; 4 5 import javax.persistence.CollectionTable; 6 import javax.persistence.Column; 7 import javax.persistence.ElementCollection; 8 import javax.persist

JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)

一.结构 二.代码 1. 1 package org.jpwh.model.collections.listofstrings; 2 3 import org.jpwh.model.Constants; 4 5 import javax.persistence.CollectionTable; 6 import javax.persistence.Column; 7 import javax.persistence.ElementCollection; 8 import javax.persis

JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)

一.结构 二.代码 1. 1 package org.jpwh.model.collections.setofstrings; 2 3 import org.jpwh.model.Constants; 4 5 import javax.persistence.CollectionTable; 6 import javax.persistence.Column; 7 import javax.persistence.ElementCollection; 8 import javax.persist

Python核心编程(第二版) 第四章习题答案

4-1.Python对象.与所有Python对象有关的三个属性是什么?请简单的描述一下.答:与所有Python对象有关的三个属性是身份.类型.值.身份:每一个对象都有一个唯一的身份标识自己,任何对象的身份可以使用内建函数id()来得到.这个值可以被认为是该对象的内存地址.类型:对象的类型决定了该对象可以保存什么类型的值,可以进行什么样的操作,以及遵循什么规则.可以用内建函数type()来查看Python的类型.值:对象表示的数据项.4-2.类型.不可更改(immutable)指的是什么?Pyth

“全栈2019”Java第六十四章:接口与静态方法详解

难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第六十四章:接口与静态方法详解 下一章 "全栈2019"Java第六十五章:接口与默认方法详解 学习小组 加入同步学习小组,共同交流与进步. 方式一:关注头条号Gorhaf,私信"Java学习小组". 方式二:关注公众号Gorhaf,回复"Java学习小组&qu