Skip to main content

Part V : A Simple and Smart Result Mapper

The examples so far only deal with single table. But it is very much possible to join multiple tables and still use this mapper. As an example consider three entities as shown below:
T_REPOSITORY
REPOSITORY_ID int
REPOSITORY_NAME varchar
CONNECTOR_ID int
T_CONNECTOR
CONNECTOR_ID int
CONNECTOR_NAME varchar
URL varchar
OWNER_ID int
T_OWNER
OWNER_ID int
OWNER_NAME varchar
OWNER_KEY varchar
The relationship is clear - a repository has a connector and a connector has a owner. So how can you populate a Repository object containing a Connector Object which in turn has an Owner object. This is just simple. Execute this query.
SELECT r.repository_id "repositoryId", r.repository_name "repositoryName",
c.connector_id "connector.connectorId",
c.connector_name "connector.connectorName",
c.url "connector.url",
o.owner_id "connector.owner.ownerId",
o.owner_name "connector.owner.ownerName",
o.owner_key "connector.owner.ownerKey"
FROM
t_repository r,t_connector c, t_owner o
WHERE
r.repository_id = ?
AND c.connector_id = r.connector_id
AND o.owner_id = c.owner_id


The mapper automatically checks if the child object exists, if not it creates one and then populates the properties. The Javabeans are listed below for reference.
Listing - Repository.java
import org.apache.commons.lang.builder.ToStringBuilder;
public class Repository {
private int repositoryId;
private String repositoryName;
private Connector connector;
/**
* @return the repositoryId
*/
public int getRepositoryId() {
return repositoryId;
}
/**
* @param repositoryId the repositoryId to set
*/
public void setRepositoryId(int repositoryId) {
this.repositoryId = repositoryId;
}
/**
* @return the repositoryName
*/
public String getRepositoryName() {
return repositoryName;
}
/**
* @param repositoryName the repositoryName to set
*/
public void setRepositoryName(String repositoryName) {
this.repositoryName = repositoryName;
}
/**
* @return the connector
*/
public Connector getConnector() {
return connector;
}
/**
* @param connector the connector to set
*/
public void setConnector(Connector connector) {
this.connector = connector;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {

return ToStringBuilder.reflectionToString(this);
}


}


Listing - Connector.java
import org.apache.commons.lang.builder.ToStringBuilder;
public class Connector {
private int connectorId;
private String connectorName;
private String url;
private Owner owner;
/**
* @return the connectorId
*/
public int getConnectorId() {
return connectorId;
}
/**
* @param connectorId the connectorId to set
*/
public void setConnectorId(int connectorId) {
this.connectorId = connectorId;
}
/**
* @return the connectorName
*/
public String getConnectorName() {
return connectorName;
}
/**
* @param connectorName the connectorName to set
*/
public void setConnectorName(String connectorName) {
this.connectorName = connectorName;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}


/**
* @return the owner
*/
public Owner getOwner() {
return owner;
}
/**
* @param owner the owner to set
*/
public void setOwner(Owner owner) {
this.owner = owner;
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {

return ToStringBuilder.reflectionToString(this);
}
}


Listing - Owner.java
import org.apache.commons.lang.builder.ToStringBuilder;


/**
* @author dhrubo
*
*/
public class Owner {
private long ownerId;
private String ownerName;
private String ownerKey;



/**
* @return the ownerId
*/
public long getOwnerId() {
return ownerId;
}



/**
* @param ownerId the ownerId to set
*/
public void setOwnerId(long ownerId) {
this.ownerId = ownerId;
}



/**
* @return the ownerName
*/
public String getOwnerName() {
return ownerName;
}



/**
* @param ownerName the ownerName to set
*/
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}



/**
* @return the ownerKey
*/
public String getOwnerKey() {
return ownerKey;
}



/**
* @param ownerKey the ownerKey to set
*/
public void setOwnerKey(String ownerKey) {
this.ownerKey = ownerKey;
}



@Override
public String toString() {

return ToStringBuilder.reflectionToString(this);
}
}

Comments

Popular posts from this blog

Why do you need Spring Cloud Config server?

Last month I wrote a primer on concepts around 12 factor app. Before getting into the details of the Spring Cloud Config Server, I must refresh on the principle #3 from the list presented in that post.

3 – ConfigurationStore config in the environments
Configuration information must be separate from the source code. This may seem so obvious, but often we are guilty of leaving critical configuration parameters in the scattered in the code. Instead, applications should have environment specific configuration files. The sensitive information like database password or API key should be stored in these environment configuration files in encrypted format.
 The key takeaways from this postulate for a cloud-native microservices application are:
Do not store configuration as part of the deployable unit (in the case of lead microservice - inside the jar or war if you are still deploying war like the good old days). Instead, store it in an external location and make it easily accessible during run-…

Upgrading Lead Microservice - Use MariaDB and Flyway with Spring Boot

So far I have been using an in-memory H2 database or Mockito for testing the lead microservice. To make the transition towards using the Spring Cloud Config server, I need to upgrade the micro-application to use MariaDB. I will be adding the configuration in the application.yml  the file which in the subsequent post will move over to the config server store. I will also be using Flyway to make it easy to maintain the database schema changes in future. I will use this post to introduce Flyway in the mix. Spring Boot also provides first class integration with Flyway. I am using Flyway as its really quick and easy to get started, minimal learning curve (no DSL) and I am comfortable with it having used it in the past.

Assumptions

MariaDB 10 is installedBasic familiarity with FlywayHeidi SQL client is installed.
Step 1 - Update build.gradle to include the MariaDB JDBC and Flyway dependencies.
Do not forget to do a Gradle refresh on your IDE (I am using STS 3.8.4 on Java 8)

Step 2 - Rename the…

Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- Tiles -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${tiles.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>${tiles.version}</version>
<scope>compile</scope>