错误:从Spring Data Jpa中列名称id无效。

huangapple go评论72阅读模式
英文:

Error: The column name id is not valid from Spring Data Jpa

问题

It seems like you're encountering an error related to the column name "id" not being valid when executing a query with Spring Data JPA. This error might be caused by an inconsistency between your entity class and the actual database table structure. Here are some things you can check:

  1. Case Sensitivity: Make sure that the column names in your entity class match the column names in your database table exactly, including case sensitivity. Column names in your entity class are defined using Java naming conventions, which are typically camelCase, while column names in the database are often written in uppercase or lowercase.

  2. Table Name: Double-check that the @Table annotation's name attribute in your entity class matches the actual name of the database table. Also, verify that the schema of the table matches the schema you are connecting to.

  3. Column Annotations: Ensure that each field in your entity class has a @Column annotation with the correct column name that corresponds to your database table. The column names in your entity class should match the column names in the database.

  4. Repository Queries: Your repository interface should have methods that match the queries defined in your @Query annotations. In your repository code, you seem to have two methods with similar queries, but one returns a List<Portal_User> and the other returns a List<PortalUserInterface>. Make sure the return types and method names correspond correctly.

  5. Database Schema: Check the actual schema of your database. It's possible that there is another schema in use, and the JPA connection might not be configured to use the correct schema.

  6. Data Source Configuration: Verify that your data source configuration in your Spring application properties or configuration class is pointing to the correct database.

  7. Entity Class Location: Ensure that your entity class is being scanned and picked up by Spring. The package containing your entity class should be included in the scanning configuration.

  8. Database Connection: If you've recently made changes to your entity class or database schema, make sure you've restarted your application to apply those changes.

Remember to carefully compare the column names in your entity class with the actual column names in the database. Even small discrepancies in case or naming can lead to issues like the one you're encountering.

英文:

I am trying to find some records from a table using Spring Data Jpa but I keep getting error that says The column id is not valid. I have searched online for solutions like <https://stackoverflow.com/questions/31921482/the-column-name-is-not-valid-error> , <https://stackoverflow.com/questions/11269892/the-column-name-is-not-valid> , <https://stackoverflow.com/questions/50859830/spring-data-jpa-the-column-name-id-is-not-valid> and the solutions have not helped my specific case. So I decided to ask for help here.

The error occurs when I hit the controller from postman localhost:8080//api/portal/portal-user

The get request enters the controller and stops at portal_usersRepository.findApprovedUsers(); it doesn’t enter the service class but throws that error at the point of queryin the dB.

PS: The dB table am working with is a legacy table with a lot of null columns.

Below is the entity class:


@Data
@Entity
@Table(name = &quot;PORTAL_USER&quot;)
@JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;, &quot;handler&quot;})
public class Portal_User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = &quot;ID&quot;)
private Long id;
@Column(name = &quot;SURNAME&quot;)
private String surname;
@Column(name = &quot;FIRSTNAME&quot;)
private String firstname;
@Column(name = &quot;OTHER_NAME&quot;)
private String otherName;
@Column(name = &quot;EMAIL&quot;)
private String email;
@Column(name = &quot;PHONE_NUMBER&quot;)
private String phoneNumber;
@Column(name = &quot;USER_ID&quot;)
private String userId;
@Column(name = &quot;APPROVED&quot;)
private Boolean approved;
@ManyToOne
@JoinColumn(name = &quot;DEPARTMENT_FK&quot;, referencedColumnName = &quot;id&quot;)
private Department departmentFk;
@ManyToOne
@JoinColumn(name = &quot;PORTAL_USER_TYPE_FK&quot;, referencedColumnName = &quot;id&quot;)
private Portal_User_Type portalUserTypeFk;
@ManyToOne
@JoinColumn(name = &quot;MINISTRY_FK&quot;, referencedColumnName = &quot;id&quot;)
private Ministry ministryFk;
@ManyToOne
@JoinColumn(name = &quot;AGENCY_FK&quot;, referencedColumnName = &quot;id&quot;)
private Agency agencyFk;
@Column(name = &quot;GENDER&quot;)
private String gender;
@Column(name = &quot;DATE_OF_BIRTH&quot;)
private Date dateOfBirth;
@Column(name = &quot;STREET_NUMBER&quot;)
private String streetNumber;
@Column(name = &quot;POSTCODE&quot;)
private String postcode;
@ManyToOne
@JoinColumn(name = &quot;COUNTRY_FK&quot;, referencedColumnName = &quot;id&quot;)
private Country countryFk;
@Column(name = &quot;NATIONALITY&quot;)
private String nationality;
@Column(name = &quot;ADDRESS&quot;)
private String address;
@Column(name = &quot;CITY&quot;)
private String city;
@Column(name = &quot;STATE&quot;)
private String state;
@Column(name = &quot;LGA&quot;)
private String lga;
@Column(name = &quot;ACCREDITATION_NUMBER&quot;)
private String accreditationNumber;
@ManyToOne
@JoinColumn(name = &quot;PICK_UP_LOCATIONS_FK&quot;, referencedColumnName = &quot;id&quot;)
private Pick_Up_Locations pickUpLocations_fk;
@Column(name = &quot;OCCUPATION&quot;)
private String occupation;
@Column(name = &quot;IS_ACCREDITED_USER&quot;)
private Boolean isAccreditedUser;
@OneToOne(targetEntity = CAC_Branch_Location.class)
@JoinColumn(name = &quot;CAC_BRANCH_LOCATION_FK&quot;, referencedColumnName = &quot;id&quot;)
private CAC_Branch_Location branchLocation;
@Column(name = &quot;STAFF_ID&quot;)
private String staffId;
@Column(name = &quot;IS_CUSTOMER&quot;)
private Boolean isCustomer;
@Column(name = &quot;NIN&quot;)
private String nin;
@Column(name = &quot;identityType&quot;)
private String identityType;
@Column(name = &quot;PWD&quot;)
private String PWD;
@Column(name = &quot;IS_A_SPECIAL_ACCESS_USER&quot;)
private Boolean isASspecialAccessUser;
@Column(name = &quot;UPDATING_OFFICE&quot;)
private String updatingOffice;
@Column(name = &quot;PASSWORD&quot;)
private String password;
@Column(name = &quot;DATE_CREATED&quot;)
private Date dateCreated;
@Column(name = &quot;EMAIL_ADDRESS_VERIFIED&quot;)
private Boolean emailAddressVerified;
@Column(name = &quot;PHONE_NUMBER_VERIFIED&quot;)
private Boolean phoneNumberVerified;
@Column(name = &quot;LOCKED_OUT&quot;)
private Boolean lockedOut;
@Column(name = &quot;LOCKOUT_DATE&quot;)
private Date lockoutDate;
@Column(name = &quot;STATUS&quot;)
private String status;
@Column(name = &quot;SECRET_QUESTION&quot;)
private String secretQuestion;
@Column(name = &quot;SECRET_QUESTION_ANSWER&quot;)
private String secretQuestionAnswer;
@Column(name = &quot;CONTACT_ADDRESS&quot;)
private String contactAddress;
@OneToOne(targetEntity = Address.class)
@JoinColumn(name = &quot;ADDRESS_FK&quot;, referencedColumnName = &quot;id&quot;)
private Address addressFk;
@ManyToOne
@JoinColumn(name = &quot;NATIONALITY_FK&quot;, referencedColumnName = &quot;id&quot;)
private Nationality nationalityFk;
@Column(name = &quot;USERNAME&quot;)
private String username;
@JsonIgnore
@ManyToMany
@JoinTable(name = &quot;portal_user_role&quot;,
joinColumns = @JoinColumn(name = &quot;PORTAL_USER_FK&quot;),
inverseJoinColumns = @JoinColumn(name = &quot;ROLE_FK&quot;))
private List&lt;Role&gt; roles;
@Transient
private long noOfTasks;
@Transient
private AccreditationUserRequests accreditationUserRequests;
public Portal_User() {
}
public Portal_User(Long id, String username, String email) {
this.id = id;
this.email = email;
this.username = username;
}
public Portal_User(Long id, String surname, String firstname, String otherName) {
this.id = id;
this.surname = surname;
this.firstname = firstname;
this.otherName = otherName;
}
public Portal_User(List&lt;Role&gt; roles) {
this.roles = roles;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
Portal_User user = (Portal_User) o;
return Objects.equals(id, user.getId());
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Transient
public String getFullNames(){
String fullNames = this.getFirstname();
if (this.getOtherName()!=null &amp;&amp; this.getOtherName().length() &gt; 0){
fullNames += &quot; &quot;+this.getOtherName();
}
fullNames += &quot; &quot;+this.getSurname();
return fullNames;
}
@Transient
public String getFullAddress2() {
String anAddress = &quot;&quot;;
if (!Strings.isNullOrEmpty(this.address)) {
anAddress = anAddress + this.address;
}
if (!Strings.isNullOrEmpty(this.city)) {
if (Strings.isNullOrEmpty(anAddress)) {
anAddress = this.city;
} else {
anAddress = anAddress + &quot;, &quot; + this.city;
}
}
if (!Strings.isNullOrEmpty(this.state)) {
if (Strings.isNullOrEmpty(anAddress)) {
anAddress = this.state;
} else {
anAddress = anAddress + &quot;, &quot; + this.state;
}
}
return anAddress;
}
}

This is my service class:

@Data
@Service
public class Portal_UserService {
@Autowired
private Portal_UsersRepository portal_usersRepository;
public List&lt;PortalUserDto&gt; findPortalUsers(List&lt;Portal_User&gt; portal_users) {
System.out.println(&quot;Hello SERVICE...&gt;&gt;&gt;&quot;);
List&lt;PortalUserDto&gt; portalUserDtoList = new ArrayList&lt;&gt;();
if (portal_users != null &amp;&amp; !portal_users.isEmpty()) {
portal_users.stream().map(portalUserReport -&gt; {
PortalUserDto portalDto = new PortalUserDto();
portalDto.setFirstname(portalUserReport.getFirstname());
portalDto.setSurname(portalUserReport.getSurname());
portalDto.setEmail(portalUserReport.getEmail());
portalDto.setApproved(portalUserReport.getApproved());
return portalDto;
}).forEachOrdered(portalDto -&gt; {
portalUserDtoList.add(portalDto);
});
}
return portalUserDtoList;
}
}

Here is a snippet from my controller:

    @GetMapping(&quot;/portal-user&quot;)
public ResponseEntity&lt;Object&gt; findPortalByApproved() {
List&lt;PortalUserInterface&gt; portalUsers = portal_usersRepository.findApprovedUsers();
List&lt;PortalUserDto&gt; userDtoList = new ArrayList&lt;&gt;();
System.out.println(&quot;Hello CONTROLLER...&gt;&gt;&gt;&quot;);
if (portalUsers != null) {
portalUsers.forEach((portalUser) -&gt; {
PortalUserDto userDto = new PortalUserDto();
userDto.setFirstname(portalUser.getFirstname());
userDto.setSurname(portalUser.getSurname());
userDto.setEmail(portalUser.getEmail());
userDto.setApproved(portalUser.getApproved());
userDtoList.add(userDto);
});
}
return ResponseEntity.ok(new JsonResponse(&quot;See Data Object for Details&quot;, userDtoList));
}

UPDATE

Here is my repository:

public interface Portal_UsersRepository extends JpaRepository&lt;Portal_User, Long&gt; {
@Query(value=&quot;SELECT FIRSTNAME, SURNAME, EMAIL FROM PORTAL_USER &quot;
+ &quot;WHERE APPROVED = 1 &quot;
+ &quot;ORDER BY ID DESC &quot;, nativeQuery = true)
List&lt;Portal_User&gt; findApprovedPortalUsers();
@Query(value=&quot;SELECT FIRSTNAME, SURNAME, EMAIL FROM PORTAL_USER &quot;
+ &quot;WHERE APPROVED = 1 &quot;
+ &quot;ORDER BY ID DESC &quot;, nativeQuery = true)
List&lt;PortalUserInterface&gt; findApprovedUsers();
}

When I use the entity in my controller like this:
List&lt;Portal_User&gt; portalUsers = portal_usersRepository.findApprovedPortalUsers();
List&lt;PortalUserDto&gt; userDtoList = new ArrayList&lt;&gt;();

I get the following error trace:


...SYSTEM LAUNCHED!!!
2020-09-29 21:28:01.452  INFO 32616 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet &#39;dispatcherServlet&#39;
2020-09-29 21:28:01.456  INFO 32616 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet &#39;dispatcherServlet&#39;
2020-09-29 21:28:01.468  INFO 32616 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms
Hibernate: SELECT FIRSTNAME, SURNAME, EMAIL FROM PORTAL_USER WHERE APPROVED = 1 ORDER BY ID DESC 
2020-09-29 21:28:37.894  WARN 32616 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: S1093
2020-09-29 21:28:37.894 ERROR 32616 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : The column name id is not valid.
2020-09-29 21:28:37.914 ERROR 32616 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: The column name id is not valid.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234) ~[mssql-jdbc-7.4.1.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(SQLServerResultSet.java:686) ~[mssql-jdbc-7.4.1.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getLong(SQLServerResultSet.java:2328) ~[mssql-jdbc-7.4.1.jre8.jar:na]
at com.zaxxer.hikari.pool.HikariProxyResultSet.getLong(HikariProxyResultSet.java) ~[HikariCP-3.4.5.jar:na]
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:808) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:732) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1044) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.processResultSet(Loader.java:995) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:964) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:350) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2887) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2869) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2701) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.Loader.list(Loader.java:2696) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2142) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1163) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:173) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.query.Query.getResultList(Query.java:165) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:126) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:154) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:142) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) ~[spring-tx-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:149) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.sun.proxy.$Proxy119.findApprovedPortalUsers(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.sun.proxy.$Proxy67.findApprovedPortalUsers(Unknown Source) ~[na:na]
at com.oasis.isds.executivedashboard.controller.PortalUsersController.findPortalByApproved(PortalUsersController.java:34) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
2020-09-29 21:56:23.953  WARN 32616 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=14m3s351ms76&#181;s200ns).

And When I use PortalUserInterface, List&lt;PortalUserInterface&gt; portalUsers = portal_usersRepository.findApprovedUsers();

I get this error:

com.microsoft.sqlserver.jdbc.SQLServerException: Read timed out
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2924) ~[mssql-jdbc-7.4.1.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.TDSChannel.read(IOBuffer.java:2029) ~[mssql-jdbc-7.4.1.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:6418) ~[mssql-jdbc-7.4.1.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:7579) ~[mssql-jdbc-7.4.1.jre8.jar:na]

Here is a snippet from my pom.xml


&lt;properties&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
&lt;artifactId&gt;mssql-jdbc&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;

Please help me because this error is happening in different controllers I am using for this project. I have spent hours trying to fix it to no avail. I would appreciate your help.

答案1

得分: 0

我成功地找出了这个问题的原因并进行了修复,因此我决定分享出来,以造福他人。行之有效的方法是使用一个接口来获取记录。但最初这种方法并不起作用,因为数据库表格中有超过400万条记录。这就是我为什么会收到读取超时错误的原因。只有在将项目运行在调试模式下后,我才发现了这一点,因为最初我并没有被通知表格的大小。所以这里是我查询数据库的方式...

我将搜索减少到了前5000条记录...而且它奏效了!

    @Query(value="SELECT TOP 5000 FIRSTNAME, SURNAME, EMAIL FROM PORTAL_USER "
            + "WHERE APPROVED = 1 "
            + "ORDER BY ID DESC ", nativeQuery = true)
    List<PortalUserInterface> findApprovedUsers();

以下是我用于从仓库中获取记录的接口

public interface PortalUserInterface {

    public Long getId();
    
    public String getFirstname();

    public String getSurname();

    public String getEmail();

}

这就是我解决问题的方法。

感谢所有在某种程度上做出贡献的人。

英文:

I was able to identify the cause of this problem and fixed it, so I decided to share for the benefit of others. The approach that worked was using an interface to fetch the records. But this approach did not work initially because the database table has over 4 million records. That was why I was getting the read timed out error. I only discovered this after running my project in debug mode as I was not notified of the size of the table initially. So here is how I queried the database....

I reduced the search to TOP 5000 ... and it worked!

    @Query(value=&quot;SELECT TOP 5000 FIRSTNAME, SURNAME, EMAIL FROM PORTAL_USER &quot;
+ &quot;WHERE APPROVED = 1 &quot;
+ &quot;ORDER BY ID DESC &quot;, nativeQuery = true)
List&lt;PortalUserInterface&gt; findApprovedUsers();

Here is the interface I used for fetching the records from the repository

public interface PortalUserInterface {
public Long getId();
public String getFirstname();
public String getSurname();
public String getEmail();
}

This was how I solved the problem.

Thanks to all who contributed one way or the other.

huangapple
  • 本文由 发表于 2020年9月30日 02:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64125932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定