简体   繁体   中英

How do I configure a many-to-one and one-to-many relationship in spring? Hibernate keeps call the same query infinitely

I have been reading through some older posts related to this issue and none of the proposed solutions have worked for me. Whever I try to make the call i get a stack overflow error.

Does anyone know how to resolve this?

I have a set up like this:

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
} 

The sql queries produced by hibernate are all the same using the same value for the?.

Stacktrace:

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]


I don't know if this could be causing your problem, but you should be using a lazy load on the many side of your mapping.

@ManyToOne(fetch = FetchType.LAZY)

Also, if your query is going to be one-directional all the time, you should implement this as a one-directional relationship. Check out the Unidirectional @OneToMany on this site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM