关闭

org.hibernate.AnnotationException: Unknown Id.generator: Teacher_GEN

标签: hibernatejava控制台
414人阅读 评论(0) 收藏 举报
分类:

hibernate中ID生成策略:

联合主键和Generation_TABLE联合使用,在控制台会报错

八月 31, 2016 10:49:51 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.2.Final}
八月 31, 2016 10:49:51 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
八月 31, 2016 10:49:51 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
八月 31, 2016 10:49:51 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
八月 31, 2016 10:49:52 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
八月 31, 2016 10:49:52 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
八月 31, 2016 10:49:52 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
八月 31, 2016 10:49:52 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate]
八月 31, 2016 10:49:52 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
八月 31, 2016 10:49:52 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
八月 31, 2016 10:49:52 上午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
八月 31, 2016 10:49:52 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
<span style="color:#ff0000;">org.hibernate.AnnotationException: Unknown Id.generator: Teacher_GEN</span>
	at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:641)
	at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:2678)
	at org.hibernate.cfg.AnnotationBinder.bindIdClass(AnnotationBinder.java:2742)
	at org.hibernate.cfg.AnnotationBinder.mapAsIdClass(AnnotationBinder.java:997)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:725)
	at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
	at com.xh.hibernate.model.TeacherTest.beforeClass(TeacherTest.java:42)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

<span style="color:#ff0000;">org.hibernate.AnnotationException: Unknown Id.generator: Teacher_GEN</span>
找不到Id。

所以我们的注解使用Generation_TABLE不可以写在类名称的上面,要写在方法的上面。

@Id
	@javax.persistence.TableGenerator(name = "Teacher_GEN", table = "GENERATOR_TABLE", pkColumnName = "pc_key", valueColumnName = "pc_value", pkColumnValue = "Teacher", allocationSize = 1)
	//表生成器 可以夸平台 很少用
	@SequenceGenerator(name = "teacherSEQ", sequenceName = "teacherSEQ_DB")
	// 使用一个策略 然后sequence名字是teacherSEQ_DB 创建用的是这个名字的表 必须在方法上面,不可以在类上面否则报错
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "Teacher_GEN")
	// 默认使用native 和sequence
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Id
	@Column(name = "_name")
	// 当前表_不对应的话 自己设定字段名
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

我使用了两种生成Generation_TABLE的方法,可以去掉,只要
 generator = "Teacher_GEN"
和两种Generation_TABLE的其中一个名字相同就可以了,我试了后,都可以正常运行。



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4499次
    • 积分:141
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条