JPA SQLQuery doesn’t work for java entity

I developp my java application with spring and hibernate and i have two entities

@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();
    }   
    
}

I create my class of service to get result with 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;
    }
    
}

but when i debug , i get this error in this line List lstObj =queryObj.list();

104165 [http-bio-8080-exec-3] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 904, SQLState: 42000

104166 [http-bio-8080-exec-3] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-00904: "US"."IDUSER" : identificateur non valide

How can i resolve my problem ?



Read more here: https://stackoverflow.com/questions/67011460/jpa-sqlquery-doesnt-work-for-java-entity

Content Attribution

This content was originally published by lnikoli31 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: