annotate flys-backend/src/main/java/de/intevation/flys/App.java @ 183:222f4db3430a

Importer: Used thread local pattern to make sharing of session easier. flys-backend/trunk@1494 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 17 Mar 2011 10:47:31 +0000
parents 249390dd24e7
children bc3747a371cc
rev   line source
165
a69b8494fcac Created a new empty maven project
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys;
a69b8494fcac Created a new empty maven project
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
3 import org.hibernate.cfg.Configuration;
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
4
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
5 import org.hibernate.SessionFactory;
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
6
174
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
7 import de.intevation.flys.model.Annotation;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
8 import de.intevation.flys.model.Attribute;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
9 import de.intevation.flys.model.DischargeTable;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
10 import de.intevation.flys.model.DischargeTableValue;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
11 import de.intevation.flys.model.Gauge;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
12 import de.intevation.flys.model.MainValueType;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
13 import de.intevation.flys.model.NamedMainValues;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
14 import de.intevation.flys.model.Position;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
15 import de.intevation.flys.model.Range;
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
16 import de.intevation.flys.model.River;
174
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
17 import de.intevation.flys.model.TimeInterval;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
18 import de.intevation.flys.model.WstColumn;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
19 import de.intevation.flys.model.WstColumnQRange;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
20 import de.intevation.flys.model.WstColumnValue;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
21 import de.intevation.flys.model.Wst;
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
22 import de.intevation.flys.model.WstQRange;
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
23
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
24 import org.hibernate.dialect.resolver.DialectFactory;
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
25
169
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
26 import java.util.Properties;
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
27
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
28 import org.hibernate.cfg.Environment;
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
29
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
30 import org.hibernate.Session;
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
31
165
a69b8494fcac Created a new empty maven project
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 public class App
a69b8494fcac Created a new empty maven project
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 {
169
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
34 private static final String USER =
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
35 System.getProperty("flys.user", "flys");
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
36
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
37 private static final String PASS =
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
38 System.getProperty("flys.pass", "flys");
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
39
174
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
40 public static void dumpSchema(Configuration cfg) {
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
41 System.out.println("BEGIN;");
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
42
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
43 String [] setupScript = cfg.generateSchemaCreationScript(
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
44 DialectFactory.constructDialect(
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
45 "org.hibernate.dialect.PostgreSQLDialect"));
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
46
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
47 for (String line: setupScript) {
174
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
48 System.out.println(line + ";");
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
49 }
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
50
174
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
51 System.out.println("COMMIT;");
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
52 }
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
53
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
54 public static void main(String [] args)
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
55 throws Exception
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
56 {
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
57 Configuration cfg = new Configuration();
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
58
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
59 cfg.addAnnotatedClass(Annotation.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
60 cfg.addAnnotatedClass(Attribute.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
61 cfg.addAnnotatedClass(DischargeTable.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
62 cfg.addAnnotatedClass(DischargeTableValue.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
63 cfg.addAnnotatedClass(Gauge.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
64 cfg.addAnnotatedClass(MainValueType.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
65 cfg.addAnnotatedClass(NamedMainValues.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
66 cfg.addAnnotatedClass(Position.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
67 cfg.addAnnotatedClass(Range.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
68 cfg.addAnnotatedClass(River.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
69 cfg.addAnnotatedClass(TimeInterval.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
70 cfg.addAnnotatedClass(WstColumn.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
71 cfg.addAnnotatedClass(WstColumnQRange.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
72 cfg.addAnnotatedClass(WstColumnValue.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
73 cfg.addAnnotatedClass(Wst.class);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
74 cfg.addAnnotatedClass(WstQRange.class);
169
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
75 Properties props = new Properties();
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
76
174
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
77 dumpSchema(cfg);
249390dd24e7 Added foreign key constraint annotations to model classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 169
diff changeset
78
169
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
79 props.setProperty(
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
80 Environment.DIALECT,
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
81 "org.hibernate.dialect.PostgreSQLDialect");
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
82
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
83 props.setProperty(
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
84 "hibernate.connection.provider_class",
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
85 "org.hibernate.connection.DBCPConnectionProvider");
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
86
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
87 props.setProperty(
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
88 Environment.USER,
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
89 USER);
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
90
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
91 props.setProperty(
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
92 Environment.PASS,
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
93 PASS);
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
94
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
95 props.setProperty(
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
96 Environment.DRIVER,
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
97 "org.postgresql.Driver");
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
98
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
99 props.setProperty(
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
100 Environment.URL,
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
101 "jdbc:postgresql://localhost:54321/flystest1");
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
102
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
103 cfg.mergeProperties(props);
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
104
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
105 SessionFactory sessionFactory = cfg.buildSessionFactory();
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
106
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
107 Session session = sessionFactory.openSession();
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
108 session.beginTransaction();
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
109
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
110 River river = new River("Hase-" + new java.util.Date());
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
111
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
112 session.save(river);
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
113
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
114 System.out.println("river id: " + river.getId());
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
115
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
116 session.getTransaction().commit();
7929f4144d2f Bound Apache Commons DBCP with Hibernate.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 168
diff changeset
117 session.close();
165
a69b8494fcac Created a new empty maven project
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 }
a69b8494fcac Created a new empty maven project
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 }
168
86a1bd9cc50e More Hibernate/JPA stuff
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 165
diff changeset
120 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org