简体   繁体   中英

How can I pass optional params to procedure?

I have 2 mrthods save() and edit() entity. When I save entity I check date overlap and if date period unique - save. For that I use oracle procedure. If procedure return >0 rows - this period not unique. When I edit entity - it already in db and I need optional check by id.

CallableStatement call = connection.prepareCall("{ ? = call checkDateRangesOverlap(?,?,?,?)}");
call.registerOutParameter(1, Types.INTEGER); // or whatever it is
Date dateStart = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
Date dateEnd = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
call.setDate(2, new java.sql.Date(dateStart.getTime()));
call.setDate(3, new java.sql.Date(dateEnd.getTime()));
call.setLong(4, user.getId());
call.setLong(5, id);
call.execute();

but if I create new entity - id is null and call.setLong(5, id); throw error.

How can I pass optional params to procedure?

If your id can be null , so instead of using call.setLong(5, null) you can use setNull() like this :

if (id == null) {
    call.setNull(5, java.sql.Types.BIGINT);
} else {
    call.setLong(5, id);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM