|
|
@@ -6,12 +6,20 @@
|
|
6
|
6
|
*/
|
|
7
|
7
|
package se.citerus.dddsample.tracking.core.infrastructure.persistence.hibernate;
|
|
8
|
8
|
|
|
|
9
|
+import org.hibernate.HibernateException;
|
|
|
10
|
+import org.hibernate.Session;
|
|
9
|
11
|
import org.hibernate.SessionFactory;
|
|
10
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
13
|
+import org.springframework.orm.hibernate3.HibernateCallback;
|
|
|
14
|
+import org.springframework.orm.hibernate3.HibernateTemplate;
|
|
11
|
15
|
import org.springframework.stereotype.Repository;
|
|
12
|
16
|
import se.citerus.dddsample.tracking.core.domain.model.cargo.TrackingId;
|
|
13
|
17
|
import se.citerus.dddsample.tracking.core.domain.model.cargo.TrackingIdFactory;
|
|
14
|
18
|
|
|
|
19
|
+import javax.annotation.PostConstruct;
|
|
|
20
|
+import java.math.BigInteger;
|
|
|
21
|
+import java.sql.SQLException;
|
|
|
22
|
+
|
|
15
|
23
|
@Repository
|
|
16
|
24
|
public class DatabaseTrackingIdFactory implements TrackingIdFactory {
|
|
17
|
25
|
|
|
|
@@ -23,14 +31,26 @@ public class DatabaseTrackingIdFactory implements TrackingIdFactory {
|
|
23
|
31
|
this.sessionFactory = sessionFactory;
|
|
24
|
32
|
}
|
|
25
|
33
|
|
|
|
34
|
+ @PostConstruct
|
|
|
35
|
+ public void createSequence() {
|
|
|
36
|
+ final HibernateTemplate template = new HibernateTemplate(sessionFactory);
|
|
|
37
|
+ final HibernateCallback callback = new HibernateCallback() {
|
|
|
38
|
+ @Override
|
|
|
39
|
+ public Object doInHibernate(final Session session) throws HibernateException, SQLException {
|
|
|
40
|
+ return session.createSQLQuery("create sequence " + SEQUENCE_NAME + " as bigint start with 1").executeUpdate();
|
|
|
41
|
+ }
|
|
|
42
|
+ };
|
|
|
43
|
+
|
|
|
44
|
+ template.execute(callback);
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
26
|
47
|
@Override
|
|
27
|
48
|
public TrackingId nextTrackingId() {
|
|
28
|
|
- final Long seq = (Long) sessionFactory.getCurrentSession().
|
|
29
|
|
- createSQLQuery("select next_value from system_sequences where sequence_name = ?").
|
|
30
|
|
- setParameter(1, SEQUENCE_NAME).
|
|
|
49
|
+ final BigInteger seq = (BigInteger) sessionFactory.getCurrentSession().
|
|
|
50
|
+ createSQLQuery("call next value for " + SEQUENCE_NAME).
|
|
31
|
51
|
uniqueResult();
|
|
32
|
52
|
|
|
33
|
|
- return new TrackingId(seq);
|
|
|
53
|
+ return new TrackingId(seq.longValue());
|
|
34
|
54
|
}
|
|
35
|
55
|
|
|
36
|
56
|
}
|