Yêu cầu thg 2 11, 2022 9:33 SA 68 0 0
  • 68 0 0
0

Why Hibernate error when get data @Many to many in hibernate, JPA

Chia sẻ
  • 68 0 0

I create 2 entities (Student, Subject) with @ManyToMany relationship, I use mysql database. Why does it fail when i send an api localhost:8080/admin2/student (case 1) from postman

Thank all of you for all your assistance

In Student.java

@Fetch(value = FetchMode.SUBSELECT)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "tbl_student_subject", schema = "supeo_forum",
        joinColumns = {@JoinColumn(name = "student_id")},
        inverseJoinColumns = {@JoinColumn(name = "subject_id")})
private Set<Subject> subjects = new HashSet<>(0);

and in Subject.java


@ManyToMany(fetch = FetchType.EAGER, mappedBy = "subjects")
private Set<Student> students = new HashSet<>(0);

Case 1: Use database in the file resources/supeo_forum.sql When I run api from postman, It causes error

DELETE FROM `tbl_student_subject`;
DELETE FROM `tbl_student`;
DELETE FROM `tbl_subject`;

INSERT INTO `tbl_subject` (`subject_id`, `subject_name`) VALUES
(1, 'Math'),
(2, 'English');

INSERT INTO `tbl_student` (`student_id`, `student_name`) VALUES
(1, 'Jay'),
(2, 'Anna');

INSERT INTO `tbl_student_subject` (`student_id`, `subject_id`) VALUES
(1, 1),
(1, 2),
(2, 2);

Result:

{ "timestamp": "2022-02-11T06:56:48.483+00:00", "status": 500, "error": "Internal Server Error", "trace": "java.lang.StackOverflowError\n\tat com.mysql.cj.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:1565)\n\tat com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)\n\tat com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)\n\tat org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:149)\n\tat org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)\n\tat org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:151)\n\tat org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2122)\n\tat org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2059)\n\tat org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2037)\n\tat org.hibernate.loader.Loader.doQuery(Loader.java:956)\n\tat org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:357)\n\tat org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:327)\n\tat org.hibernate.loader.Loader.loadCollectionSubselect(Loader.java:2645)\n\tat org.hibernate.loader.collection.SubselectCollectionLoader.initialize(SubselectCollectionLoader.java:64)\n\tat org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:705)\n\tat org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76)\n\tat org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)\n\tat org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2203)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:595)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:591)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149)\n\tat org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458)\n\tat com.tuyennguyen.supeo.forum.entity.Student.hashCode(Student.java:13)\n\tat java.base/java.util.HashMap.hash(HashMap.java:340)\n\tat java.base/java.util.HashMap.put(HashMap.java:608)\n\tat java.base/java.util.HashSet.add(HashSet.java:220)\n\tat java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:352)\n\tat org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198)\n\tat org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154)\n\tat org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:232)\n\tat org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:190)\n\tat org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:96)\n\tat org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:105)\n\tat org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87)\n\tat org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:705)\n\tat org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76)\n\tat org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)\n\tat org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2203)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:595)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:591)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149)\n\tat org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458)\n\tat com.tuyennguyen.supeo.forum.entity.Subject.hashCode(Subject.java:11)\n\tat java.base/java.util.HashMap.hash(HashMap.java:340)\n\tat java.base/java.util.HashMap.put(HashMap.java:608)\n\tat java.base/java.util.HashSet.add(HashSet.java:220)\n\tat java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:352)\n\tat org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198)\n\tat org.hibernate.loader.Loader.endCollectionLoad(Loader.java:1257)\n\tat org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1214)\n\tat org.hibernate.loader.Loader.processResultSet(Loader.java:1009)\n\tat org.hibernate.loader.Loader.doQuery(Loader.java:967)\n\tat org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:357)\n\tat org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:327)\n\tat org.hibernate.loader.Loader.loadCollectionSubselect(Loader.java:2645)\n\tat org.hibernate.loader.collection.SubselectCollectionLoader.initialize(SubselectCollectionLoader.java:64)\n\tat org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:705)\n\tat org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76)\n\tat org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)\n\tat org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2203)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:595)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:591)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149)\n\tat org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458)\n\tat com.tuyennguyen.supeo.forum.entity.Student.hashCode(Student.java:13)\n\tat java.base/java.util.HashMap.hash(HashMap.java:340)\n\tat java.base/java.util.HashMap.put(HashMap.java:608)\n\tat java.base/java.util.HashSet.add(HashSet.java:220)\n\tat java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:352)\n\tat org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198)\n\tat org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154)\n\tat org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:232)\n\tat org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:190)\n\tat org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:96)\n\tat org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:105)\n\tat org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87)\n\tat org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:705)\n\tat org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76)\n\tat org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)\n\tat org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2203)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:595)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:591)\n\tat org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149)\n\tat org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458)\n\tat com.tuyennguyen.supeo.forum.entity.Subject.hashCode(Subject.java:11)\n\tat java.base/java.util.HashMap.hash(HashMap.java:340)\n\tat java.base/java.util.HashMap.put(HashMap.java:608)\n\tat java.base/java.util.HashSet.add(HashSet.java:220)\n\tat java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:352)\n\tat org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224)\n\tat org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198)\n\tat org.hibernate.loader.Loader.endCollectionLoad(Loader.java:1257)\n\tat org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1214)\n\tat org.hibernate.loader.Loader.processResultSet(Loader.java:1009)\n\tat org.hibernate.loader.Loader.doQuery(Loader.java:967)\n\tat org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:357)\n\tat ...

Case 2: Use database in the file resources/supeo_forum2.sql When I run api from postman, It causes error

DELETE FROM `tbl_student_subject`;

DELETE FROM `tbl_student`;
INSERT INTO `tbl_student` (`student_id`, `student_name`) VALUES
(1, 'Jay'),
(2, 'Anna');

DELETE FROM `tbl_subject`;
INSERT INTO `tbl_subject` (`subject_id`, `subject_name`) VALUES
(1, 'Math'),
(2, 'English');

Result:

[
    {
        "studentId": 1,
        "studentName": "Jay",
        "subjects": []
    },
    {
        "studentId": 2,
        "studentName": "Anna",
        "subjects": []
    }
]
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí