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:
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.
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
Listing - Connector.java
Listing - Owner.java
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 |
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
Post a Comment