JOOQ Vs Hibernate behavior

前端 未结 2 1132
猫巷女王i
猫巷女王i 2020-12-22 01:30

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

相关标签:
2条回答
  • 2020-12-22 02:01

    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();    
    }
    
    0 讨论(0)
  • 2020-12-22 02:05

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

    0 讨论(0)
提交回复
热议问题