英文:
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<UserEntity, Long>{
@Query(value="Select new UserResponse(u.user_id,u.user_name,u.password) from public.\"DetailsCompany\" u 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;
}
#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="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;
}
答案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="Select new com.company.user.Response.UserResponse(u.userId,u.userName,u.password) from UserEntity u where u.userId =:id",nativeQuery = false)
答案2
得分: 1
构造函数名称必须是完全限定的。尝试使用 com.your.package.UserResponse
英文:
The constructor name must be fully qualified. Try using com.your.package.UserResponse
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论