java - Hibernate : JDBC Vs Hibernate Performance in bulk records -
description : loadoutoperatingsession
represents particular time period , belongs loadoutid
. there can lot of loadoutoperatingsession
objects same loadoutid
before inserting new session, check had performed overlappings.
below model object have designed.
public class loadoutoperatingsession extends entity implements serializable, comparable<loadoutoperatingsession>{ private long loadoutid; private date effectivefromdate; private date effectivetodate; private string sessionstarttime; private string sessionendtime; private string isschedule; /** * compares given session current 1 , return 1 if session greater given session, * -1 if session less given session , * 0 sessions overlapping. * * @param session1 * first session * * @param session * second session */ @override public int compareto(loadoutoperatingsession session) { if (session.geteffectivetodate().gettime() < this.geteffectivefromdate().gettime()) { return 1; } else if (this.geteffectivetodate().gettime() < session.geteffectivefromdate().gettime()) { return -1; } else if (this.geteffectivefromdate().gettime() == session.geteffectivefromdate().gettime()) { int thisstarttime = integer.parseint(this.getsessionstarttime()); int thisendtime = integer.parseint(this.getsessionendtime()); int sessionstarttime = integer.parseint(session.getsessionstarttime()); int sessionendtime = integer.parseint(session.getsessionendtime()); if (thisendtime < sessionstarttime) { return -1; } else if (thisstarttime > sessionendtime) { return 1; } return 0; } return 0; }
}
assume there lots of loadoutoperatingsession
objects same loadoutid
. in order check overlaps, have fetched loadoutoperatingsession
objects , used compareto
method compare each other.
note : check done before persisting current session.
fetchloadoutoperatingsessionslist method return loadoutoperatingsession
objects given loadoutid
validateforoverlappings(model, fetchloadoutoperatingsessionslist(model)); private <t extends comparable> void validateforoverlappings(t obj, list<t> objlist){ (comparable c : objlist) { if(obj.compareto((t) c) == 0){ throw new illegalargumentexception("overlapping sessions not allowed!"); } } }
question : same validation can done executing jdbc query , taking count of overlapping sessions.
would more efficient above mentioned java solution?
please justify.
using sql statement possible perform check on db level:
select top 1 a.id loadoutoperatingsession a, loadoutoperatingsession b a.from_date <= b.to_date , b.from_date <= a.to_date
why faster:
- it same on application level. sql returns first overlapping record
- but, not returning list of records db, simple integer - no serialization overhead
however think there else wrong if throwing illegalargument on application level after validating records read db. not know use case safer prevent saving such records
edit
my previous answer not correct. equal check done on db level
select top 1 a.id loadoutoperatingsession a.from_date <= :insertedstartdate , a.to_date >= :insertedenddate
Comments
Post a Comment