主键设为自增后,插入新的记录,出现主键重复

public class LugiaPlanPoaBidPrice extends Model<LugiaPlanPoaBidPrice> {

    public static final LugiaPlanPoaBidPrice dao = new LugiaPlanPoaBidPrice();


    public int getId() {

        return getInt("id");

    }


    public int getAppId() {

        return getInt("app_id");

    }


    public int getMarketId() {

        return getInt("market_id");

    }


    public int getPlanId() {

        return getInt("plan_id");

    }


    public Date getDth() {

        return getDate("dth");

    }


    public BigDecimal getPrice() {

        return getBigDecimal("price");

    }


    public static final LugiaPlanPoaBidPrice findFirstPoaBidPrice(int planId, String date) {

        return dao.findFirst("select * from lugia_plan_poa_bid_price where plan_id=? and dth=?", planId, date);

    }


    public void saveOrUpdate() {

        LugiaPlanPoaBidPrice inDb = dao.findFirst("select * from lugia_plan_poa_bid_price where dth=? and plan_id=? and app_id=? and market_id=?",

                get("dth"), get("plan_id"), get("app_id"), get("market_id"));

        if (inDb == null) {

            this.save();//新的记录主键重复

        } else {

            this.set("id", inDb.getId());

            this.update();

        }


    }

}


评论区

feynman61

2018-07-11 18:39

问题解决了,哈哈

112313

2018-07-12 21:02

@feynman61 save之后怎么获取自增的id? 试过User.getInt("id"),获取不到

suxiaoqiuking

2018-07-19 18:58

@112313 save之后直接 通过对象.getId();

热门分享

扫码入社