hibernate---table_Generator

首先讲一下调试技巧;;

@javax.persistence.TableGenerator(
	    name="Teacher_GEN",
	    table="GENERATOR_TABLE",  //表明
	    pkColumnName = "key",   //字段
	    valueColumnName = "value", //字段的值
	    pkColumnValue="Teacher",  //记录名
	    allocationSize=1//记录值, 这里是1取完之后自动加一
	)


由于我

 Unsuccessful: create table GENERATOR_TABLE ( key varchar(255),  value integer )  报错;

把这条sql语句拿去execute一下, 发现key是关键词,把字段key换个名字就行了

@javax.persistence.TableGenerator(
	    name="Teacher_GEN",
	    table="GENERATOR_TABLE",  //表明
	    pkColumnName = "pk_key",   //字段
	    valueColumnName = "pk_value", //字段的值
	    pkColumnValue="Teacher",  //记录名
	    allocationSize=1//记录值, 这里是1取完之后自动加一
	)

运行结果:

14:58:04,411  INFO SchemaExport:226 - Running hbm2ddl schema export
14:58:04,416 DEBUG SchemaExport:242 - import file not found: /import.sql
14:58:04,417  INFO SchemaExport:251 - exporting generated schema to database
14:58:04,420 DEBUG SchemaExport:377 - 
    drop table if exists Student
14:58:04,512 DEBUG SchemaExport:377 - 
    drop table if exists Teacher
14:58:04,516 DEBUG SchemaExport:377 - 
    drop table if exists GENERATOR_TABLE
14:58:04,520 DEBUG SchemaExport:377 - 
    create table Student (
        id integer not null,
        name varchar(255),
        age integer,
        primary key (id)
    )
14:58:04,662 DEBUG SchemaExport:377 - 
    create table Teacher (
        id integer not null,
        brithday date,
        hobby varchar(255),
        name varchar(255),
        title varchar(255),
        primary key (id)
    )
14:58:04,811 DEBUG SchemaExport:377 - 
    create table GENERATOR_TABLE (
         pk_key varchar(255),
         pk_value integer 
    ) 
14:58:04,955  INFO SchemaExport:268 - schema export complete
Hibernate: 
    insert 
    into
        Teacher
        (brithday, hobby, name, title, id) 
    values
        (?, ?, ?, ?, ?)



desc generator_table;



mysql> select * from  generator_table;
+---------+----------+
| pk_key  | pk_value |
+---------+----------+
| Teacher |        2 |
+---------+----------+
1 row in set (0.03 sec)

发现value=2了 因为每次查一下都会加一


再Rerun Test(Junit框的一个绿色按钮)一下;

一般用在跨数据库平台,了解一下就够了,工作中比较少用

版权声明:本文为博主原创文章,未经博主允许不得转载。

today lazy . tomorrow die .
原文地址:https://www.cnblogs.com/france/p/4808669.html