Spring Hibernate Spatial和PostGIS主要关键问题
我是PostGIS新手,我正在使用带有Hibernate Spatial和Spring框架的PostGIS。问题是主键没有自动设置,在将数据插入数据库时出现以下错误:Spring Hibernate Spatial和PostGIS主要关键问题,spring,hibernate,postgresql,postgis,hibernate-spatial,Spring,Hibernate,Postgresql,Postgis,Hibernate Spatial,我是PostGIS新手,我正在使用带有Hibernate Spatial和Spring框架的PostGIS。问题是主键没有自动设置,在将数据插入数据库时出现以下错误: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: ERROR: null
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: ERROR: null value in column "id" violates not-null constraint
Detail: Failing row contains (null,name , country).
我已经在MySQL上测试了代码,它工作正常。然而,当使用Hibernate Spatial和PostGIS时,它给了我提到的错误。模型如下:
@Entity
@Table(name="person")
public class Person {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
private String country;
...
}
以下是插入数据的代码:
public void addPerson(Person p) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(p);
}
以下是Maven中的依赖项:
postgis-jdbc: 1.5.2
postgresql: 8.4-702.jdbc3
hibernate-entitymanager: 4.0.0.Final
hibernate-core: 4.0.0.Final
hibernate-spatial: 4.0
commons-dbcp: 1.4
spring version: 4.0.3.RELEASE
和休眠配置:
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>it.polimi.thesis.model.Person</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.ddl-auto">update</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:prop key="hibernate.format_sql">true</beans:prop>
it.polimi.thesis.model.Person
org.hibernate.spatical.dial.postgis.postgisdial
真的
更新
我将感谢任何帮助。如果需要任何其他信息,请告诉我。好的,经过一些搜索后,此解决方案有效:
@Id
@Column(name="id", unique=true, nullable=false)
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
private int id;
我还添加了以下hibernate配置:
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>it.polimi.thesis.model.Person</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.ddl-auto">update</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:prop key="hibernate.format_sql">true</beans:prop>
true