主键的生成策略

前端之家收集整理的这篇文章主要介绍了主键的生成策略前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

主键的生成策略

<generator>的取值可以为:

1、 increment:用于long,short或者int类型生成的唯一标识。只有在没有其他进程往同一张表中插入数据时才能使用,在集群下不要使用

2、 identity:返回的标识符是long,short或者int类型。对DB2,MS sql Service,Sybase和Hypersonicsql的内置标识符字段提供支持

3、 sequence:在DB2,Postgresql,Oracle,SAPDB,McKOI中使用序列(sequence),而在Interbase中使用生成器(generator)。返回的标识符是long,short或者int类型的

4、 hilo、seqhilo:使用一个高/低位算法高效的生成long,short或者int类型的标识符

5、 uuid:用一个128-bit的UUID算法生成字符串类型的标识符,这在一个网络中是唯一的(使用了IP地址)。UUID被编码为一个32位16进制数字的字符串

6、 guid:在MS sql Server和MysqL中使用数据库生成的GUID字符串

7、 native:根据底层数据库的能力选择identity,sequence或者hilo中的一个

8、 assigned:让应用程序在save()之前为对象分配一个标示符。这是<generator>元素没有指定时的默认的生成策略。

9、 select:通过数据库触发器选择一些唯一主键的行并返回主键值来分配一个主键

10、foreige:使用另一个相关联的对象的标识符。通常和<ont-to-one>联合起来使用

11、sequence-indentity:一种特别的序列生成策略,使用数据库序列来生成实际值,但将它和JDBC3的getGeneratedKeys结合在一起,使得在插入语句执行的时候返回生成的值。

1、2、7可以用于MysqL

3、4、5、6、9用于MysqL时会抛异常

注:验证时用于sql的情况均为直接配置,如:

<generator class=”native”></generator>

3的异常:

org.hibernate.MappingException:Could not instantiate id generator[entity-name=com.csdn.products.domain.Customers]

at org.hibernate.id.factory.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118)

atorg.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:193)

atorg.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:248)

atorg.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)

at com.csdn.products.juint.Demo.test(Demo.java:18)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

atorg.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)

atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

atorg.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)

atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by:org.hibernate.MappingException: org.hibernate.dialect.MysqLInnoDBDialect doesnot support sequences

at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:603)

atorg.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:101)

atorg.hibernate.id.factory.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:111)

... 26 more

9的异常:

org.hibernate.id.IdentifierGenerationException:no natural-id property defined; need to specify [key] in generator parameters

atorg.hibernate.id.SelectGenerator.determineNameOfPropertyToUse(SelectGenerator.java:73)

at org.hibernate.id.SelectGenerator.access$100(SelectGenerator.java:52)

atorg.hibernate.id.SelectGenerator$SelectGeneratorDelegate.<init>(SelectGenerator.java:117)

atorg.hibernate.id.SelectGenerator$SelectGeneratorDelegate.<init>(SelectGenerator.java:98)

at org.hibernate.id.SelectGenerator.getInsertGeneratedIdentifierDelegate(SelectGenerator.java:64)

atorg.hibernate.persister.entity.AbstractEntityPersister.postConstruct(AbstractEntityPersister.java:3175)

atorg.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:439)

atorg.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)

atorg.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)

at com.csdn.products.juint.Demo.test(Demo.java:18)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

atorg.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)

atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

atorg.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

atorg.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)

atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

原文链接:/postgresql/196749.html

猜你在找的Postgre SQL相关文章