当前位置:编程学习 > JAVA >>

hibernate 设置联合主键的时候出现异常

package com.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.IdClass;
import javax.persistence.Id;
@Entity
@javax.persistence.TableGenerator(
    name="Teacher_GEN",
    table="GENERATOR_TABLE",
    pkColumnName = "pk_key",
    valueColumnName = "pk_value",
    pkColumnValue="Teacher",
    allocationSize=1
)
@IdClass(TeacherPK.class)
public class Teacher {

private int id;
private String name;
private String title; 
@Id
@GeneratedValue(strategy=GenerationType.TABLE,generator = "Teacher_GEN")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}


org.hibernate.AnnotationException: Unknown Id.generator: Teacher_GEN
at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:507)
at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:2407)
at org.hibernate.cfg.AnnotationBinder.bindIdClass(AnnotationBinder.java:2472)
at org.hibernate.cfg.AnnotationBinder.mapAsIdClass(AnnotationBinder.java:846)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:672)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3431)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3385)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1337)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1727)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1778)
at com.hibernate.TeacherTest.beforClass(TeacherTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:306)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

java.lang.NullPointerException
at com.hibernate.TeacherTest.afterClass(TeacherTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
at org.junit.runners.ParentRunner.run(ParentRunner.java:306)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
--------------------编程问答-------------------- 你的TeacherPK.class呢?序列化、重写equals、hashcode了么? --------------------编程问答--------------------
引用 1 楼  的回复:
你的TeacherPK.class呢?序列化、重写equals、hashcode了么?

package com.hibernate;

import javax.persistence.Embeddable;

//@Embeddable
public class TeacherPK implements java.io.Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o){
if(o instanceof TeacherPK){
TeacherPK pk = (TeacherPK)o;
if(this.id ==pk.getId() && this.name.equals(pk.getName())){
return true;
}
}
return false;
}
@Override
public int hashCode(){
return this.name.hashCode();
}

}

--------------------编程问答--------------------  @GeneratedValue(strategy=GenerationType.TABLE,generator = "Teacher_GEN")注释掉就行了 --------------------编程问答-------------------- 因为无法识别id和name哪一个是序列化 --------------------编程问答-------------------- 楼主搞定了没 我也在做这个实验 搞定的话 还请楼主指导一下
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,