Reputation: 17
I developed 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;
}
}
When I debug, I get this error in this line:
List<Object> 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?
Upvotes: 0
Views: 143
Reputation: 8206
If you want to run a JPA query, it should look something like this:
private static final String MY_REQUEST =
"SELECT us.login, cd.date, us.password " +
"FROM User us JOIN us.commands cd " +
"WHERE us.idUser = 1";
and make sure to use createQuery
:
List<Object[]> = sessionFactory.getCurrentSession().createQuery(MY_REQUEST)
.list();
createSQLQuery
is for when you want to run a native SQL query.
In this case it would look like:
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();
But you should use JPQL queries when possible.
Upvotes: 1