performing create-or-update with jdbi

喜夏-厌秋 提交于 2019-12-08 00:03:11

问题


For a small new project, I decided to give JDBI a try (normally I work with hibernate/jpa).

I like the lightweight, annotation based dao creation using @SqlUpdate/@SqlQuery.

But: There are situations where I can't be sure if I want to create an entity or update an existing one. I would place a "select" statement and depending on its return value use the insert or update statement.

Question: is this somehow supported by the "interface-only" dao in jdbi? Or do I have to write a "createOrUpdate" method myself (making the auto generated dao more or less obsolete)?

Thanks for any hints.


回答1:


Thanks to @zloster I now built a solution based on an abstract class instead of an interface. Works as required.

@SqlUpdate("insert ...")
public abstract void insert(...);

@SqlUpdate("update...")
public abstract void update();

public X createOrUpdate(final X x) {
    if (!exists(x)) {
        insert(x);
    } else {
        update(x);
    }
    return find(...);
}


来源:https://stackoverflow.com/questions/26584080/performing-create-or-update-with-jdbi

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