【问题标题】:Hibernate Spatial and PostGIS Primary Key IssueHibernate 空间和 PostGIS 主键问题
【发布时间】:2016-04-12 14:28:52
【问题描述】:

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

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>

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

【问题讨论】:

    标签: spring hibernate postgresql postgis hibernate-spatial


    【解决方案1】:

    好的,经过一番搜索,这个解决方案奏效了:

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

    我还添加了以下休眠配置:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多