JOOQ Vs Hibernate behavior

房东的猫 提交于 2019-11-28 10:51:22

问题


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?


回答1:


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).




回答2:


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();    
}


来源:https://stackoverflow.com/questions/19998470/jooq-vs-hibernate-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!