Reputation: 470
I've got the following table in a PostgreSQL database:
CREATE TABLE users
(
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR(64) NOT NULL,
age INTEGER NOT NULL
);
CREATE UNIQUE INDEX users_name_uindex ON users (name);
And the following Hibernate entity mapping:
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
private int id;
@Column (name = "name")
private String name;
@Column (name = "age")
private int age;
public User() {}
// setters and getters
}
And a function to get all users from table
@Repository
public class UserDAO {
@PersistenceContext
EntityManager em;
public List<User> getAll() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<User> fromUser = cq.from(User.class);
cq.select(fromUser);
TypedQuery<User> tq = em.createQuery(cq);
return tq.getResultList();
}
}
When I run this function, the following SQL gets run (outputted using show_sql):
select
user0_.id as id1_,
user0_.age as age1_,
user0_.name as name1_
from
User user0_
And the following error occurs:
WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 42703
ERROR: org.hibernate.util.JDBCExceptionReporter - ERROR: column user0_.id not exists
Does anyone know why this is happening?
Upvotes: 0
Views: 4684
Reputation: 12076
Try running the actual query against your database:
select user0_.id as id1_,
user0_.age as age1_,
user0_.name as name1_
from User user0_
It looks to me like your Hibernate query is trying to select from the User
table, when your table is named users
.
You can resolve this by adding the following annotation to your entity:
@Table(name = "USERS")
Upvotes: 1