Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

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();    
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top