2014-03-05 35 views
1

嘗試使用HibernateSpatial API在數據庫中存儲多邊形形狀。我已按照this tutorial存儲值。我正在使用Mysql作爲我的數據庫服務器。所以爲我的場景做了一些改變。獲取以下錯誤以將其存儲在數據庫中。如何在HibernateSpatial API中從多邊形對象數據類型更改幾何對象數據類型?

:ERROR:

錯誤[org.hibernate.engine.jdbc.spi.SqlExceptionHelper(基於HTTP/192.168.5.244:8080-1)數據截斷:無法獲取幾何從數據對象發送到幾何領域

錯誤[com.service.HibernateSaveService(基於HTTP/192.168.5.244:8080-1)例外:_:無法執行語句

提示

我得到這個錯誤,因爲試圖直接在Geometry數據類型中傳遞多邊形(這是我的假設)。請幫我解決這個問題,並幫助我將值存儲在數據庫中。

我的代碼如下。

實體類:_:

package com.entity; 

// Generated Mar 1, 2014 12:22:56 PM by Hibernate Tools 3.4.0.CR1 

import static javax.persistence.GenerationType.IDENTITY; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.persistence.UniqueConstraint; 

import org.hibernate.annotations.Type; 

import com.vividsolutions.jts.geom.Polygon; 

/** 
* HibernateSpatial generated by hbm2java 
*/ 
@Entity 
@Table(name = "HIBERNATE_SPATIAL", catalog = "geocodedb", uniqueConstraints = @UniqueConstraint(columnNames = "GEO_REGION_NAME")) 
public class HibernateSpatialEntity implements java.io.Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private Integer prdGeoRegionId; 
    private String geoRegionName; 
    private String geoRegionDescription; 

    @Type(type="org.hibernate.spatial.GeometryType") 
    private Polygon geoRegionDefinition; 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "PRD_GEO_REGION_ID", unique = true, nullable = false) 
    public Integer getPrdGeoRegionId() { 
     return this.prdGeoRegionId; 
    } 

    public void setPrdGeoRegionId(Integer prdGeoRegionId) { 
     this.prdGeoRegionId = prdGeoRegionId; 
    } 

    @Column(name = "GEO_REGION_NAME", unique = true, nullable = false, length = 50) 
    public String getGeoRegionName() { 
     return this.geoRegionName; 
    } 

    public void setGeoRegionName(String geoRegionName) { 
     this.geoRegionName = geoRegionName; 
    } 

    @Column(name = "GEO_REGION_DESCRIPTION", length = 70) 
    public String getGeoRegionDescription() { 
     return this.geoRegionDescription; 
    } 

    public void setGeoRegionDescription(String geoRegionDescription) { 
     this.geoRegionDescription = geoRegionDescription; 
    } 

    @Column(name = "GEO_REGION_DEFINITION") 
    public Polygon getGeoRegionDefinition() { 
     return this.geoRegionDefinition; 
    } 

    public void setGeoRegionDefinition(Polygon geoRegionDefinition) { 
     this.geoRegionDefinition = geoRegionDefinition; 
    } 

} 

PERSIST CLASS:_:

package com.service; 

import java.util.Random; 
import javax.ejb.Stateless; 
import javax.ejb.TransactionManagement; 
import javax.ejb.TransactionManagementType; 
import javax.persistence.EntityManager; 
import javax.persistence.Persistence; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 

import org.hibernate.Session; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import com.solartis.entity.HibernateSpatialEntity; 
import com.vividsolutions.jts.geom.Geometry; 
import com.vividsolutions.jts.geom.Polygon; 
import com.vividsolutions.jts.io.ParseException; 
import com.vividsolutions.jts.io.WKTReader; 

@Stateless 
@TransactionManagement(TransactionManagementType.BEAN) 
@Path("/") 
public class HibernateSaveService { 
    /** 
    * mLogger - attribute used for to write the log 
    */ 
    private static final Logger mLogger = LoggerFactory 
      .getLogger(HibernateSaveService.class); 

    @POST 
    @Path("/saveHibernate") 
    @Consumes("*/*") 
    @Produces("*/*") 
    public String saveShapes(String jsonRequest) { 
     String statusResult = null; 
     String methodName = "saveShapes"; 
     StringBuilder regionName = null; 
     Session session = null; 
     Random random = null; 
     HibernateSpatialEntity hibernateEntity = null; 
     Polygon poly = null; 
     String singleShape = "Polygon((-50.123456789 -50.123456789,50.98765432175 -50.753951654852,50 50,-50 50,-50.123456789 -50.123456789))"; 

     mLogger.debug("Entered inside the Method :_: " + methodName); 
     Geometry geom = wktToGeometry(singleShape); 
     try { 
      EntityManager entity = Persistence.createEntityManagerFactory(
        "spatialPersistenceUnit").createEntityManager(); 
      session = (Session) entity.getDelegate(); 
      if (!geom.getGeometryType().equals("Polygon")) { 
       throw new RuntimeException("Geometry must be a Polygon. Got a " + geom.getGeometryType()); 
      } 
      else { 
       //poly. 
       mLogger.info("GeoType :_: " + geom.getGeometryType()); 
       hibernateEntity = new HibernateSpatialEntity(); 
       random = new Random(); 
       hibernateEntity.setGeoRegionDefinition((Polygon)geom); 
       hibernateEntity.setGeoRegionDescription("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
       regionName = new StringBuilder(); 
       regionName.append("RegionName :_: ").append(random.nextInt(10000)); 
       hibernateEntity.setGeoRegionName(regionName.toString()); 
       mLogger.info("StoredName :_: " + regionName.toString()); 
       session.beginTransaction(); 
       session.save(hibernateEntity); 
      } 

     } catch(Exception exception) { 
      mLogger.error("Exception :_: " + exception.getMessage()); 
     } finally { 
      session.clear(); 
      if ((session != null) && (session.isOpen())) { 
       session.close(); 
      } 
     } 
     mLogger.debug("Exited from the Method :_: " + methodName); 
     return statusResult; 
    } 

    private Geometry wktToGeometry(String wktPoint) { 
     WKTReader fromText = new WKTReader(); 
     Geometry geom = null; 
     try { 
      geom = fromText.read(wktPoint); 
     } catch (ParseException e) { 
      throw new RuntimeException("Not a WKT string:" + wktPoint); 
     } 
     return geom; 
    } 
} 

DATABASE SCHEMA

+------------------------+--------------+------+-----+---------+----------------+ 
| Field     | Type   | Null | Key | Default | Extra   | 
+------------------------+--------------+------+-----+---------+----------------+ 
| PRD_GEO_REGION_ID  | int(11)  | NO | PRI | NULL | auto_increment | 
| OWNER_ID    | decimal(3,0) | NO |  | NULL |    | 
| GEO_REGION_NAME  | varchar(50) | NO | UNI | NULL |    | 
| GEO_REGION_DESCRIPTION | varchar(70) | YES |  | NULL |    | 
| GEO_REGION_DEFINITION | geometry  | YES |  | NULL |    | 
+------------------------+--------------+------+-----+---------+----------------+ 

倫瑞格ds, ArunRaj。

回答

2

通過將註釋放在適當的位置解決了問題。

問題:_:

@Type(type="org.hibernate.spatial.GeometryType") 
private Polygon geoRegionDefinition; 

我已經指定在錯誤的地方類型標註。那就是問題所在。這就是JavaObject無法將數據傳輸到GeometryType的原因。

解決方案:_:

只要把類型標註的實體類的getter方法。問題已得到解決。

@Type(type="org.hibernate.spatial.GeometryType") 
@Column(name = "GEO_REGION_DEFINITION") 
public Polygon getGeoRegionDefinition() { 
    return this.geoRegionDefinition; 
} 

public void setGeoRegionDefinition(Polygon geoRegionDefinition) { 
    this.geoRegionDefinition = geoRegionDefinition; 
}