1

When I'm trying to update User object in my test I'm getting following output:

2018-paź-26 22:18:24 PM [main] DEBUG org.hibernate.SQL - update "user" set "date_of_birth"=?, "email"=?, "login"=?, "password"=?, "user_rating"=? where    "user_id"=?
2018-paź-26 22:18:24 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [DATE] - [2018-10-26]
2018-paź-26 22:18:24 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as     [VARCHAR] - [someEmail]
2018-paź-26 22:18:24 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as[VARCHAR] - [updatedLogin]
2018-paź-26 22:18:24 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [4] as     [VARCHAR] - [somePassword]
2018-paź-26 22:18:24 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [5] as [FLOAT] - [0.0]
2018-paź-26 22:18:24 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [6] as [BIGINT] - [5]
2018-paź-26 22:18:24 PM [main] DEBUG org.hibernate.SQL - update "user" set date_of_birth=?, "email"=?, "login"=?, "password"=?, user_rating=? where "user_id"=?
2018-paź-26 22:18:24 PM [main] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 42122, SQLState: 42S22
2018-paź-26 22:18:24 PM [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Kolumna "DATE_OF_BIRTH" nie istnieje
Column "DATE_OF_BIRTH" not found; SQL statement:
update "user" set date_of_birth=?, "email"=?, "login"=?, "password"=?, user_rating=? where "user_id"=? [42122-197]

User.class

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private long id;

@Column(name = "email")
private String email;

@Column(name = "login")
private String login;

@Column(name = "password")
private String password;

@Column(name = "date_of_birth")
private LocalDate dateOfBirth;

@Column(name = "user_rating")
private float userRating;
//Getters and setters ommited for clarity

And update method in dao implementation:

@Override
public void update(User user) {
    log.info("Update user with id= " + user.getId() + ", set email= " + user.getEmail() 
        + ", login = " + user.getLogin() + ", dateOfBirth= " + user.getDateOfBirth());
    Query query = getSession().createQuery("update User"
            + " set date_of_birth = :dateOfBirth,"
            + " email = :email,"
            + " login = :login,"
            + " password = :password,"
            + " user_rating = :userRating where id = :id");
    query.setParameter("email", user.getEmail());
    query.setParameter("login", user.getLogin());
    query.setParameter("dateOfBirth", user.getDateOfBirth());
    query.setParameter("password", user.getPassword());
    query.setParameter("userRating", user.getUserRating());
    query.setParameter("id", user.getId());
    int result = query.executeUpdate();
    log.info("Update result: " + result);
}

For other test where I'm getting user from database, or saving there are no problems with column "date_of_birth".

P.S. In stack track I found that the sql date_of_birth column isn't in quotes when in other tests, quotes always appear, it's possible that the problem is here ?

org.hibernate.SQL - update "user" set date_of_birth=?, "email"=?, "login"=?, "password"=?, user_rating=? where "user_id"=?
karol512
  • 23
  • 8
  • Did u check if u have the "date_of_birth" column in the data base!? – Rostan Oct 26 '18 at 22:49
  • I think the issue here is that you are confusing the physical column name with the 'mapped' column name. The actual database column name appears to be date_of_birth, the the mapped name appears to be dateOfBirth. You must only use the mapped names in your JPQL. – Tom Drake Oct 26 '18 at 23:04
  • @Rostan Yes, I can get user object from database and obtain his date, – karol512 Oct 26 '18 at 23:10
  • @TomDrake Isn't it that dateOfBirth is just name of parameter of query ? (replacing dateOfBirth for date_of_birth in update method didn't change anything) – karol512 Oct 26 '18 at 23:15
  • so, the problem just appears when u try u update the user data? – Rostan Oct 26 '18 at 23:20
  • @Rostan Exactly, other test works fine, that's why I think problem is in update method – karol512 Oct 26 '18 at 23:28
  • Sounds like the issue is what Tom said above. Try removing the date_of_birth parameter from your JPQL query to see if the issue goes away or just moves to another column. – cleberz Oct 26 '18 at 23:43
  • @ckeberz As I mentioned above I tried this and result was this same. – karol512 Oct 26 '18 at 23:47
  • I just noticed that you have defined dateOfBirth as a LocalDate. Try, redefining this as a java.sql.Date . The interesting thing about LocalDate is that it is bound to a timezone. If you really need to use LocalDate, you will probably have to [define your own mapping] (https://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/) – Tom Drake Oct 28 '18 at 01:39
  • (This stackoverflow thread) [https://stackoverflow.com/questions/23718383/jpa-support-for-java-8-new-date-and-time-api] provides some more information on this and explains that JPA 2.2 is required in order to get JPA support for the Java 8 date/time apis. – Tom Drake Oct 28 '18 at 01:57
  • @TomDrake, but didn't hibernate 5 provide support for localdate? – karol512 Oct 30 '18 at 13:29
  • @karol512, It's not clear what version of hibernate is in use here. It's worth a try since this is a known issue with JPA. – Tom Drake Oct 30 '18 at 20:23

0 Answers0