Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 237 → Rev 259

/tags/1.0.1/hibernate-spatial-mysql/src/test/java/org/hibernatespatial/mysql/MySQLExpressionTemplate.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-mysql/src/test/java/org/hibernatespatial/mysql/TestMySQLStoreRetrieve.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-oracle/src/test/resources/org.hibernatespatial.oracle.OracleSpatial10gDialect.properties
1,5 → 1,5
#
# $Id:$
# $Id$
#
# This file is part of Hibernate Spatial, an extension to the
# hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-oracle/src/test/resources/log4j.properties
1,5 → 1,5
#
# $Id:$
# $Id$
#
# This file is part of Hibernate Spatial, an extension to the
# hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-oracle/src/test/resources/hibernate.cfg.xml
1,6 → 1,6
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ $Id:$
~ $Id$
~
~ This file is part of Hibernate Spatial, an extension to the
~ hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-oracle/src/main/java/org/hibernatespatial/oracle/OracleSpatial10gDialect.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-oracle/src/main/java/org/hibernatespatial/oracle/SDOObjectProperty.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-oracle/src/main/java/org/hibernatespatial/oracle/SDOObjectMethod.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-maven/pom.xml
6,7 → 6,7
<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernatespatial</groupId>
<artifactId>hibernate-spatial-maven</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<name>Hibernate Spatial</name>
<description>
This is the Maven parent project for all Hibernate Spatial
24,6 → 24,7
<module>../hibernate-spatial-oracle</module>
<module>../hibernate-spatial-mysql</module>
<module>../hibernate-spatial-sqlserver</module>
<module>../hibernate-spatial-h2-geodb</module>
</modules>
<repositories>
<repository>
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/LineStringConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/MultiPolygonConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/AbstractConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/MultiPointConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/GeometryCollectionConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/PolygonConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/MultiLineStringConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/PointConvertorTest.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/TestSqlServerSpatialRestrictions.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Shape.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/LineStringDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/FigureAttribute.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PointDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/AbstractDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PointEncoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial-h2-geodb/pom.xml
22,7 → 22,7
<parent>
<groupId>org.hibernatespatial</groupId>
<artifactId>hibernate-spatial-maven</artifactId>
<version>1.1-SNAPSHOT</version>
<version>1.0</version>
<relativePath>../hibernate-spatial-maven</relativePath>
</parent>
<artifactId>hibernate-spatial-h2-geodb</artifactId>
/tags/1.0.1/hibernate-spatial-postgis/src/site/xdoc/usage.xml
1,7 → 1,7
<?xml version="1.0" encoding="UTF-8"?>
 
<!--
~ $Id:$
~ $Id$
~
~ This file is part of Hibernate Spatial, an extension to the
~ hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/test/java/org/hibernatespatial/test/DataSourceUtils.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/Circle.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/SpatialAnalysis.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/SpatialAggregate.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/readers/BasicFeatureReader.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/readers/FeatureAdapter.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/spi/SpatialDialectProvider.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/cfg/GeometryFactoryHelper.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/SpatialDialect.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/PKeyNotFoundException.java
File deleted
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/FeatureMapper.java
3,77 → 3,147
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
public class FeatureMapper {
 
private final NamingStrategy naming;
private final TypeMapper typeMapper;
public FeatureMapper (NamingStrategy naming, TypeMapper typeMapper){
this.naming = naming;
this.typeMapper = typeMapper;
}
public ClassInfo createClassInfo(String catalog, String schema, String tableName, DatabaseMetaData dmd) throws TableNotFoundException {
ResultSet rs = null;
boolean empty = true;
String className = naming.createClassName(tableName);
ClassInfo cInfo = new ClassInfo(tableName, className);
try {
rs = dmd.getColumns(catalog, schema, tableName, null);
while (rs.next()){
empty = false;
String colName = rs.getString("COLUMN_NAME");
String dbType = rs.getString("TYPE_NAME");
int javaType = rs.getInt("DATA_TYPE");
AttributeInfo ai = new AttributeInfo();
ai.setColumnName(colName);
ai.setFieldName(naming.createPropertyName(colName));
ai.setHibernateType(typeMapper.getHibernateType(dbType, javaType));
ai.setCtClass(typeMapper.getCtClass(dbType, javaType));
cInfo.addAttribute(ai);
}
} catch (SQLException ex){
throw new RuntimeException(ex);
} finally {
try {
rs.close();
} catch (SQLException e) {
// do nothing
}
}
if (empty) {
throw new TableNotFoundException(tableName);
}
try {
// locate the primary key
rs = dmd.getPrimaryKeys(catalog, schema, tableName);
// TODO -- improve this.
// We should check whether or not a suitable pkey exists
private final NamingStrategy naming;
private final TypeMapper typeMapper;
 
boolean hasPkey = rs.next();
if (hasPkey) {
String pkn = rs.getString("COLUMN_NAME");
for (AttributeInfo ai : cInfo.getAttributes()) {
if (ai.getColumnName().equals(pkn)) {
ai.setIdentifier(true);
break;
}
}
}
}catch (SQLException e){
throw new RuntimeException(e);
} finally {
try {
rs.close();
} catch (SQLException e) {
//do nothing
}
}
return cInfo;
}
public FeatureMapper(NamingStrategy naming, TypeMapper typeMapper) {
this.naming = naming;
this.typeMapper = typeMapper;
}
 
public ClassInfo createClassInfo(String catalog, String schema, String tableName, DatabaseMetaData dmd) throws TableNotFoundException, MissingIdentifierException {
String className = naming.createClassName(tableName);
ClassInfo cInfo = new ClassInfo(tableName, className);
readColums(catalog, schema, tableName, dmd, cInfo);
determineIdentifier(catalog, schema, tableName, dmd, cInfo);
return cInfo;
}
 
private void determineIdentifier(String catalog, String schema, String tableName, DatabaseMetaData dmd, ClassInfo cInfo) throws MissingIdentifierException {
String pkn = null;
pkn = determinePrimaryKey(catalog, schema, tableName, dmd);
if (pkn == null) {
pkn = findUniqueIndex(catalog, schema, tableName, dmd);
}
if (pkn == null) throw new MissingIdentifierException(tableName);
setAsIdentifier(cInfo, pkn);
return;
 
}
 
private String findUniqueIndex(String catalog, String schema, String tableName, DatabaseMetaData dmd) {
Map<String, String> indexes = new HashMap<String,String>();
Set<String> rejectedIndexes = new HashSet<String>();
readUniqueIndexes(catalog, schema, tableName, dmd, indexes, rejectedIndexes);
for (String candidate : indexes.keySet()){
if (!rejectedIndexes.contains(candidate)) return indexes.get(candidate);
}
return null;
}
 
private void readUniqueIndexes(String catalog, String schema, String tableName, DatabaseMetaData dmd, Map<String, String> indexes, Set<String> rejectedIndexes) {
ResultSet rs = null;
try {
rs = dmd.getIndexInfo(catalog, schema, tableName, true, false);
while(rs.next()){
String colName = rs.getString("COLUMN_NAME");
String indexName = rs.getString("INDEX_NAME");
if (indexName == null){
indexName = colName;
}
if (indexes.get(indexName) != null){
rejectedIndexes.add(indexName);
} else {
indexes.put(indexName, colName);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
rs.close();
} catch (SQLException e) {
//do nothing
}
}
}
 
private String determinePrimaryKey(String catalog, String schema, String tableName, DatabaseMetaData dmd) {
String pkn = null;
ResultSet rs = null;
try {
rs = dmd.getPrimaryKeys(catalog, schema, tableName);
if(!rs.next()) return null;
pkn = rs.getString("COLUMN_NAME");
//check whether the primary key is non-composite
if (rs.next()) return null;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
rs.close();
} catch (SQLException e) {
//do nothing
}
}
return pkn;
}
 
private void readColums(String catalog, String schema, String tableName, DatabaseMetaData dmd, ClassInfo cInfo) throws TableNotFoundException {
ResultSet rs = null;
boolean empty = true;
try {
rs = dmd.getColumns(catalog, schema, tableName, null);
while (rs.next()) {
empty = false;
String colName = rs.getString("COLUMN_NAME");
String dbType = rs.getString("TYPE_NAME");
int javaType = rs.getInt("DATA_TYPE");
addAttribute(cInfo, colName, dbType, javaType);
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
} finally {
try {
rs.close();
} catch (SQLException e) {
// do nothing
}
}
if (empty) {
throw new TableNotFoundException(tableName);
}
}
 
private void setAsIdentifier(ClassInfo cInfo, String pkn) {
for (AttributeInfo ai : cInfo.getAttributes()) {
if (ai.getColumnName().equals(pkn)) {
ai.setIdentifier(true);
break;
}
}
}
 
private void addAttribute(ClassInfo cInfo, String colName, String dbType, int javaType) {
String hibernateType = null;
try {
hibernateType = typeMapper.getHibernateType(dbType, javaType);
} catch (TypeNotFoundException e) {
 
}
AttributeInfo ai = new AttributeInfo();
ai.setColumnName(colName);
ai.setFieldName(naming.createPropertyName(colName));
ai.setHibernateType(hibernateType);
ai.setCtClass(typeMapper.getCtClass(dbType, javaType));
cInfo.addAttribute(ai);
}
 
}
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/ClassInfo.java
40,13 → 40,13
this.tableName = tableName;
}
 
public AttributeInfo getIdAttribute() throws PKeyNotFoundException {
public AttributeInfo getIdAttribute() throws MissingIdentifierException {
for (AttributeInfo ai : getAttributes()) {
if (ai.isIdentifier()) {
return ai;
}
}
throw new PKeyNotFoundException();
throw new MissingIdentifierException();
}
 
public AttributeInfo getGeomAttribute() throws GeometryNotFoundException {
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/MappingsGenerator.java
24,16 → 24,16
*/
package org.hibernatespatial.pojo;
 
import java.io.IOException;
import java.io.Writer;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
 
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
 
/**
* This class creates a Hibernate mapping file for a list of tables.
*
60,22 → 60,25
return this.mappingDoc;
}
 
public void load(List<ClassInfo> mappedClasses)
throws PKeyNotFoundException {
public void load(Collection<ClassInfo> mappedClasses, String schema)
throws MissingIdentifierException {
 
this.mappingDoc = DocumentHelper.createDocument();
this.mappingDoc.addDocType("hibernate-mapping",
"-//Hibernate/Hibernate Mapping DTD 3.0//EN",
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd");
Element root = this.mappingDoc.addElement("hibernate-mapping")
.addAttribute("package", this.packageName);
Element root = this.mappingDoc.addElement("hibernate-mapping");
root.addAttribute("package", this.packageName);
if (schema != null){
root.addAttribute("schema", schema);
}
for (ClassInfo classInfo: mappedClasses) {
addTableElement(root, classInfo);
}
}
 
private void addTableElement(Element root, ClassInfo classInfo)
throws PKeyNotFoundException {
throws MissingIdentifierException {
Element tableEl = root.addElement("class");
tableEl.addAttribute("name", classInfo.getClassName());
tableEl.addAttribute("table", classInfo.getTableName());
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/TypeMapper.java
24,16 → 24,18
*/
package org.hibernatespatial.pojo;
 
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
 
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
 
import javassist.NotFoundException;
import org.hibernatespatial.GeometryUserType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
 
/**
* The <code>TypeMapper</code> maps a pair consisting of java.sql.Type, and a
* database type name to a CtClass (a representation of a java type used by the
44,6 → 46,8
*/
public class TypeMapper {
 
protected final static Logger logger = LoggerFactory.getLogger(TypeMapper.class);
 
private final static String GEOMETRY_USER_TYPE = GeometryUserType.class
.getCanonicalName();
 
63,37 → 67,53
// ensure that we can load the JTS classes.
pool.insertClassPath(new ClassClassPath(this.getClass()));
 
CtClass ctString = null;
CtClass ctDate = null;
CtClass ctString;
CtClass ctDate;
CtClass ctInteger;
CtClass ctBoolean;
CtClass ctFloat;
CtClass ctDouble;
CtClass ctLong;
CtClass ctShort;
CtClass ctBigDecimal;
CtClass ctByte;
CtClass ctBinary;
try {
ctString = pool.get("java.lang.String");
ctDate = pool.get("java.util.Date");
ctGeom = pool.get("com.vividsolutions.jts.geom.Geometry");
} catch (Exception e) {
ctInteger = pool.get("java.lang.Integer");
ctBoolean = pool.get("java.lang.Boolean");
ctDouble = pool.get("java.lang.Double");
ctLong = pool.get("java.lang.Long");
ctShort = pool.get("java.lang.Short");
ctFloat = pool.get("java.lang.Float");
ctBigDecimal = pool.get("java.math.BigDecimal");
ctByte = pool.get("java.lang.Byte");
ctBinary = pool.get("byte[]");
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
 
entries.add(new TMEntry(Types.BIGINT, "integer", CtClass.longType));
 
entries.add(new TMEntry(Types.SMALLINT, "integer", CtClass.intType));
 
entries.add(new TMEntry(Types.BOOLEAN, "boolean", CtClass.booleanType));
 
entries.add(new TMEntry(Types.BIGINT, "integer", ctInteger));
entries.add(new TMEntry(Types.SMALLINT, "short", ctShort));
entries.add(new TMEntry(Types.TINYINT, "byte", ctByte));
entries.add(new TMEntry(Types.BOOLEAN, "boolean", ctBoolean));
entries.add(new TMEntry(Types.BIT, "boolean", ctBoolean));
entries.add(new TMEntry(Types.CHAR, "string", ctString));
 
entries.add(new TMEntry(Types.DATE, "date", ctDate));
 
entries.add(new TMEntry(Types.DECIMAL, "double", CtClass.doubleType));
 
entries.add(new TMEntry(Types.DOUBLE, "double", CtClass.doubleType));
 
entries.add(new TMEntry(Types.NUMERIC, "double", CtClass.doubleType));
 
entries.add(new TMEntry(Types.FLOAT, "double", CtClass.doubleType));
 
entries.add(new TMEntry(Types.INTEGER, "long", CtClass.longType));
 
entries.add(new TMEntry(Types.TIMESTAMP, "timestamp", ctDate));
entries.add(new TMEntry(Types.TIME, "time", ctDate));
entries.add(new TMEntry(Types.DECIMAL, "big_decimal", ctBigDecimal));
entries.add(new TMEntry(Types.DOUBLE, "double", ctDouble));
entries.add(new TMEntry(Types.NUMERIC, "big_decimal", ctBigDecimal));
entries.add(new TMEntry(Types.FLOAT, "float", ctFloat));
entries.add(new TMEntry(Types.INTEGER, "integer", ctInteger));
entries.add(new TMEntry(Types.BIGINT, "long", ctLong));
entries.add(new TMEntry(Types.VARCHAR, "string", ctString));
entries.add(new TMEntry(Types.BINARY, "binary", ctBinary));
entries.add(new TMEntry(Types.CLOB, "text", ctString));
}
 
public CtClass getCtClass(String dbType, int sqlType) {
108,7 → 128,7
return null;
}
 
public String getHibernateType(String dbType, int sqlType) {
public String getHibernateType(String dbType, int sqlType) throws TypeNotFoundException {
if (dbType.equalsIgnoreCase(this.dbGeomType)) {
return GEOMETRY_USER_TYPE;
}
117,7 → 137,7
return entry.hibernateTypeName;
}
}
return null;
throw new TypeNotFoundException(dbType);
 
}
 
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/MissingIdentifierException.java
New file
0,0 → 1,58
/**
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2008 Geovise BVBA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
package org.hibernatespatial.pojo;
 
/**
* This Exception is thrown when the POJO Utility cannot locate a primary key.
*
* @author Karel Maesen, Geovise BVBA
*
*/
public class MissingIdentifierException extends Exception {
 
/**
*
*/
private static final long serialVersionUID = 1L;
 
private static final String basemsg = "No suitable identifier found in table";
 
public MissingIdentifierException() {
super(basemsg);
}
 
public MissingIdentifierException(String msg) {
super(basemsg + ":" + msg);
}
 
public MissingIdentifierException(Throwable cause) {
super(cause);
}
 
public MissingIdentifierException(String msg, Throwable cause) {
super(basemsg + ":" + msg, cause);
}
 
}
Property changes:
Added: svn:keywords
+ Id
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/AutoMapper.java
3,7 → 3,7
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
*
* Copyright © 2008 Geovise BVBA
*
* This library is free software; you can redistribute it and/or
24,134 → 24,257
*/
package org.hibernatespatial.pojo;
 
import org.dom4j.Document;
import org.hibernatespatial.HBSpatialExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.dom4j.Document;
import org.hibernatespatial.HBSpatialExtension;
 
/**
* @author Karel Maesen, Geovise BVBA
*
*/
public class AutoMapper {
 
protected final static Logger logger = LoggerFactory.getLogger(AutoMapper.class);
protected final static String PACKAGE_NAME = "org.hibernatespatial.features.generated";
private static Map<TableName, Class<?>> tableClassMap = new HashMap<TableName, Class<?>>();
public static synchronized Document map(Connection conn, String catalog, String schema, Collection<String> tableNames) throws SQLException {
NamingStrategy naming = new SimpleNamingStrategy();
TypeMapper typeMapper = new TypeMapper(HBSpatialExtension.getDefaultSpatialDialect().getDbGeometryTypeName());
DatabaseMetaData dmd = conn.getMetaData();
FeatureMapper fMapper = new FeatureMapper(naming, typeMapper);
FeatureClassGenerator fGenerator = new FeatureClassGenerator(PACKAGE_NAME,naming);
List<ClassInfo> cInfos = new ArrayList<ClassInfo>();
for (String tableName : tableNames) {
logger.info("Generating class info for table " + tableName + " in catalog/schema " + catalog + "/" + schema);
//TODO -- test if we haven't already mapped the table
ClassInfo cInfo;
try {
cInfo = fMapper.createClassInfo(catalog, schema, tableName, dmd);
logger.info("Generating class " + cInfo.getClassName() + " for table " + tableName);
Class <?> clazz = fGenerator.generate(cInfo);
tableClassMap.put(new TableName(catalog, schema, tableName), clazz);
cInfos.add(cInfo);
} catch (TableNotFoundException e) {
logger.warn(e.getMessage());
}
}
logger.info("Generating Hibernate Mapping file");
MappingsGenerator mappingGenerator = new MappingsGenerator(PACKAGE_NAME);
try {
mappingGenerator.load(cInfos);
} catch (PKeyNotFoundException e) {
throw new RuntimeException(e);
}
return mappingGenerator.getMappingsDoc();
}
public static Class<?> getClass(String catalog, String schema, String tableName){
TableName tbn = new TableName(catalog, schema, tableName);
return tableClassMap.get(tbn);
}
public static List<String[]> getMappedTables(){
List<String[]> list = new ArrayList<String[]>();
for (TableName tbn : tableClassMap.keySet()){
String[] sa = new String[3];
sa[0] = tbn.catalog;
sa[1] = tbn.schema;
sa[2] = tbn.tableName;
list.add(sa);
}
return list;
}
private static class TableName {
String catalog;
String schema;
String tableName;
private TableName(String catalog, String schema, String tableName){
this.catalog = catalog;
this.schema = schema;
this.tableName = tableName;
}
protected final static Logger logger = LoggerFactory.getLogger(AutoMapper.class);
 
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((catalog == null) ? 0 : catalog.hashCode());
result = prime * result
+ ((schema == null) ? 0 : schema.hashCode());
result = prime * result
+ ((tableName == null) ? 0 : tableName.hashCode());
return result;
}
protected final static String PACKAGE_NAME = "org.hibernatespatial.features.generated";
 
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof TableName))
return false;
TableName other = (TableName) obj;
if (catalog == null) {
if (other.catalog != null)
return false;
} else if (!catalog.equals(other.catalog))
return false;
if (schema == null) {
if (other.schema != null)
return false;
} else if (!schema.equals(other.schema))
return false;
if (tableName == null) {
if (other.tableName != null)
return false;
} else if (!tableName.equals(other.tableName))
return false;
return true;
}
}
private static Map<TableName, Class<?>> tableClassMap = new HashMap<TableName, Class<?>>();
 
private static Map<TableName, ClassInfo> tableClassInfoMap = new HashMap<TableName, ClassInfo>();
 
private static NamingStrategy naming = new SimpleNamingStrategy();
 
/**
* Returns the Hibernate mapping document for the specified tables
* <p/>
* <p>To create the mapping, a <code>Connection</code> object must be
* provided to provide access to the specified tables.
* This connection will not be closed on return.</p>
*
* @param conn JDBC <code>Connection</code> used during mapping
* @param catalog database catalog
* @param schema database schema
* @param tableNames list of table names
* @return the XML mapping document that maps the tables specified by the catalog, schema and tablenames arguments.
* @throws SQLException
*/
public static synchronized Document map(Connection conn, String catalog, String schema, Collection<String> tableNames) throws SQLException {
TypeMapper typeMapper = new TypeMapper(HBSpatialExtension.getDefaultSpatialDialect().getDbGeometryTypeName());
DatabaseMetaData dmd = conn.getMetaData();
FeatureMapper fMapper = new FeatureMapper(naming, typeMapper);
FeatureClassGenerator fGenerator = new FeatureClassGenerator(PACKAGE_NAME, naming);
 
for (String tableName : tableNames) {
TableName table = new TableName(catalog, schema, tableName);
if (tableClassInfoMap.get(table) != null) {
logger.info("Class info for table " + tableName + " in catalog/schema " + catalog + "/" + schema + " has already been mapped.");
continue;
}
logger.info("Generating class info for table " + tableName + " in catalog/schema " + catalog + "/" + schema);
ClassInfo cInfo;
try {
cInfo = fMapper.createClassInfo(catalog, schema, tableName, dmd);
logger.info("Generating class " + cInfo.getClassName() + " for table " + tableName);
Class<?> clazz = fGenerator.generate(cInfo);
tableClassMap.put(table, clazz);
tableClassInfoMap.put(table, cInfo);
 
} catch (TableNotFoundException e) {
logger.warn(e.getMessage());
} catch (MissingIdentifierException e) {
logger.warn(e.getMessage());
}
}
logger.info("Generating Hibernate Mapping file");
MappingsGenerator mappingGenerator = new MappingsGenerator(PACKAGE_NAME);
try {
mappingGenerator.load(tableClassInfoMap.values(), schema);
} catch (MissingIdentifierException e) {
throw new RuntimeException(e);
}
return mappingGenerator.getMappingsDoc();
}
 
/**
* Returns the <code>Class</code> object to which the specified table is mapped
*
* @param catalog catalog of the table
* @param schema schema of the table
* @param tableName name of the table
* @return class to which the table specified by the arguments is mapped
*/
public static Class<?> getClass(String catalog, String schema, String tableName) {
TableName tbn = new TableName(catalog, schema, tableName);
return tableClassMap.get(tbn);
}
 
/**
* Returns the tables mapped by this automapper.
*
* @return a List of mapped tables. Each table is represented by a String array with the first
* component the catalog, the second the schema, and the third the table name.
*/
public static List<String[]> getMappedTables() {
List<String[]> list = new ArrayList<String[]>();
for (TableName tbn : tableClassMap.keySet()) {
String[] sa = new String[3];
sa[0] = tbn.catalog;
sa[1] = tbn.schema;
sa[2] = tbn.tableName;
list.add(sa);
}
return list;
}
 
/**
* Returns the attribute names of the class to with the specified table is mapped
*
* @param catalog catalog of the table
* @param schema schema of the table
* @param tableName name of the table
* @return list of attribute (field) names of the class that corresponds with the table identified by the arguments
*/
public static List<String> getAttributes(String catalog, String schema, String tableName) {
List<AttributeInfo> attributes = getAttributeInfos(catalog, schema, tableName);
List<String> result = new ArrayList<String>();
for (AttributeInfo attributeInfo : attributes) {
result.add(attributeInfo.getFieldName());
}
return result;
}
 
private static List<AttributeInfo> getAttributeInfos(String catalog, String schema, String tableName) {
TableName tbn = new TableName(catalog, schema, tableName);
ClassInfo cInfo = tableClassInfoMap.get(tbn);
if (cInfo == null) return new ArrayList<AttributeInfo>();
return cInfo.getAttributes();
}
 
/**
* Returns the Identifier attribute
*
* @param catalog catalog of the table
* @param schema schema of the table
* @param tableName name of the table
* @return the attribute name which functions as a unique identifier for the objects corresponding
* to rows in the specified table
* @throws MissingIdentifierException when no Identifier property is available
*/
public static String getIdAttribute(String catalog, String schema, String tableName) throws MissingIdentifierException {
TableName tbn = new TableName(catalog, schema, tableName);
ClassInfo cInfo = tableClassInfoMap.get(tbn);
return cInfo.getIdAttribute().getFieldName();
}
 
/**
* Returns the (default) <code>Geometry</code>-valued attribute
*
* @param catalog catalog of the table
* @param schema schema of the table
* @param tableName name of the table
* @return the name of the <code>Geometry</code>-valued attribute
* @throws GeometryNotFoundException when no <code>Geometry</code>-valued property is available
*/
public static String getGeometryAttribute(String catalog, String schema, String tableName) throws GeometryNotFoundException {
TableName tbn = new TableName(catalog, schema, tableName);
ClassInfo cInfo = tableClassInfoMap.get(tbn);
return cInfo.getGeomAttribute().getFieldName();
}
 
/**
* Returns the name of the setter-method for the attribute
*
* @param catalog catalog of the table
* @param schema schema of the table
* @param tableName name of the table
* @param attribute name of the attribute of the class to which this class is mapped
* @return the name of the setter-method of the attribute specified by the arguments
*/
public static String getAttributeSetterName(String catalog, String schema, String tableName, String attribute) {
getAttributeInfo(catalog, schema, tableName, attribute);
return naming.createSetterName(attribute);
}
 
/**
* Returns the name of the getter-method for the attribute
*
* @param catalog catalog of the table
* @param schema schema of the table
* @param tableName name of the table
* @param attribute name of the attribute of the class to which this class is mapped
* @return the name of the getter-method of the attribute specified by the arguments
* */
public static String getAttributeGetterName(String catalog, String schema, String tableName, String attribute) {
getAttributeInfo(catalog, schema, tableName, attribute);
return naming.createGetterName(attribute);
}
 
private static AttributeInfo getAttributeInfo(String catalog, String schema, String tableName, String attribute) {
if (attribute == null) throw new IllegalArgumentException("Null attribute received.");
for (AttributeInfo candidate : getAttributeInfos(catalog, schema, tableName)) {
if (candidate.getFieldName().equals(attribute)) {
return candidate;
}
}
throw new IllegalArgumentException(String.format("%s is not an attribute of the class to which table %s is mapped.", attribute, tableName));
}
 
private static class TableName {
String catalog;
String schema;
String tableName;
 
private TableName(String catalog, String schema, String tableName) {
this.catalog = catalog;
this.schema = schema;
this.tableName = tableName;
}
 
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((catalog == null) ? 0 : catalog.hashCode());
result = prime * result
+ ((schema == null) ? 0 : schema.hashCode());
result = prime * result
+ ((tableName == null) ? 0 : tableName.hashCode());
return result;
}
 
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof TableName))
return false;
TableName other = (TableName) obj;
if (catalog == null) {
if (other.catalog != null)
return false;
} else if (!catalog.equals(other.catalog))
return false;
if (schema == null) {
if (other.schema != null)
return false;
} else if (!schema.equals(other.schema))
return false;
if (tableName == null) {
if (other.tableName != null)
return false;
} else if (!tableName.equals(other.tableName))
return false;
return true;
}
 
 
}
}
/tags/1.0.1/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/TypeNotFoundException.java
New file
0,0 → 1,60
/*
* $Id:$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007-2010 Geovise BVBA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.pojo;
 
/**
* This Exception is thrown when the POJO Utility cannot locate a primary key.
*
* @author Karel Maesen, Geovise BVBA
* creation-date: Jun 24, 2010
*/
public class TypeNotFoundException extends Exception {
 
/**
*
*/
private static final long serialVersionUID = 1L;
 
private static final String basemsg = "Type not found";
 
public TypeNotFoundException() {
super(basemsg);
}
 
public TypeNotFoundException(String msg) {
super(basemsg + ":" + msg);
}
 
public TypeNotFoundException(Throwable cause) {
super(cause);
}
 
public TypeNotFoundException(String msg, Throwable cause) {
super(basemsg + ":" + msg, cause);
}
 
}
 
/tags/1.0.1/hibernate-spatial/src/site/site.xml
66,6 → 66,7
<item name="Postgresql" href="hibernate-spatial-postgis"/>
<item name="Microsft SQL Server" href="hibernate-spatial-sqlserver"/>
<item name="MySQL" href="hibernate-spatial-mysql"/>
<item name="GeoDB (H2)" href="hibernate-spatial-h2-geodb" />
</menu>
 
</body>
/tags/1.0.1/hibernate-spatial/src/site/xdoc/download.xml
219,6 → 219,24
</a>
</td>
</tr>
 
<tr>
 
<td>GeoDB Provider</td>
<td>
<a
href="http://www.hibernatespatial.org/repository/org/hibernatespatial/hibernate-spatial-h2-geodb/1.0/hibernate-spatial-h2-geodb-1.0.jar">
hibernate-spatial-h2-geodb-1.0.jar
</a>
</td>
<td>
<a
href="http://www.hibernatespatial.org/repository/org/hibernatespatial/hibernate-spatial-h2-geodb/1.0/hibernate-spatial-h2-geodb-1.0.jar.md5">
hibernate-spatial-h2-geodb-1.0.jar.md5
</a>
</td>
</tr>
 
</table>
 
<subsection name="Release 1.0-M2 (2008-08-15)">
/tags/1.0.1/hibernate-spatial/pom.xml
6,7 → 6,7
<parent>
<groupId>org.hibernatespatial</groupId>
<artifactId>hibernate-spatial-maven</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<relativePath>../hibernate-spatial-maven</relativePath>
</parent>
<name>Hibernate Spatial Core</name>