CREATE TABLE Stock ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, NO VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, price DECIMAL(6,2) NOT NULL, UNIQUE KEY Stock_NO (NO), INDEX Stock_Name(name) ) ENGINE = InnoDB;
@Entity @Table(name = "Stock", uniqueConstraints = { @UniqueConstraint(name = "Stock_NO", columnNames = { "NO" }) }, indexes = { @Index(name = "Stock_Name", columnList = "name") }) public class Stock { private long id; private String no; private String name; private double price; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long getId() { return id; } public void setId(long id) { this.id = id; }@GeneratedValue(strategy = GenerationType.IDENTITY):实体主键生成策略是自动生成,兼容MySQL主键自动生成策略,关键词是AUTO_INCREMENT。若是MySQL主键没有指定AUTO_INCREMENT,报出以下异常。
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute statement Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
<properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" /> </properties>模式生成虽然很方便,能自动生成表结构,但是,由它生成的表结构不总是最佳的,而且还不能保证是正确的。因此,作为最佳实践,不建议在生产环境启用模式生成,手工维护表机构。禁用模式生成,在配置文件persistence.xml做如下配置:
<properties> <property name="javax.persistence.schema-generation.database.action" value="none" /> </properties>
CREATE TABLE CreatorKey ( TableName VARCHAR(64) NOT NULL PRIMARY KEY, KeyValue BIGINT UNSIGNED NOT NULL, INDEX CreatorKey_Table_Values (TableName, KeyValue) ) ENGINE = InnoDB;
CREATE TABLE Student ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY, name VARCHAR(100) NOT NULL, INDEX Student_name (name) ) ENGINE = InnoDB;
@Entity @Table public class Student { private long id; private String name; @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "studentGenerator") @TableGenerator(name = "studentGenerator", table = "creatorkey", pkColumnName = "TableName", pkColumnValue = "Publishers", valueColumnName = "KeyValue", initialValue = 1, allocationSize = 1) @Column(name = "studentId") public long getId() { return id; }name :主键生成策略定义的名字;table:生成器表在数据库中的名字;pkColumnName:生成器表的主键列的名字;pkColumnValue:生成器表主键列的值;valueColumnName:生成器表值列的名字;initialValue:生成器表初始值;allocationSize:生成器表数值递增或递减幅度。generator:主键生成策略定义的名字,该属性与name属性保持一致;generator = "studentGenerator":使用生成器表的主键生成策略。
/** * (Optional) The initial value to be used to initialize the column * that stores the last value generated. */ int initialValue() default 0; /** * (Optional) The amount to increment by when allocating id * numbers from the generator. */ int allocationSize() default 50;但是,根据实际测试结果,情况并非如源代码表示的那样。清空生成器表creatorkey、目标表student,去掉属性initialValue、allocationSize,执行持久化操作。
Student student = new Student(); student.setName("张三"); manager.persist(student);得到的结果却是这样的。
/** * Defines a primary key generator that may be * referenced by name when a generator element is specified for * the {@link GeneratedValue} annotation. A table generator * may be specified on the entity class or on the primary key * field or property. The scope of the generator name is global * to the persistence unit (across all generator types).
@Entity @Table public class Book implements Serializable { private long id; @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "studentGenerator") public long getId() { return this.id; } public void setId(long id) { this.id = id; }启动web server,报出如下异常。
javax.persistence.PersistenceException: [PersistenceUnit: EntityMappings] Unable to build Hibernate SessionFactory Caused by: org.hibernate.AnnotationException: Unknown Id.generator: studentGenerator org.hibernate.AnnotationException: Unknown Id.generator: studentGenerator若在实体Book加上对主键生成策略的定义,就运行正常。
@Entity @Table public class Book implements Serializable { private long id; @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "studentGenerator") @TableGenerator(name = "studentGenerator", table = "creatorkey", pkColumnName = "TableName", pkColumnValue = "Publishers", valueColumnName = "KeyValue", initialValue = 1, allocationSize=1) public long getId() { return this.id; } public void setId(long id) { this.id = id; }
原文:http://blog.csdn.net/seedshome/article/details/52239664