1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.contrib.hibernate;
17
18 import java.io.File;
19 import java.sql.SQLException;
20
21 import org.hibernate.Session;
22 import org.hibernate.SessionFactory;
23 import org.hibernate.cfg.Configuration;
24 import org.joda.time.Instant;
25
26 public class TestPersistentInstantAsBigInt extends HibernateTestCase
27 {
28 private Instant[] writeReadTimes = new Instant[]
29 {
30 new Instant(0),
31 new Instant(1000),
32 new Instant(1000000)
33 };
34
35 public void testSimpleStore() throws SQLException
36 {
37 SessionFactory factory = getSessionFactory();
38
39 Session session = factory.openSession();
40
41 for (int i = 0; i<writeReadTimes.length; i++)
42 {
43 Instant writeReadTime = writeReadTimes[i];
44
45 ThingWithInstant thing = new ThingWithInstant();
46 thing.setId(i);
47 thing.setInstant(writeReadTime);
48
49 session.save(thing);
50 }
51
52 session.flush();
53 session.connection().commit();
54 session.close();
55
56 for (int i = 0; i<writeReadTimes.length; i++)
57 {
58 Instant writeReadTime = writeReadTimes[i];
59
60 session = factory.openSession();
61 ThingWithInstant thingReread = (ThingWithInstant)session.get(ThingWithInstant.class, new Integer(i));
62
63 assertNotNull("get failed - thing#'" + i + "'not found", thingReread);
64 assertNotNull("get failed - returned null", thingReread.getInstant());
65
66 Instant reReadTime = thingReread.getInstant();
67 if (writeReadTime.getMillis() != reReadTime.getMillis())
68 {
69 fail("get failed - returned different date. expected " + writeReadTime + " was " + thingReread.getInstant());
70 }
71 }
72
73 session.close();
74 }
75
76 protected void setupConfiguration(Configuration cfg)
77 {
78 cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/thingWithInstantAsBigInt.hbm.xml"));
79 }
80 }