reladomo example (#2469)
* reladomo example * fix formatting * add plugin versions * fix closing streams
This commit is contained in:
parent
6d7fa197dd
commit
04fa1782a2
@ -105,6 +105,92 @@
|
|||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!-- /Neuroph -->
|
<!-- /Neuroph -->
|
||||||
|
|
||||||
|
<!-- Reladomo -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-antrun-plugin</artifactId>
|
||||||
|
<version>1.8</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>generateMithra</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>run</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<tasks>
|
||||||
|
<property name="plugin_classpath" refid="maven.plugin.classpath" />
|
||||||
|
<taskdef name="gen-reladomo" classpath="plugin_classpath"
|
||||||
|
classname="com.gs.fw.common.mithra.generator.MithraGenerator" />
|
||||||
|
<gen-reladomo
|
||||||
|
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
|
||||||
|
generateGscListMethod="true"
|
||||||
|
generatedDir="${project.build.directory}/generated-sources/reladomo"
|
||||||
|
nonGeneratedDir="${project.basedir}/src/main/java" />
|
||||||
|
|
||||||
|
<taskdef name="gen-ddl"
|
||||||
|
classname="com.gs.fw.common.mithra.generator.dbgenerator.MithraDbDefinitionGenerator"
|
||||||
|
loaderRef="reladomoGenerator">
|
||||||
|
<classpath refid="maven.plugin.classpath" />
|
||||||
|
</taskdef>
|
||||||
|
<gen-ddl
|
||||||
|
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
|
||||||
|
generatedDir="${project.build.directory}/generated-db/sql"
|
||||||
|
databaseType="postgres" />
|
||||||
|
</tasks>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.goldmansachs.reladomo</groupId>
|
||||||
|
<artifactId>reladomogen</artifactId>
|
||||||
|
<version>${reladomo.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.goldmansachs.reladomo</groupId>
|
||||||
|
<artifactId>reladomo-gen-util</artifactId>
|
||||||
|
<version>${reladomo.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>build-helper-maven-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>add-source</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>add-source</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<sources>
|
||||||
|
<source>${project.build.directory}/generated-sources/reladomo</source>
|
||||||
|
</sources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>add-resource</id>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>add-resource</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>${project.build.directory}/generated-db/</directory>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<!-- /Reladomo-->
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -497,6 +583,16 @@
|
|||||||
<artifactId>gt-swing</artifactId>
|
<artifactId>gt-swing</artifactId>
|
||||||
<version>${geotools.version}</version>
|
<version>${geotools.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.goldmansachs.reladomo</groupId>
|
||||||
|
<artifactId>reladomo</artifactId>
|
||||||
|
<version>${reladomo.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.goldmansachs.reladomo</groupId>
|
||||||
|
<artifactId>reladomo-test-util</artifactId>
|
||||||
|
<version>${reladomo.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
@ -563,6 +659,6 @@
|
|||||||
<streamex.version>0.6.5</streamex.version>
|
<streamex.version>0.6.5</streamex.version>
|
||||||
<vavr.version>0.9.0</vavr.version>
|
<vavr.version>0.9.0</vavr.version>
|
||||||
<geotools.version>15.2</geotools.version>
|
<geotools.version>15.2</geotools.version>
|
||||||
|
<reladomo.version>16.5.1</reladomo.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
|
||||||
|
public class Department extends DepartmentAbstract {
|
||||||
|
public Department() {
|
||||||
|
super();
|
||||||
|
// You must not modify this constructor. Mithra calls this internally.
|
||||||
|
// You can call this constructor. You can also add new constructors.
|
||||||
|
}
|
||||||
|
|
||||||
|
public Department(long id, String name) {
|
||||||
|
super();
|
||||||
|
this.setId(id);
|
||||||
|
this.setName(name);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
public class DepartmentDatabaseObject extends DepartmentDatabaseObjectAbstract
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
import com.gs.fw.finder.Operation;
|
||||||
|
import java.util.*;
|
||||||
|
public class DepartmentList extends DepartmentListAbstract
|
||||||
|
{
|
||||||
|
public DepartmentList()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DepartmentList(int initialSize)
|
||||||
|
{
|
||||||
|
super(initialSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DepartmentList(Collection c)
|
||||||
|
{
|
||||||
|
super(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DepartmentList(Operation operation)
|
||||||
|
{
|
||||||
|
super(operation);
|
||||||
|
}
|
||||||
|
}
|
16
libraries/src/main/java/com/baeldung/reladomo/Employee.java
Normal file
16
libraries/src/main/java/com/baeldung/reladomo/Employee.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
public class Employee extends EmployeeAbstract
|
||||||
|
{
|
||||||
|
public Employee()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
// You must not modify this constructor. Mithra calls this internally.
|
||||||
|
// You can call this constructor. You can also add new constructors.
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(long id, String name){
|
||||||
|
super();
|
||||||
|
this.setId(id);
|
||||||
|
this.setName(name);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
public class EmployeeDatabaseObject extends EmployeeDatabaseObjectAbstract
|
||||||
|
{
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
import com.gs.fw.finder.Operation;
|
||||||
|
import java.util.*;
|
||||||
|
public class EmployeeList extends EmployeeListAbstract
|
||||||
|
{
|
||||||
|
public EmployeeList()
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeList(int initialSize)
|
||||||
|
{
|
||||||
|
super(initialSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeList(Collection c)
|
||||||
|
{
|
||||||
|
super(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeList(Operation operation)
|
||||||
|
{
|
||||||
|
super(operation);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.gs.fw.common.mithra.MithraManager;
|
||||||
|
import com.gs.fw.common.mithra.MithraManagerProvider;
|
||||||
|
|
||||||
|
public class ReladomoApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
ReladomoConnectionManager.getInstance().createTables();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
MithraManager mithraManager = MithraManagerProvider.getMithraManager();
|
||||||
|
mithraManager.setTransactionTimeout(120);
|
||||||
|
|
||||||
|
try (InputStream is = ReladomoApplication.class.getClassLoader().getResourceAsStream("ReladomoRuntimeConfig.xml")) {
|
||||||
|
MithraManagerProvider.getMithraManager().readConfiguration(is);
|
||||||
|
|
||||||
|
Department department = new Department(1, "IT");
|
||||||
|
Employee employee = new Employee(1, "John");
|
||||||
|
department.getEmployees().add(employee);
|
||||||
|
department.cascadeInsert();
|
||||||
|
|
||||||
|
Department depFound = DepartmentFinder.findByPrimaryKey(1);
|
||||||
|
System.out.println("Department Name:" + department.getName());
|
||||||
|
|
||||||
|
Employee empFound = EmployeeFinder.findOne(EmployeeFinder.name().eq("John"));
|
||||||
|
System.out.println("Employee Id:" + empFound.getId());
|
||||||
|
empFound.setName("Steven");
|
||||||
|
empFound.delete();
|
||||||
|
Department depDetached = DepartmentFinder.findByPrimaryKey(1).getDetachedCopy();
|
||||||
|
|
||||||
|
mithraManager.executeTransactionalCommand(tx -> {
|
||||||
|
Department dep = new Department(2, "HR");
|
||||||
|
Employee emp = new Employee(2, "Jim");
|
||||||
|
dep.getEmployees().add(emp);
|
||||||
|
dep.cascadeInsert();
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (java.io.IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.h2.tools.RunScript;
|
||||||
|
|
||||||
|
import com.gs.fw.common.mithra.bulkloader.BulkLoader;
|
||||||
|
import com.gs.fw.common.mithra.bulkloader.BulkLoaderException;
|
||||||
|
import com.gs.fw.common.mithra.connectionmanager.SourcelessConnectionManager;
|
||||||
|
import com.gs.fw.common.mithra.connectionmanager.XAConnectionManager;
|
||||||
|
import com.gs.fw.common.mithra.databasetype.DatabaseType;
|
||||||
|
import com.gs.fw.common.mithra.databasetype.H2DatabaseType;
|
||||||
|
|
||||||
|
public class ReladomoConnectionManager implements SourcelessConnectionManager {
|
||||||
|
|
||||||
|
private static ReladomoConnectionManager instance;
|
||||||
|
|
||||||
|
private XAConnectionManager xaConnectionManager;
|
||||||
|
|
||||||
|
private final String databaseName = "myDb";
|
||||||
|
|
||||||
|
public static synchronized ReladomoConnectionManager getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new ReladomoConnectionManager();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ReladomoConnectionManager() {
|
||||||
|
this.createConnectionManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
private XAConnectionManager createConnectionManager() {
|
||||||
|
xaConnectionManager = new XAConnectionManager();
|
||||||
|
xaConnectionManager.setDriverClassName("org.h2.Driver");
|
||||||
|
xaConnectionManager.setJdbcConnectionString("jdbc:h2:mem:" + databaseName);
|
||||||
|
xaConnectionManager.setJdbcUser("sa");
|
||||||
|
xaConnectionManager.setJdbcPassword("");
|
||||||
|
xaConnectionManager.setPoolName("My Connection Pool");
|
||||||
|
xaConnectionManager.setInitialSize(1);
|
||||||
|
xaConnectionManager.setPoolSize(10);
|
||||||
|
xaConnectionManager.initialisePool();
|
||||||
|
return xaConnectionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BulkLoader createBulkLoader() throws BulkLoaderException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() {
|
||||||
|
return xaConnectionManager.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseType getDatabaseType() {
|
||||||
|
return H2DatabaseType.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TimeZone getDatabaseTimeZone() {
|
||||||
|
return TimeZone.getDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDatabaseIdentifier() {
|
||||||
|
return databaseName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createTables() throws Exception {
|
||||||
|
Path ddlPath = Paths.get(ClassLoader.getSystemResource("sql").toURI());
|
||||||
|
|
||||||
|
try (Connection conn = xaConnectionManager.getConnection(); Stream<Path> list = Files.list(ddlPath);) {
|
||||||
|
list.forEach(path -> {
|
||||||
|
try {
|
||||||
|
RunScript.execute(conn, Files.newBufferedReader(path));
|
||||||
|
} catch (SQLException | IOException exc) {
|
||||||
|
exc.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
libraries/src/main/resources/ReladomoRuntimeConfig.xml
Normal file
6
libraries/src/main/resources/ReladomoRuntimeConfig.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<MithraRuntime>
|
||||||
|
<ConnectionManager className="com.baeldung.reladomo.ReladomoConnectionManager ">
|
||||||
|
<MithraObjectConfiguration className="com.baeldung.reladomo.Department" cacheType="partial"/>
|
||||||
|
<MithraObjectConfiguration className="com.baeldung.reladomo.Employee " cacheType="partial"/>
|
||||||
|
</ConnectionManager>
|
||||||
|
</MithraRuntime>
|
11
libraries/src/main/resources/reladomo/Department.xml
Normal file
11
libraries/src/main/resources/reladomo/Department.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<MithraObject objectType="transactional">
|
||||||
|
<PackageName>com.baeldung.reladomo</PackageName>
|
||||||
|
<ClassName>Department</ClassName>
|
||||||
|
<DefaultTable>departments</DefaultTable>
|
||||||
|
|
||||||
|
<Attribute name="id" javaType="long" columnName="department_id" primaryKey="true"/>
|
||||||
|
<Attribute name="name" javaType="String" columnName="name" maxLength="50" truncate="true"/>
|
||||||
|
<Relationship name="employees" relatedObject="Employee" cardinality="one-to-many" reverseRelationshipName="department" relatedIsDependent="true">
|
||||||
|
Employee.departmentId = this.id
|
||||||
|
</Relationship>
|
||||||
|
</MithraObject>
|
9
libraries/src/main/resources/reladomo/Employee.xml
Normal file
9
libraries/src/main/resources/reladomo/Employee.xml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<MithraObject objectType="transactional">
|
||||||
|
<PackageName>com.baeldung.reladomo</PackageName>
|
||||||
|
<ClassName>Employee</ClassName>
|
||||||
|
<DefaultTable>employees</DefaultTable>
|
||||||
|
|
||||||
|
<Attribute name="id" javaType="long" columnName="employee_id" primaryKey="true"/>
|
||||||
|
<Attribute name="name" javaType="String" columnName="name" maxLength="50" truncate="true"/>
|
||||||
|
<Attribute name="departmentId" javaType="long" columnName="department_id"/>
|
||||||
|
</MithraObject>
|
@ -0,0 +1,4 @@
|
|||||||
|
<Mithra>
|
||||||
|
<MithraObjectResource name="Department"/>
|
||||||
|
<MithraObjectResource name="Employee"/>
|
||||||
|
</Mithra>
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.reladomo;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.gs.fw.common.mithra.test.ConnectionManagerForTests;
|
||||||
|
import com.gs.fw.common.mithra.test.MithraTestResource;
|
||||||
|
|
||||||
|
public class ReladomoTest {
|
||||||
|
private MithraTestResource mithraTestResource;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
this.mithraTestResource = new MithraTestResource("reladomo/ReladomoTestConfig.xml");
|
||||||
|
|
||||||
|
final ConnectionManagerForTests connectionManager = ConnectionManagerForTests.getInstanceForDbName("testDb");
|
||||||
|
this.mithraTestResource.createSingleDatabase(connectionManager);
|
||||||
|
|
||||||
|
mithraTestResource.addTestDataToDatabase("reladomo/test-data.txt", connectionManager);
|
||||||
|
|
||||||
|
this.mithraTestResource.setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetTestData_thenOk() {
|
||||||
|
Employee employee = EmployeeFinder.findByPrimaryKey(1);
|
||||||
|
assertEquals(employee.getName(), "Paul");
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
this.mithraTestResource.tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
<MithraRuntime>
|
||||||
|
<ConnectionManager className="com.gs.fw.common.mithra.test.ConnectionManagerForTests">
|
||||||
|
<Property name="resourceName" value="testDb"/>
|
||||||
|
<MithraObjectConfiguration className="com.baeldung.reladomo.Department" cacheType="partial"/>
|
||||||
|
<MithraObjectConfiguration className="com.baeldung.reladomo.Employee " cacheType="partial"/>
|
||||||
|
</ConnectionManager>
|
||||||
|
</MithraRuntime>
|
7
libraries/src/test/resources/reladomo/test-data.txt
Normal file
7
libraries/src/test/resources/reladomo/test-data.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
class com.baeldung.reladomo.Department
|
||||||
|
id, name
|
||||||
|
1, "Marketing"
|
||||||
|
|
||||||
|
class com.baeldung.reladomo.Employee
|
||||||
|
id, name
|
||||||
|
1, "Paul"
|
Loading…
x
Reference in New Issue
Block a user