JOOQ Vs Hibernate behavior

2019-03-02 05:00发布

As we know Hibernate have a a very good feature SaveOrUpdate when we pass any object to this method it know data would be update or new record will be added in database. Is this feature also available in JOOQ Or in my code i have to handle this?

2条回答
虎瘦雄心在
2楼-- · 2019-03-02 05:32

jOOQ does the same. If you change the primary key of a record, then it will use INSERT, otherwise, it will use UPDATE.

As it is, when you read a record from the database, then calling store() will trigger an UPDATE as you'd expect. If you create a new record, then it will be INSERTed.

With 2.6, it's a bit hard to clone a record and then ask jOOQ to update it (since cloning will set the primary key in a new instance, hence marking it as "new" -> insert).

查看更多
小情绪 Triste *
3楼-- · 2019-03-02 05:45

if you read a record from database and call record.store() you will have the same behavior of hibernate saveOrUpdate method, it's works perfectly!

But in the most of cases you will not read the record from the database, you will receive a record from a controller or a view for example, in this case the method record.store() does not update, it always insert even you have the id setted.

For now I am implementing my own saveOrUpdate, checking by the record id.

public int saveOrUpdate(Record record) {
    if(record.getId() != null) {
        return record.update();
    } 
    return record.store();    
}
查看更多
登录 后发表回答