o.h.engine.jdbc.spi.SqlExceptionHelper : 错误:在“(”附近或附近有语法错误

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

o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: syntax error at or near "("

问题

以下是翻译好的内容:

我正在尝试创建一个用户API,但是在我的简单查询中遇到了上述错误。请查看下面的代码。

在日志中,我得到了以下错误信息:

    Hibernate: 从 public."DetailsCompany" u 中选择新的 UserResponse(u.user_id,u.user_name,u.password) where user_id =?
    2020-08-26 20:31:49.078  警告 26274 --- [nio-7071-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL错误: 0, SQL状态: 42601
    2020-08-26 20:31:49.078  错误 26274 --- [nio-7071-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : 错误: 在 "(" 附近或附近有语法错误
    
<!-- -->    
   
    @Repository
    public interface UserRepository extends JpaRepository<UserEntity, Long>{
      @Query(value="从 public.\"DetailsCompany\" u 中选择新的 UserResponse(u.user_id,u.user_name,u.password) where user_id =:id",nativeQuery = true)
      public UserResponse findUserById(@Param("id")Integer id);
    }
    
    UserResponse.java
    package com.company.user.Response;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class UserResponse{
    	private Integer userId;
    	private String userName;
    	private String password;
    }
    #根据请求的实体类详情#
    #UserEntity.java#
    package com.company.user.Entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Entity
    @Table(name="DetailsCompany", schema = "public")
    public class UserEntity {
    	@Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "user_id")
    private Integer userId;
    @Column(name = "user_name")
    private String userName;
    @Column(name = "password")
    private String password;
    }
英文:

I am trying to make an user API but I am getting above error for my simple query. Please find my below codes.

In logs I am getting this as error

Hibernate: Select new  UserResponse(u.user_id,u.user_name,u.password) from public."DetailsCompany" u where user_id =?
2020-08-26 20:31:49.078  WARN 26274 --- [nio-7071-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42601
2020-08-26 20:31:49.078 ERROR 26274 --- [nio-7071-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: syntax error at or near "("

<!-- -->

@Repository
public interface UserRepository extends JpaRepository&lt;UserEntity, Long&gt;{
  @Query(value=&quot;Select new  UserResponse(u.user_id,u.user_name,u.password) from public.\&quot;DetailsCompany\&quot; u where user_id =:id&quot;,nativeQuery = true)
  public UserResponse findUserById(@Param(&quot;id&quot;)Integer id);
}

UserResponse.java
package com.company.user.Response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserResponse{
	private Integer userId;
	private String userName;
	private String password;
}
#As requested Entity Class Details#
#UserEntity.java#
package com.company.user.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name=&quot;DetailsCompany&quot;, schema = &quot;public&quot;)
public class UserEntity {
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;user_id&quot;)
private Integer userId;
@Column(name = &quot;user_name&quot;)
private String userName;
@Column(name = &quot;password&quot;)
private String password;
}

答案1

得分: 1

你正在将一个 HQL 传递给本地查询。将 nativeQuery 改为 false。

将你的查询修改为:

@Query(value="Select new com.company.user.Response.UserResponse(u.userId,u.userName,u.password) from UserEntity u where u.userId =:id", nativeQuery = false)
英文:

You are passing an HQL to a native query. Change nativeQuery to false.

Change your query to

@Query(value=&quot;Select new  com.company.user.Response.UserResponse(u.userId,u.userName,u.password) from UserEntity u where u.userId =:id&quot;,nativeQuery = false)

答案2

得分: 1

构造函数名称必须是完全限定的。尝试使用 com.your.package.UserResponse

英文:

The constructor name must be fully qualified. Try using com.your.package.UserResponse

huangapple
  • 本文由 发表于 2020年8月26日 23:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63600522.html
匿名

发表评论

匿名网友

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

确定