I have a model class that is mapped to a postgres database using hibernate. My model class is:
@Entity
@Table(name="USER")
public class User {
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="username", unique=true)
private String username;
@Column(name="email")
private String email;
@Column(name="created")
private Timestamp created;
public User(long id, String username, String email) {
this.id = id;
this.username = username;
this.email = email;
}
}
I try to retrieve the user with username "adam" using the below query:
tx = session.beginTransaction();
TypedQuery<User> query = session.createQuery("FROM User u WHERE u.username = :username", User.class).setParameter("username", "adam");
user = query.getSingleResult();
I get an exception that says:
org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist
My database from bash shell looks like:
How does hibernate map class attributes to table columns? Does it match based on the @Column(name="username")
only or does it also try to match based on datatypes and constraints such as unique/auto-increment?
myapp
. – Pistole