简体   繁体   中英

Hibernate and Mysql object does not refresh timestamp column

I am currently developing a web-application with spring, hibernate and mysql. The problem was to add a created_at and updated_at timestamp to all the objects.

I have the following abstract @MappedSuperClass:

@MappedSuperclass
public abstract class AbstractTimestampEntity {

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at", nullable = false)
    private Date created;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at", nullable = false, updatable = false)
    private Date updated;
...
}

And in Mysql defined CURRENT_TIMESTAMP for created_at and CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP for updated_at.

When I manually test it, it works fine after inserting an object and updating it the times in mysql are correct. However I wanted to write automatic tests and it does not retrieve the values from the DB and refresh the object.

// before update
sessionFactory.getCurrentSession().refresh(tester1);
Date before = tester1.getUpdated();
// update
userResource.updateUser(new UserDTO(tester1), tester1.getUserId());
// after update
sessionFactory.getCurrentSession().refresh(tester1);
Date after = ((User) sessionFactory.getCurrentSession().get(User.class, tester1.getUserId())).getUpdated();
// verify
assertTrue(before.before(after));

It will result in a nullpointer exception because the date of the tester1 object is never set. (Tester1 Object extends AbstractTimestampEntity)

I manually retrieved the date with a sql-query:

SQLQuery sqlQuery = sessionFactory.getCurrentSession().createSQLQuery("SELECT updated_at FROM smarterTestDB.User WHERE userId="+tester1.getUserId());
sqlQuery.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List list = sqlQuery.list();
String test = list.get(0).toString();

And it gave me the set value from mysql: Date: {updated_at=2017-01-06 12:25:59.0}

Why does it not write it in the object? And it does not give any error about TypeMissmatch or similar. Is the @Temporal(TemporalType.TIMESTAMP) not converting from sql.Timestamp to util.Date?

Kind regards


Update:

I got the error by passing the object to the userResource I construct a the UserDAO object the user would actually send. This one gets updated but not the original User-Object as long as it is not newly retrieved from the database. Simple pass by reference/value error.

Modifications made by the database (like CURRENT_TIMESTAMP or any kind of trigger) aren't recognized by the entity you are trying to save. All modifications have to be made by the java application. Doing so the correct values are in the database and in your entity without doing a "refresh". I've used the following code.

@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@Version
private Date updated_at;

The database column is a simple "TIMESTAMP" without any further functionality.

So the database hasn't any logic, all is done by the application in one place. The application is responsible for all modifications. Maintaining your code will be easier this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM