【问题标题】:How do I configure a many-to-one and one-to-many relationship in spring? Hibernate keeps call the same query infinitelyspring如何配置多对一和一对多的关系? Hibernate 不断调用相同的查询
【发布时间】:2020-02-21 04:05:33
【问题描述】:

我一直在阅读与此问题相关的一些较早的帖子,但没有一个建议的解决方案对我有用。无论我尝试拨打电话,都会收到堆栈溢出错误。

有谁知道如何解决这个问题?

我有这样的设置:

public class SchoolDistrict implements Serializable {
    @OneToMany(mappedBy="schoolDistrict", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonIgnore
    //@JsonManagedReference
    private List<SchoolDistrictSvc> schoolDistrictSvc;
    ...
    //getters and setters
}

public class SchoolDistrictSvc implements Serializable {
    @ManyToOne
    @JoinColumn(name = "SCHOOL_DISTRICT_ID", referencedColumnName = "SCHOOL_DISTRICT_ID", insertable=false, updatable=false)
    @JsonIgnore
    //@JsonBackReference
    private SchoolDistrict schoolDistrict;

    ...
    //getters setters
} 

hibernate 生成的 sql 查询都是相同的,使用相同的 ? 值。

堆栈跟踪:

Hibernate: select schooldist0_.school_district_id as school_district_id1_19_1_, schooldist0_.city as city2_19_1_, schooldist0_.create_timestamp as create_timestamp3_19_1_, schooldist0_.created_by_id as created_by_id4_19_1_, schooldist0_.created_by_name as created_by_name5_19_1_, schooldist0_.date_display_type as date_display_type6_19_1_, schooldist0_.first_view_customer as first_view_custome7_19_1_, schooldist0_.fv_streets_customer as fv_streets_custome8_19_1_, schooldist0_.fv_student_customer as fv_student_custome9_19_1_, schooldist0_.jde_number as jde_number10_19_1_, schooldist0_.last_updated_by_id as last_updated_by_i11_19_1_, schooldist0_.last_updated_by_name as last_updated_by_n12_19_1_, schooldist0_.last_updated_timestamp as last_updated_time13_19_1_, schooldist0_.postal_code as postal_code14_19_1_, schooldist0_.route_management_type as route_management_15_19_1_, schooldist0_.school_calendar_enabled as school_calendar_e16_19_1_, schooldist0_.school_district_code as school_district_c17_19_1_, schooldist0_.school_district_name as school_district_n18_19_1_, schooldist0_.state as state19_19_1_, schooldist0_.state_plane_coordinate_project as state_plane_coord20_19_1_, schooldist0_.state_plane_coordinate_trans as state_plane_coord21_19_1_, schooldist0_.status as status22_19_1_, schooldist0_.unit_of_measure as unit_of_measure23_19_1_, schooldist1_.school_district_id as school_district_id1_22_3_, schooldist1_.business_unit_id as business_unit_id2_22_3_, schooldist1_.created_by_id as created_by_id3_22_3_, schooldist1_.create_timestamp as create_timestamp4_22_3_, schooldist1_.school_district_id as school_district_id1_22_0_, schooldist1_.business_unit_id as business_unit_id2_22_0_, schooldist1_.created_by_id as created_by_id3_22_0_, schooldist1_.create_timestamp as create_timestamp4_22_0_ from school_district schooldist0_ left outer join school_district_service schooldist1_ on schooldist0_.school_district_id=schooldist1_.school_district_id where schooldist0_.school_district_id=?
Hibernate: select schooldist0_.school_district_id as school_district_id1_19_1_, schooldist0_.city as city2_19_1_, schooldist0_.create_timestamp as create_timestamp3_19_1_, schooldist0_.created_by_id as created_by_id4_19_1_, schooldist0_.created_by_name as created_by_name5_19_1_, schooldist0_.date_display_type as date_display_type6_19_1_, schooldist0_.first_view_customer as first_view_custome7_19_1_, schooldist0_.fv_streets_customer as fv_streets_custome8_19_1_, schooldist0_.fv_student_customer as fv_student_custome9_19_1_, schooldist0_.jde_number as jde_number10_19_1_, schooldist0_.last_updated_by_id as last_updated_by_i11_19_1_, schooldist0_.last_updated_by_name as last_updated_by_n12_19_1_, schooldist0_.last_updated_timestamp as last_updated_time13_19_1_, schooldist0_.postal_code as postal_code14_19_1_, schooldist0_.route_management_type as route_management_15_19_1_, schooldist0_.school_calendar_enabled as school_calendar_e16_19_1_, schooldist0_.school_district_code as school_district_c17_19_1_, schooldist0_.school_district_name as school_district_n18_19_1_, schooldist0_.state as state19_19_1_, schooldist0_.state_plane_coordinate_project as state_plane_coord20_19_1_, schooldist0_.state_plane_coordinate_trans as state_plane_coord21_19_1_, schooldist0_.status as status22_19_1_, schooldist0_.unit_of_measure as unit_of_measure23_19_1_, schooldist1_.school_district_id as school_district_id1_22_3_, schooldist1_.business_unit_id as business_unit_id2_22_3_, schooldist1_.created_by_id as created_by_id3_22_3_, schooldist1_.create_timestamp as create_timestamp4_22_3_, schooldist1_.school_district_id as school_district_id1_22_0_, schooldist1_.business_unit_id as business_unit_id2_22_0_, schooldist1_.created_by_id as created_by_id3_22_0_, schooldist1_.create_timestamp as create_timestamp4_22_0_ from school_district schooldist0_ left outer join school_district_service schooldist1_ on schooldist0_.school_district_id=schooldist1_.school_district_id where schooldist0_.school_district_id=?
Hibernate: select schooldist0_.school_district_id as school_district_id1_19_1_, schooldist0_.city as city2_19_1_, schooldist0_.create_timestamp as create_timestamp3_19_1_, schooldist0_.created_by_id as created_by_id4_19_1_, schooldist0_.created_by_name as created_by_name5_19_1_, schooldist0_.date_display_type as date_display_type6_19_1_, schooldist0_.first_view_customer as first_view_custome7_19_1_, schooldist0_.fv_streets_customer as fv_streets_custome8_19_1_, schooldist0_.fv_student_customer as fv_student_custome9_19_1_, schooldist0_.jde_number as jde_number10_19_1_, schooldist0_.last_updated_by_id as last_updated_by_i11_19_1_, schooldist0_.last_updated_by_name as last_updated_by_n12_19_1_, schooldist0_.last_updated_timestamp as last_updated_time13_19_1_, schooldist0_.postal_code as postal_code14_19_1_, schooldist0_.route_management_type as route_management_15_19_1_, schooldist0_.school_calendar_enabled as school_calendar_e16_19_1_, schooldist0_.school_district_code as school_district_c17_19_1_, schooldist0_.school_district_name as school_district_n18_19_1_, schooldist0_.state as state19_19_1_, schooldist0_.state_plane_coordinate_project as state_plane_coord20_19_1_, schooldist0_.state_plane_coordinate_trans as state_plane_coord21_19_1_, schooldist0_.status as status22_19_1_, schooldist0_.unit_of_measure as unit_of_measure23_19_1_, schooldist1_.school_district_id as school_district_id1_22_3_, schooldist1_.business_unit_id as business_unit_id2_22_3_, schooldist1_.created_by_id as created_by_id3_22_3_, schooldist1_.create_timestamp as create_timestamp4_22_3_, schooldist1_.school_district_id as school_district_id1_22_0_, schooldist1_.business_unit_id as business_unit_id2_22_0_, schooldist1_.created_by_id as created_by_id3_22_0_, schooldist1_.create_timestamp as create_timestamp4_22_0_ from school_district schooldist0_ left outer join school_district_service schooldist1_ on schooldist0_.school_district_id=schooldist1_.school_district_id where schooldist0_.school_district_id=?
2019-10-24 14:45:13.110 ERROR 8404 --- [nio-8081-exec-1] o.s.t.i.TransactionInterceptor           : Application exception overridden by rollback exception

java.lang.StackOverflowError: null
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774) ~[ojdbc7-12.1.0.jar:12.1.0.2.0]
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925) ~[ojdbc7-12.1.0.jar:12.1.0.2.0]
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111) ~[ojdbc7-12.1.0.jar:12.1.0.2.0]
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798) ~[ojdbc7-12.1.0.jar:12.1.0.2.0]
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845) ~[ojdbc7-12.1.0.jar:12.1.0.2.0]
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501) ~[ojdbc7-12.1.0.jar:12.1.0.2.0]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2167) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1930) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1892) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:937) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:310) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]

    //skipped some lines is stack trace

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200) ~[tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_171]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.17.jar:9.0.17]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_171]


【问题讨论】:

  • 你的数据库中有多少数据?你能展示你用于表的架构脚本吗?另外,什么 exacle 代码触发了 StackOverflow?

标签: java spring hibernate join


【解决方案1】:

我不知道这是否会导致您的问题,但您应该在映射的许多方面使用延迟加载。

@ManyToOne(fetch = FetchType.LAZY)

此外,如果您的查询始终是单向的,您应该将其实现为单向关系。查看单向@OneToMany on this site.

【讨论】:

    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多