JPA SQLQuery 不适用于 java 实体

Posted

技术标签:

【中文标题】JPA SQLQuery 不适用于 java 实体【英文标题】:JPA SQLQuery doesn't work for java entity 【发布时间】:2021-07-04 18:20:54 【问题描述】:

我使用 Spring 和 Hibernate 开发了我的 Java 应用程序,并且我有两个实体。

@Entity 
@Table(name = "Users")
public class User 

    private int idUser;
    
    private String login;
    
    private String password;
    
    private List<Command> commands = new ArrayList<>();

    
    public User()  
    
    public User( String login, String password ) 
        super();
        this.setLogin( login );
        this.setPassword( password );
    

    @Id
    @Column(name = "USER_ID", unique = true, nullable = false, scale = 0)
    public int getIdUser() 
        return idUser;
    
    
    @Column(name = "USER_LOGIN", nullable = false)
    public String getLogin() 
        return login;
    
    
    public void setLogin(String login) 
        this.login = login;
    

    @Column(name = "USER_PASSWORD", nullable = false)
    public String getPassword() 
        return password;
    
    
    public void setPassword(String password) 
        this.password = password;
    
    
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "utilisateur")
    public List<Command> getCommands() 
        return commands;
    
    
    public String toString() 
        return this.idUser + ": " + this.login + "/" + this.password 
             ;
    
    




@Entity  
@Table(name="Commands")
public class Command 
    
    private int idCommand;
    
    private User utilisateur;
    
    private Date commandDate;
    
    
    public Command() 
    
    public Command( User user, Date commandDate ) 
        this.setUser( user );
        this.setCommandDate( commandDate );
    

    @Id
    @Column(name = "CMD_Id", unique = true, nullable = false, scale = 0)
    public int getIdCommand() 
        return idCommand;
    

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID")
    public User getUtilisateur() 
        return user;
    

    public void setUtilisateur(User user) 
        this.user = user;
    
    
    @Column(name = "CMD_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCommandDate() 
        return commandDate;
    
    
    public void setCommandDate(Date commandDate) 
        this.commandDate = commandDate;
    
    
    public String toString() 
        StringBuilder builder = new StringBuilder();
        builder.append( "Commande de >> " ).append( this.user )
               .append( " - " ).append( this.commandDate ).append( "\n" );
        return builder.toString();
       
    

我创建了我的服务类以使用 Hibernate 获得结果

@Service
@Transactional
public class MyServiceImpl implements MyService 
    private static final long serialVersionUID = 8393594103219622298L;

    private static final String MY_REQUEST 
        ="SELECT us.login, cd.date, us.password "+
    " FROM user us , commande cd "+ 
    " WHERE "+
    " us.idUser = cd.user.idUser AND "+
    " us.idUser = 1 ";
    
    public List<Object> findUsersCmd() 
        String query1 = MY_REQUEST;
        SQLQuery queryObj = sessionFactory.getCurrentSession().createSQLQuery(query1);
        List<Object> lstObj =queryObj.list(); 
        return lstObj;
    
    

当我调试时,我在这一行得到这个错误:

List<Object> lstObj =queryObj.list();

104165 [http-bio-8080-exec-3] 警告 org.hibernate.util.JDBCExceptionReporter - SQL 错误:904,SQLState: 42000

104166 [http-bio-8080-exec-3] 错误 org.hibernate.util.JDBCExceptionReporter - ORA-00904: "US"."IDUSER" : 身份验证无效

我该如何解决我的问题?

【问题讨论】:

user 应该是 User 【参考方案1】:

如果您想运行 JPA 查询,它应该如下所示:

private static final String MY_REQUEST =
          "SELECT us.login, cd.date, us.password " +
          "FROM User us JOIN us.commands cd " +
          "WHERE us.idUser = 1";

并确保使用createQuery:

List<Object[]> = sessionFactory.getCurrentSession().createQuery(MY_REQUEST)
    .list();

createSQLQuery 适用于您想要运行本机 SQL 查询的情况。 在这种情况下,它看起来像:

private static final String MY_REQUEST = 
    "select us.USER_ID, cd.CMD_DATE, us.USER_PASSWORD " +
    "from Users us JOIN ComaCommands cd ON us.USER_ID = cd.USER_ID " +
    "where us.USER_ID = 1";


List<Object[]> = sessionFactory.getCurrentSession().createSQLQuery(MY_REQUEST)
    .list();

但您应该尽可能使用 JPQL 查询。

【讨论】:

以上是关于JPA SQLQuery 不适用于 java 实体的主要内容,如果未能解决你的问题,请参考以下文章

JPA 孤儿删除不适用于 OneToOne 关系

Fetch.lazy 不适用于 spring boot jpa,一定是啥问题?

Spring boot JPA - 延迟加载不适用于一对一映射

java 在JPA中使用来自NativeNamedQuery的标量SQLQuery。

为啥以下查询不适用于双 LIKE 子句?

Spring Data Jpa:保存方法仅返回选择,但不执行插入