您好我正在使用@ManyToOne Annotation进行实体映射。
下面的是我的实体,它们之间有许多关联
student.java
@Entity
@Table(name = "student")
public class student{
@Id
@Column(name = "UserID")
private String userid;
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "userrole1", referencedColumnName = "VALUE"),
@JoinColumn(name = "userrole2", referencedColumnName = "DESCRIPTION")
})
private studentdetails1 userrole1;
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "userrole1", referencedColumnName = "VALUE",insertable=false, updatable=false),
@JoinColumn(name = "userrole2", referencedColumnName = "DESCRIPTION", insertable=false, updatable=false)
})
private studentdetails2 userrole2;
//setters and getters
//constructor
}
studentdetails1.java
@Data
@Entity
@Table(name = "student_details1")
public class studentdetails1 {
@Id
@Column(name = "VALUE")
private String value;
@Id
@Column(name = "DESCRIPTION")
private String description;
//setters and getters
//constructor
}
studentdetails2.java
@Data
@Entity
@Table(name = "student_details2")
public class studentdetails2 {
@Id
@Column(name = "VALUE")
private String value;
@Id
@Column(name = "DESCRIPTION")
private String description;
//setters and getters
//constructor
}
appmain.java
public static void main()
{
//session configuration
StudentDetails1 sd1 = new StudentDetails1();
sd1.setValue("abc");
sd1.setDescription("abcdef");
StudentDetails2 sd2 = new StudentDetails2();
sd2.setValue("abcd");
sd2.setDescription("abcdefd");
Student student1 = new Student();
student1.setUserid("1");
student1.setUserrole1(sd1);
student1.setUserrole2(sd2);
Student student2 = new Student();
student2.setUserid("2");
student2.setUserrole1(sd1);
student2.setUserrole2(sd2);
session.save(student1 );
session.save(student2 );
Student con = (Student ) session.get(Student .class,
String.valueOf(1));
System.out.println(con.getUserid());
}
获取以下行的错误
student con = (student ) session.get(student .class,
String.valueOf(1));
我正在使用EAGER FETCH TYPE,当我保存数据时我可以保存并使用SESSION.GET检索数据但是当我评论session.save方法时,即使数据库中有这些表的值,我也是低于错误
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.studentdetails2e#studentdetails2(value=1, description=null)]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:447)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:233)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1028)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:623)
at org.hibernate.type.EntityType.resolve(EntityType.java:431)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1933)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3270)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
at com.softsol.test.ActvDayRangeRlTest.AccountValid(ActvDayRangeRlTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)
我试图解决这个问题,但它显示出同样的错误。
请建议我如何解决这个问题
答案 0 :(得分:4)
从stacktrace开始,它表示hibernate加载了Student
实体,当它试图加载StudentDetail
实体时,它无法在匹配该复合主键的数据库表中找到记录。
根据堆栈跟踪,我猜问题是这样的:
您在Student
表中有一条记录,主键值为1
,外键值为userrole1=1
和userrole2=null
。因此,当您尝试使用hibernate从1
表中获取id为Student
的记录时,并且获取策略为EAGER
,那么它会尝试在Student_Detail
表中查找记录复合主键为VALUE=1
和DESCRIPTION=null
但是hibernate无法找到它并抛出异常说:
No row with the given identifier exists: [com.studentdetails2e#studentdetails(value=1, description=null)]
因此,请检查Student表中的数据库数据,主键为1
,外键映射应该不正确。我再次猜测在DB端,表之间没有任何外键关系,因为如果是这种情况,那么DB将不允许不正确的FKeys。
<强>更新强>
当您使用LAZY
进行映射并尝试获取Student
的记录时,hibernate将为集合元素分配代理,即StudentDetail。当您尝试访问此集合元素时,只有hibernate将访问数据库以获取StiudentDetail的记录。
如果加载EAGER
,当你尝试获取Student
的记录时,那么hibernate本身会尝试获取映射StudentDetail实体,因此你在使用{{{{{ 1}}但不适用于EAGER
抓取。
答案 1 :(得分:0)
以我为例,所有注释均正确无误,但在数据库中,table A fk---> table B
之间缺少FK,因此表A
中存在一行值为b
的行。在表B中不存在(此错误应在迁移中检测到)。
该异常可能使您的注意力从实际问题中转移出来,它“找不到行xxx',因此无法恢复Entity X
,但是问题可能是其他任何实体(作为X的属性) ,例如X#user
或X#person