-Mapping persistent classes-002identity详解
Posted shamgod
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了-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;
以上是关于-Mapping persistent classes-002identity详解的主要内容,如果未能解决你的问题,请参考以下文章
在 persistence.xml hibernate 中移除 <class> 标签的使用
不在JPA 的 persistence.xml 文件里配置Entity class的解决的方法
springboot使用遇到问题:Class “model.Address” is listed in the persistence.xml file but not mapped