Hibernate Spatial和PostGIS主要关键问题

时间:2016-04-12 14:28:53

标签: spring hibernate postgresql postgis hibernate-spatial

我是PostGIS的新手,我正在使用PostGIS和Hibernate Spatial和Spring框架。问题是主键未自动设置,并且在将数据插入数据库时​​出现以下错误:

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

和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>

我将不胜感激任何帮助。如果需要任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

好的,经过一些搜索,这个解决方案有效:

@Id
@Column(name="id", unique=true, nullable=false)
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
private int id;

我还添加了以下hibernate配置:

<beans:prop key="hibernate.format_sql">true</beans:prop>