主键的生成策略
<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:用一个@R_140_5028@的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)
总结以上是内存溢出为你收集整理的主键的生成策略全部内容,希望文章能够帮你解决主键的生成策略所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)