我想从实体获取集合类型变量,并且该集合用@OneToMany注释。
public class Cheat {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cheat_seq", length = 10)
private Long cheatSeq;
@OneToMany(mappedBy = "cheat", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<CheatVote> vote;
}
上述实体具有 vote 成员变量。我想得到它,但是当我调用 cheat.getVote()时,它会引发错误。
此外,CheatVote实体如下所示:
public class CheatVote{
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="seq", nullable=false)
private Long seq;
@JoinColumn(name="cheat_fk", referencedColumnName="cheat_seq")
@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@NonNull
private Cheat cheat;
}
如前所述,我叫 cheat.getVote();
@Test
public void test23123() throws Exception{
cheatService.addCheat(addDTO1);
Cheat cheat = cheatService.findAll().get(0);
assertNull(cheat.getVote());
cheatVoteService.addGoodVote(cheat.getCheatSeq(), "127.0.0.1");
cheat = cheatService.findOne(cheat.getCheatSeq());
//cheat.getVote();
}
控制台中的部分错误堆栈如下:
2018-10-14 07:34:14.414 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@63a7af06<rs=HikariProxyResultSet@1779202806 wrapping com.mysql.jdbc.JDBC42ResultSet@2762253e>
2018-10-14 07:34:14.414 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@5f01fb5c<rs=HikariProxyResultSet@460107127 wrapping com.mysql.jdbc.JDBC42ResultSet@18ca9277>
2018-10-14 07:34:14.435 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@75c77add<rs=HikariProxyResultSet@209972599 wrapping com.mysql.jdbc.JDBC42ResultSet@21688427>
2018-10-14 07:34:14.436 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@d5e3f55<rs=HikariProxyResultSet@1786422901 wrapping com.mysql.jdbc.JDBC42ResultSet@2dd2ff87>
2018-10-14 07:34:14.436 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@53df7e67<rs=HikariProxyResultSet@1398327546 wrapping com.mysql.jdbc.JDBC42ResultSet@4a6ef712>
2018-10-14 07:34:14.436 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@3d4b45b<rs=HikariProxyResultSet@1198265211 wrapping com.mysql.jdbc.JDBC42ResultSet@56d5460f>
2018-10-14 07:34:14.436 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@4d0b276e<rs=HikariProxyResultSet@618095995 wrapping com.mysql.jdbc.JDBC42ResultSet@7614378d>
2018-10-14 07:34:14.436 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@31531d0d<rs=HikariProxyResultSet@96665725 wrapping com.mysql.jdbc.JDBC42ResultSet@303fbc4>
2018-10-14 07:34:14.436 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@19d76106<rs=HikariProxyResultSet@1722528506 wrapping com.mysql.jdbc.JDBC42ResultSet@25dad235>
2018-10-14 07:34:14.436 WARN 11852 --- [ main] o.h.e.loading.internal.LoadContexts : HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@3f87780b<rs=HikariProxyResultSet@208438522 wrapping com.mysql.jdbc.JDBC42ResultSet@72168258>
2018-10-14 07:34:14.506 INFO 11852 --- [ Thread-3] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@5fb759d6: startup date [Sun Oct 14 07:33:53 KST 2018]; root of context hierarchy
2018-10-14 07:34:14.550 INFO 11852 --- [ Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-10-14 07:34:14.554 INFO 11852 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2018-10-14 07:34:14.581 INFO 11852 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
JUnit错误面板中的部分错误堆栈如下:
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.aibiigae1221.skyQuiz.data.entity.Cheat.hashCode(Cheat.java:30)
at com.aibiigae1221.skyQuiz.data.entity.CheatVote.hashCode(CheatVote.java:21)
at java.util.HashMap.hash(HashMap.java:338)
at java.util.HashMap.put(HashMap.java:611)
at java.util.HashSet.add(HashSet.java:219)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:327)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:234)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:221)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:194)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:249)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:212)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2223)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:565)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:247)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132)
我的春季启动版本是2.0.5.RELEASE, Maven中的Spring数据jpa版本(我没有手动配置)是2.0.10.RELEASE
出了什么问题?
答案 0 :(得分:0)
如果要接收完全初始化的实体,则提取类型应为EAGER而不是LAZY。在默认模式(LAZY)下,Hibernate不会费心初始化相关对象。