Skip to content

Commit 04fa178

Browse files
lor6pivovarit
authored andcommitted
reladomo example (eugenp#2469)
* reladomo example * fix formatting * add plugin versions * fix closing streams
1 parent 6d7fa19 commit 04fa178

16 files changed

Lines changed: 415 additions & 2 deletions

‎libraries/pom.xml‎

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,92 @@
105105
</executions>
106106
</plugin>
107107
<!-- /Neuroph -->
108+
109+
<!-- Reladomo -->
110+
<plugin>
111+
<artifactId>maven-antrun-plugin</artifactId>
112+
<version>1.8</version>
113+
<executions>
114+
<execution>
115+
<id>generateMithra</id>
116+
<phase>generate-sources</phase>
117+
<goals>
118+
<goal>run</goal>
119+
</goals>
120+
<configuration>
121+
<tasks>
122+
<property name="plugin_classpath" refid="maven.plugin.classpath" />
123+
<taskdef name="gen-reladomo" classpath="plugin_classpath"
124+
classname="com.gs.fw.common.mithra.generator.MithraGenerator" />
125+
<gen-reladomo
126+
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
127+
generateGscListMethod="true"
128+
generatedDir="${project.build.directory}/generated-sources/reladomo"
129+
nonGeneratedDir="${project.basedir}/src/main/java" />
130+
131+
<taskdef name="gen-ddl"
132+
classname="com.gs.fw.common.mithra.generator.dbgenerator.MithraDbDefinitionGenerator"
133+
loaderRef="reladomoGenerator">
134+
<classpath refid="maven.plugin.classpath" />
135+
</taskdef>
136+
<gen-ddl
137+
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
138+
generatedDir="${project.build.directory}/generated-db/sql"
139+
databaseType="postgres" />
140+
</tasks>
141+
</configuration>
142+
</execution>
143+
</executions>
144+
<dependencies>
145+
<dependency>
146+
<groupId>com.goldmansachs.reladomo</groupId>
147+
<artifactId>reladomogen</artifactId>
148+
<version>${reladomo.version}</version>
149+
</dependency>
150+
151+
<dependency>
152+
<groupId>com.goldmansachs.reladomo</groupId>
153+
<artifactId>reladomo-gen-util</artifactId>
154+
<version>${reladomo.version}</version>
155+
</dependency>
156+
</dependencies>
157+
</plugin>
158+
159+
<plugin>
160+
<groupId>org.codehaus.mojo</groupId>
161+
<artifactId>build-helper-maven-plugin</artifactId>
162+
<version>3.0.0</version>
163+
<executions>
164+
<execution>
165+
<id>add-source</id>
166+
<phase>generate-sources</phase>
167+
<goals>
168+
<goal>add-source</goal>
169+
</goals>
170+
<configuration>
171+
<sources>
172+
<source>${project.build.directory}/generated-sources/reladomo</source>
173+
</sources>
174+
</configuration>
175+
</execution>
176+
<execution>
177+
<id>add-resource</id>
178+
<phase>generate-resources</phase>
179+
<goals>
180+
<goal>add-resource</goal>
181+
</goals>
182+
<configuration>
183+
<resources>
184+
<resource>
185+
<directory>${project.build.directory}/generated-db/</directory>
186+
</resource>
187+
</resources>
188+
</configuration>
189+
</execution>
190+
</executions>
191+
</plugin>
192+
<!-- /Reladomo-->
193+
108194
</plugins>
109195
</build>
110196
<dependencies>
@@ -497,6 +583,16 @@
497583
<artifactId>gt-swing</artifactId>
498584
<version>${geotools.version}</version>
499585
</dependency>
586+
<dependency>
587+
<groupId>com.goldmansachs.reladomo</groupId>
588+
<artifactId>reladomo</artifactId>
589+
<version>${reladomo.version}</version>
590+
</dependency>
591+
<dependency>
592+
<groupId>com.goldmansachs.reladomo</groupId>
593+
<artifactId>reladomo-test-util</artifactId>
594+
<version>${reladomo.version}</version>
595+
</dependency>
500596
</dependencies>
501597
<repositories>
502598
<repository>
@@ -563,6 +659,6 @@
563659
<streamex.version>0.6.5</streamex.version>
564660
<vavr.version>0.9.0</vavr.version>
565661
<geotools.version>15.2</geotools.version>
662+
<reladomo.version>16.5.1</reladomo.version>
566663
</properties>
567-
</project>
568-
664+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.reladomo;
2+
3+
public class Department extends DepartmentAbstract {
4+
public Department() {
5+
super();
6+
// You must not modify this constructor. Mithra calls this internally.
7+
// You can call this constructor. You can also add new constructors.
8+
}
9+
10+
public Department(long id, String name) {
11+
super();
12+
this.setId(id);
13+
this.setName(name);
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.reladomo;
2+
public class DepartmentDatabaseObject extends DepartmentDatabaseObjectAbstract
3+
{
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.reladomo;
2+
import com.gs.fw.finder.Operation;
3+
import java.util.*;
4+
public class DepartmentList extends DepartmentListAbstract
5+
{
6+
public DepartmentList()
7+
{
8+
super();
9+
}
10+
11+
public DepartmentList(int initialSize)
12+
{
13+
super(initialSize);
14+
}
15+
16+
public DepartmentList(Collection c)
17+
{
18+
super(c);
19+
}
20+
21+
public DepartmentList(Operation operation)
22+
{
23+
super(operation);
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.reladomo;
2+
public class Employee extends EmployeeAbstract
3+
{
4+
public Employee()
5+
{
6+
super();
7+
// You must not modify this constructor. Mithra calls this internally.
8+
// You can call this constructor. You can also add new constructors.
9+
}
10+
11+
public Employee(long id, String name){
12+
super();
13+
this.setId(id);
14+
this.setName(name);
15+
}
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.reladomo;
2+
public class EmployeeDatabaseObject extends EmployeeDatabaseObjectAbstract
3+
{
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.reladomo;
2+
import com.gs.fw.finder.Operation;
3+
import java.util.*;
4+
public class EmployeeList extends EmployeeListAbstract
5+
{
6+
public EmployeeList()
7+
{
8+
super();
9+
}
10+
11+
public EmployeeList(int initialSize)
12+
{
13+
super(initialSize);
14+
}
15+
16+
public EmployeeList(Collection c)
17+
{
18+
super(c);
19+
}
20+
21+
public EmployeeList(Operation operation)
22+
{
23+
super(operation);
24+
}
25+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.reladomo;
2+
3+
import java.io.InputStream;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.gs.fw.common.mithra.MithraManager;
9+
import com.gs.fw.common.mithra.MithraManagerProvider;
10+
11+
public class ReladomoApplication {
12+
13+
public static void main(String[] args) {
14+
15+
try {
16+
ReladomoConnectionManager.getInstance().createTables();
17+
} catch (Exception e1) {
18+
e1.printStackTrace();
19+
}
20+
21+
MithraManager mithraManager = MithraManagerProvider.getMithraManager();
22+
mithraManager.setTransactionTimeout(120);
23+
24+
try (InputStream is = ReladomoApplication.class.getClassLoader().getResourceAsStream("ReladomoRuntimeConfig.xml")) {
25+
MithraManagerProvider.getMithraManager().readConfiguration(is);
26+
27+
Department department = new Department(1, "IT");
28+
Employee employee = new Employee(1, "John");
29+
department.getEmployees().add(employee);
30+
department.cascadeInsert();
31+
32+
Department depFound = DepartmentFinder.findByPrimaryKey(1);
33+
System.out.println("Department Name:" + department.getName());
34+
35+
Employee empFound = EmployeeFinder.findOne(EmployeeFinder.name().eq("John"));
36+
System.out.println("Employee Id:" + empFound.getId());
37+
empFound.setName("Steven");
38+
empFound.delete();
39+
Department depDetached = DepartmentFinder.findByPrimaryKey(1).getDetachedCopy();
40+
41+
mithraManager.executeTransactionalCommand(tx -> {
42+
Department dep = new Department(2, "HR");
43+
Employee emp = new Employee(2, "Jim");
44+
dep.getEmployees().add(emp);
45+
dep.cascadeInsert();
46+
return null;
47+
});
48+
49+
} catch (java.io.IOException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
54+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.reladomo;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
import java.util.TimeZone;
10+
import java.util.stream.Stream;
11+
12+
import org.h2.tools.RunScript;
13+
14+
import com.gs.fw.common.mithra.bulkloader.BulkLoader;
15+
import com.gs.fw.common.mithra.bulkloader.BulkLoaderException;
16+
import com.gs.fw.common.mithra.connectionmanager.SourcelessConnectionManager;
17+
import com.gs.fw.common.mithra.connectionmanager.XAConnectionManager;
18+
import com.gs.fw.common.mithra.databasetype.DatabaseType;
19+
import com.gs.fw.common.mithra.databasetype.H2DatabaseType;
20+
21+
public class ReladomoConnectionManager implements SourcelessConnectionManager {
22+
23+
private static ReladomoConnectionManager instance;
24+
25+
private XAConnectionManager xaConnectionManager;
26+
27+
private final String databaseName = "myDb";
28+
29+
public static synchronized ReladomoConnectionManager getInstance() {
30+
if (instance == null) {
31+
instance = new ReladomoConnectionManager();
32+
}
33+
return instance;
34+
}
35+
36+
private ReladomoConnectionManager() {
37+
this.createConnectionManager();
38+
}
39+
40+
private XAConnectionManager createConnectionManager() {
41+
xaConnectionManager = new XAConnectionManager();
42+
xaConnectionManager.setDriverClassName("org.h2.Driver");
43+
xaConnectionManager.setJdbcConnectionString("jdbc:h2:mem:" + databaseName);
44+
xaConnectionManager.setJdbcUser("sa");
45+
xaConnectionManager.setJdbcPassword("");
46+
xaConnectionManager.setPoolName("My Connection Pool");
47+
xaConnectionManager.setInitialSize(1);
48+
xaConnectionManager.setPoolSize(10);
49+
xaConnectionManager.initialisePool();
50+
return xaConnectionManager;
51+
}
52+
53+
@Override
54+
public BulkLoader createBulkLoader() throws BulkLoaderException {
55+
return null;
56+
}
57+
58+
@Override
59+
public Connection getConnection() {
60+
return xaConnectionManager.getConnection();
61+
}
62+
63+
@Override
64+
public DatabaseType getDatabaseType() {
65+
return H2DatabaseType.getInstance();
66+
}
67+
68+
@Override
69+
public TimeZone getDatabaseTimeZone() {
70+
return TimeZone.getDefault();
71+
}
72+
73+
@Override
74+
public String getDatabaseIdentifier() {
75+
return databaseName;
76+
}
77+
78+
public void createTables() throws Exception {
79+
Path ddlPath = Paths.get(ClassLoader.getSystemResource("sql").toURI());
80+
81+
try (Connection conn = xaConnectionManager.getConnection(); Stream<Path> list = Files.list(ddlPath);) {
82+
list.forEach(path -> {
83+
try {
84+
RunScript.execute(conn, Files.newBufferedReader(path));
85+
} catch (SQLException | IOException exc) {
86+
exc.printStackTrace();
87+
}
88+
});
89+
}
90+
}
91+
92+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<MithraRuntime>
2+
<ConnectionManager className="com.baeldung.reladomo.ReladomoConnectionManager ">
3+
<MithraObjectConfiguration className="com.baeldung.reladomo.Department" cacheType="partial"/>
4+
<MithraObjectConfiguration className="com.baeldung.reladomo.Employee " cacheType="partial"/>
5+
</ConnectionManager>
6+
</MithraRuntime>

0 commit comments

Comments
 (0)