1- package tpi .dgrv4 .entity .repository ;
2-
3- import java .util .HashMap ;
4- import java .util .List ;
5- import java .util .Map ;
6-
7- import jakarta .persistence .EntityManager ;
8- import jakarta .persistence .PersistenceContext ;
9- import jakarta .transaction .Transactional ;
10-
11- import tpi .dgrv4 .common .utils .StackTraceUtil ;
12- import tpi .dgrv4 .entity .entity .SeqStore ;
13-
14- public class SeqStoreDaoImpl extends BaseDao {
15- // add custom methods here
16-
17- @ PersistenceContext
18- private EntityManager em ;
19-
20- @ Transactional
21- public Long nextSequence (String sequenceName , Long initial , Long increment ) {
22- Map <String , Object > params = new HashMap <>();
23- StringBuffer sb = new StringBuffer ();
24- sb .append ("select s.* from seq_store s where s.sequence_name = :sequenceName" );
25- params .put ("sequenceName" , sequenceName );
26- final List <SeqStore > seqList = doNativeQuery (sb .toString (), null , params , null , SeqStore .class );
27- params .clear ();
28- sb .setLength (0 );
29-
30- Long nextVal = null ;
31- if (seqList == null || seqList .isEmpty ()) {
32- nextVal = initial ;
33- sb .append ("insert into seq_store (sequence_name, next_val) values (:sequenceName, :nextVal)" );
34- params .put ("sequenceName" , sequenceName );
35- params .put ("nextVal" , (initial + increment ));
36- } else {
37- SeqStore seq = seqList .get (0 );
38- final Long oldVal = seq .getNextVal ();
39-
40- // 否則 hibernate first-level cache 會造成無法更新值
41- em .detach (seq );
42-
43- nextVal = oldVal ;
44- sb .append ("update seq_store set next_val = :nextVal where sequence_name = :sequenceName and next_val = :oldVal" );
45- params .put ("nextVal" , (oldVal + increment ));
46- params .put ("sequenceName" , sequenceName );
47- params .put ("oldVal" , oldVal );
48- }
49-
50- int cnt = 0 ;
51- try {
52- cnt = doNativeUpdate (sb .toString (), params );
53- if (cnt > 0 ) {
54- return nextVal ;
55- }
56- } catch (Exception e ) {
57- logger .debug (StackTraceUtil .logStackTrace (e ));
58- }
59- return null ;
60- }
61-
62- public List <SeqStore > queryExpiredSequence (String reqParam , String today_yyyyMMdd ) {
63- Map <String , Object > params = new HashMap <>();
64- StringBuffer sb = new StringBuffer ();
65- sb .append (" select S" );
66- sb .append (" from SeqStore S" );
67- sb .append (" where S.sequenceName like :reqParam" );
68- sb .append (" and S.sequenceName <> :todaySequence" );
69- sb .append (" order by S.sequenceName asc" );
70- params .put ("reqParam" , reqParam + "-%" );
71- params .put ("todaySequence" , reqParam + "-" + today_yyyyMMdd );
72-
73- return doQuery (sb .toString (), params , SeqStore .class );
74- }
75-
1+ package tpi .dgrv4 .entity .repository ;
2+
3+ import java .util .HashMap ;
4+ import java .util .List ;
5+ import java .util .Map ;
6+
7+ import jakarta .persistence .EntityManager ;
8+ import jakarta .persistence .PersistenceContext ;
9+ import jakarta .transaction .Transactional ;
10+
11+ import tpi .dgrv4 .common .utils .StackTraceUtil ;
12+ import tpi .dgrv4 .entity .entity .SeqStore ;
13+
14+ public class SeqStoreDaoImpl extends BaseDao {
15+ // add custom methods here
16+
17+ @ PersistenceContext
18+ private EntityManager em ;
19+
20+ @ Transactional
21+ public Long nextSequence (String sequenceName , Long initial , Long increment ) {
22+ Map <String , Object > params = new HashMap <>();
23+ StringBuffer sb = new StringBuffer ();
24+ sb .append ("select s.* from seq_store s where s.sequence_name = :sequenceName" );
25+ params .put ("sequenceName" , sequenceName );
26+ final List <SeqStore > seqList = doNativeQuery (sb .toString (), null , params , null , SeqStore .class );
27+ params .clear ();
28+ sb .setLength (0 );
29+
30+ Long nextVal = null ;
31+ if (seqList == null || seqList .isEmpty ()) {
32+ nextVal = initial ;
33+ sb .append ("insert into seq_store (sequence_name, next_val) values (:sequenceName, :nextVal)" );
34+ params .put ("sequenceName" , sequenceName );
35+ params .put ("nextVal" , (initial + increment ));
36+ } else {
37+ SeqStore seq = seqList .get (0 );
38+ final Long oldVal = seq .getNextVal ();
39+
40+ // 否則 hibernate first-level cache 會造成無法更新值
41+ em .detach (seq );
42+
43+ nextVal = oldVal ;
44+ sb .append ("update seq_store set next_val = :nextVal where sequence_name = :sequenceName and next_val = :oldVal" );
45+ params .put ("nextVal" , (oldVal + increment ));
46+ params .put ("sequenceName" , sequenceName );
47+ params .put ("oldVal" , oldVal );
48+ }
49+
50+ int cnt = 0 ;
51+ try {
52+ cnt = doNativeUpdate (sb .toString (), params );
53+ if (cnt > 0 ) {
54+ return nextVal ;
55+ }
56+ } catch (Exception e ) {
57+ logger .debug (StackTraceUtil .logStackTrace (e ));
58+ }
59+ return null ;
60+ }
61+
62+ public List <SeqStore > queryExpiredSequence (String reqParam , String today_yyyyMMdd ) {
63+ Map <String , Object > params = new HashMap <>();
64+ StringBuffer sb = new StringBuffer ();
65+ sb .append (" select S" );
66+ sb .append (" from SeqStore S" );
67+ sb .append (" where S.sequenceName like :reqParam" );
68+ sb .append (" and S.sequenceName <> :todaySequence" );
69+ sb .append (" order by S.sequenceName asc" );
70+ params .put ("reqParam" , reqParam + "-%" );
71+ params .put ("todaySequence" , reqParam + "-" + today_yyyyMMdd );
72+
73+ return doQuery (sb .toString (), params , SeqStore .class );
74+ }
75+
7676}
0 commit comments