问题描述
我正在使用我的第一个 Spring Boot + Spring数据JPA + Hibernate 5 在 PostgreSQL 数据库。
我试图将 point 的字段映射为数据类型(所以我使用 Hibernate Spatial ,它原生包含在Hibernate 5中。
所以我有以下情况:
以下简单的数据库表名为cities:
字段类型
------------ -------------------------------------------------- -
id bigint(它是PK)
名称字符变化
位置点(它包含坐标)
因此,例如这张表包含以下记录:
id名称位置
--------------------------- -----------
1旧金山(-194,53)
然后,我有用于映射以前城市数据库表的城市实体类:
import com.vividsolutions.jts.geom.Point;
import org.hibernate.annotations.Type;
import javax.persistence。*;
import java.io.Serializable;
@Entity
@Table(name =cities)
public class Cities实现Serializable {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY)
@Column(name =id)
private Long id;
私人字符串名称;
@Column(name =location,columnDefinition =Point)
私人点位置;
public Cities(){
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Point getLocation(){
return location;
}
public void setLocation(Point location){
this.location = location;
我正在使用 Point 类: com.vividsolutions.jts.geom.Point ,因为我在某个教程中找到它,但我不确定它是否正确,因为我也可以选择其他实现:
org.geolatte.geom.Point
org.springframework.data.geo.Point
$ c $然后我有 Spring Data JPA 接口表示我的DAO类:
$ b $ p
@Repository
public interface CitiesDAO扩展JpaRepository< Cities,Long> {
城市findById(@Param(id)Long id);
}
您可以看到它扩展了 JpaRepository 方法签名实现我的查询(使用JPA)。
因此,在我的JUnit测试类中,我实现了这种测试方法,我试图插入一条新记录到数据库中的 cities 表:
@Test
public void testCitiesDAO(){
System.out.println(testCitiesDAO()START);
//城市city = citiesDAO.findById(1L);
GeometryFactory geometryFactory = new GeometryFactory();
坐标坐标=新坐标();
coordinate.x = 2;
coordinate.y = 5;
点myPoint = geometryFactory.createPoint(coordinate);
城市罗马=新城市();
rome.setId(new Long(2));
rome.setName(Rome);
rome.setLocation(myPoint);
citiesDAO.save(rome);
System.out.println(testCitiesDAO()END);
$ p
$ b $ p所以我创建了persiste对象(城市罗马)至此我设置了一个全新的 Point 对象,并且我试图通过以下行来坚持它:
citiesDAO.save(罗马);
问题是,当尝试执行保存(罗马)方法引发这个例外:
Hibernate:选择cities0_.id作为id1_0_0_,cities0_.location作为location2_0_0_,cities0_.name作为name3_0_0_ from城市cities0_ where cities0_.id =?
Hibernate:插入城市(位置,名称)值(?,?)
2016-10-29 20:08:09.509错误7956 --- [main] ohengine.jdbc.spi.SqlExceptionHelper :错误:列位置是类型点,但表达式是类型bytea
提示:您将需要重写或转换表达式。
职位:45
org.springframework.dao.InvalidDataAccessResourceUsageException:无法执行语句; SQL [n / a];嵌套的例外是org.hibernate.exception.SQLGrammarException:在org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261)
。在组织无法执行的语句
.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
at org.springframework .dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
在org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
在org.springframework.dao.support .PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(Reflecti veMethodInvocation.java:179)
处org.springframework.aop.framework.ReflectiveMethodInvocation org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
。继续(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation。
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy $ Proxy105.save(Unknown Source)
(Native方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessor Impl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
。在组织。 springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunne r.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild( SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)
at org。 junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework .org.junit.runners.parentRunner.run(ParentRunner.java:363)
org.springframewor k.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4。 JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart( JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke( Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
引起:org.hibernate.exception.SQLGrammarExcepti on:无法执行语句
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java: 42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java: 95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2803)
在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractE ntityPersister.java:3374)
在org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
在org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:在org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:273 619)
)
。在org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:254)$ b在org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:299)
$ b在org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
。在组织.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event .internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
在org.hibernate.jpa。 event.internal.core.JpaMergeEventListener.saveWithGeneratedId(JpaMergeEventListener.java:56)
位于org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:255)
位于org.hibernate.event。 internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:235)
处org.hibernate.event.internal.DefaultMergeEventListener org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:301)
。 onMerge(DefaultMergeEventListener.java:170)
在org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
在org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java: 840)
在org.hibernate.internal.SessionImpl.merge(SessionImpl.java:822)
在org.hibernate.internal.SessionImpl.merge(SessionImpl.java:827)
在org .hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.j ava:1161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl。调用(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator。
at com.sun.proxy。$ Proxy101.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:509)在sun.reflect.NativeMethodAccessorImpl.invoke0
(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl。 java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframe work.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488 )
在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java :179)在org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61
)
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
在org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)
在org.springfram ework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop。 framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 41 more
引起:org.postgresql.util.PSQLException:错误:列位置是类型点的,但表达式的类型是bytea
提示:您将需要重写或转换表达式。
档:45
在org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
在org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java :1795)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
。在org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367)
在org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:321)
。在org.hibernate.engine。 jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 86 more
pre $ 由于:org.postgresql.util.PSQLException:错误:columnlocation是类型点,但是表达式是类型bytea
所以它似乎试图将具有点的表的位置字段错误地放入数据类型中。为什么它不能将 com.vividsolutions.jts.geom.Point 对象用于实体类?
这是我的 application.propertis 文件,这是我应用程序中的唯一配置文件:
#No auth保护
endpoints.shutdown.sensitive = true
#启用关闭端点
endpoints.shutdown.enabled = true
logging.file = BeTriviusController.log
logging.level.org.springframework.web = DEBUG
logging.level.org.hibernate =错误
#Thymeleaf
spring.thymeleaf.cache:false
#DATABASE CONFIG ------------------------------------------------- -------------------------------------------------- -
spring.datasource.url = jdbc:postgresql:// localhost:5432 / test1
spring.datasource.username = postgres
spring.datasource.password = Bl4tte_Te4m
spring。 datasource.driver-class-name = org.postgresql.Driver
spring.jpa.properties.hibernate.current_session_context_ class = org.springframework.orm.hibernate5.SpringSessionContext
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
#显示与否记录每个sql查询
spring.jpa.show-sql = true
#Hibernate ddl auto(创建,创建 - 删除,更新,验证)
spring.jpa.hibernate .ddl-auto =验证
#spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisDialect
正如您所看到的,我已将 spring.jpa.hibernate.naming-strategy 改为使用Hibernate空间的 PostgisDialect 方言。
什么是worng?我错过了什么?如何解决这个问题?
解决方案我使用Hibernate- {core,spatial} v5.2.9一切都如上,除了在 application.properties
中设置方言外。我需要:
spring.jpa.database-platform = org.hibernate.spatial.dialect.postgis.PostgisDialect
现在可以使用了。我的实体中甚至不需要任何@Column注释。
I am working on my first Spring Boot + Spring Data JPA + Hibernate 5 working on a PostgreSQL database.
I have the following problem trying to map a field having point as data type (so I am using Hibernate Spatial that is natively included into Hibernate 5.
So I have the following situation:
I have the following simple database table named cities:
Field Type
----------------------------------------------------------------
id bigint (it is the PK)
name character varying
location point (it contains the coordinates)
So for example this table contains this record:
id name locoation
--------------------------------------
1 San Francisco (-194,53)
Then I have this Cities entity class that map the previous cities database table:
import com.vividsolutions.jts.geom.Point;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "cities")
public class Cities implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private String name;
@Column(name = "location", columnDefinition="Point")
private Point location;
public Cities() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
}
I am using this implementation of the Point class: com.vividsolutions.jts.geom.Point because I found it on some tutorial but I am not sure if it is correct because I also can chose these other implementations:
org.geolatte.geom.Point
org.springframework.data.geo.Point
Then I have this Spring Data JPA interface that represent my DAO class:
@Repository
public interface CitiesDAO extends JpaRepository<Cities, Long> {
Cities findById(@Param("id") Long id);
}
As you can see it extends JpaRepository and the method signature "implements" my queries (using JPA).
So, into my JUnit test class I implemented this test method in which I am trying to insert a new record into the cities table on the database:
@Test
public void testCitiesDAO() {
System.out.println("testCitiesDAO() START");
//Cities city = citiesDAO.findById(1L);
GeometryFactory geometryFactory = new GeometryFactory();
Coordinate coordinate = new Coordinate();
coordinate.x = 2;
coordinate.y = 5;
Point myPoint = geometryFactory.createPoint(coordinate);
Cities rome = new Cities();
rome.setId(new Long(2));
rome.setName("Rome");
rome.setLocation(myPoint);
citiesDAO.save(rome);
System.out.println("testCitiesDAO() END");
}
So I am creating the object to persiste (Cities rome) on wich I set a brand new Point object and I am trying to persist it by this line:
citiesDAO.save(rome);
The problem is that when try to perform the save(rome) method this excepetion is thrown:
Hibernate: select cities0_.id as id1_0_0_, cities0_.location as location2_0_0_, cities0_.name as name3_0_0_ from cities cities0_ where cities0_.id=?
Hibernate: insert into cities (location, name) values (?, ?)
2016-10-29 20:08:09.509 ERROR 7956 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: column "location" is of type point but expression is of type bytea
Hint: You will need to rewrite or cast the expression.
Position: 45
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy105.save(Unknown Source)
at com.betriuvis.controller.test.PlaceSearcherControllerTest.testCitiesDAO(PlaceSearcherControllerTest.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2803)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3374)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:619)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:273)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:254)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:299)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
at org.hibernate.jpa.event.internal.core.JpaMergeEventListener.saveWithGeneratedId(JpaMergeEventListener.java:56)
at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:255)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:235)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:301)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:840)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:822)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:827)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy101.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:509)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 41 more
Caused by: org.postgresql.util.PSQLException: ERROR: column "location" is of type point but expression is of type bytea
Hint: You will need to rewrite or cast the expression.
Position: 45
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:321)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 86 more
So basically the error message is:
Caused by: org.postgresql.util.PSQLException: ERROR: column "location" is of type point but expression is of type bytea
So it seems that is trying to put something wrong into the location field of the table having point as data type. Why it can't convert the com.vividsolutions.jts.geom.Point object used into the entity class?
This is my application.propertis file, the only configuration file in my application:
#No auth protected
endpoints.shutdown.sensitive=true
#Enable shutdown endpoint
endpoints.shutdown.enabled=true
logging.file=BeTriviusController.log
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
# Thymeleaf
spring.thymeleaf.cache:false
# DATABASE CONFIG ----------------------------------------------------------------------------------------------------
spring.datasource.url = jdbc:postgresql://localhost:5432/test1
spring.datasource.username = postgres
spring.datasource.password = Bl4tte_Te4m
spring.datasource.driver-class-name = org.postgresql.Driver
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update, validate)
spring.jpa.hibernate.ddl-auto = validate
#spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
As you can see I have set the spring.jpa.hibernate.naming-strategy to the PostgisDialect dialect to use Hibernate spatial.
What is worng? What am I missing? How can I fix this issue?
解决方案 I was using Hibernate-{core,spatial} v5.2.9 everything was as is above, except from how dialect was being set in application.properties
. I NEEDED:
spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisDialect
Now it works. I don't need even any @Column annotation in my Entity.
这篇关于为什么我无法在数据库的Point字段上映射实体类的Point字段?列“位置”是类型点,但表达式是bytea类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!