【问题标题】:is it possible to use GORM with Spring Data JPA and Rest?是否可以将 GORM 与 Spring Data JPA 和 Rest 一起使用?
【发布时间】:2014-12-18 08:35:42
【问题描述】:

我对“Groovy 世界”有点陌生,我刚刚开始使用该语言来理解语法和特征。以前我在 Java 和 Spring Boot 方面做了很多工作,由于 Pivotal 和 Groovy 和 Grails 一样支持最后一个,所以我想知道是否可以在 Spring Boot 中使用 Groovy 而没有问题。

它很好用(很明显),但是当我发现 GORM 的语法和它周围的 DSL 时,我开始使用它。不幸的是,它似乎不太适合 Spring Data JPA 和 Rest,因为我试图做这样的事情:

@grails.persistence.Entity
class Person {
    BigInteger id
    String firstName
    String lastName

    static mapping { table name: 'people' }
    static constraints { /* whatever */ }
}

@RepositoryRestResource(collectionResourceRel = "people", path = "/api/people")
interface PersonRepository extends JpaRepository<Person, BigInteger> { }

但是 Spring Boot 无法运行,因为它使用以下语句引发异常

IllegalArgumentException: Not a managed bean Person

另一方面,如果我使用 JPA 注释修复该实体(然后通过删除 Grails 的注释),我完全没有问题。

有什么解决办法吗?是否可以通过 Spring Data JPA 和 Rest 以某种方式使用 GORM,因为 GORM 无论如何都依赖于 Hibernate?

【问题讨论】:

  • 经过一番研究,我发现不可能以我想要的方式使用 GORM,因为 Grails 与 Spring Boot 不完全兼容,我需要等待它的 3.0 版本。事实上,下一个版本的 Grails 将基于 Spring Boot(据我所知),所以我需要耐心等待 :)
  • 现在 grails3 可以做到吗?
  • 坦白说我还没有尝试过,因为我完全放弃了 Groovy,不是因为 GORM 或其他原因,而是因为 Pivotal 决定不再支持该语言。这大大减慢了它的开发速度,仍然没有关于完整的 Java 8 支持的消息(默认方法,lambda - 是的,我知道 Groovy 有闭包,但它不一样 - 等等......)。主观决定。无论如何,如果您真的需要 GORM,只需使用 Grails 3,因为它是在 Spring Boot 上开发的。

标签: grails grails-orm spring-boot spring-data-rest


【解决方案1】:

您必须在 application.propertiesapplication.yml 中定义属性 hibernate.packages 以及与实体打包的路径。示例:

database:
  driver: org.postgresql.Driver
  url: jdbc:postgresql://localhost:5432/retail
  username: postgres
  password: postgres
hibernate:
  packages: ru.rd.core.analytic.dto.jpa
  dialect: org.hibernate.dialect.PostgreSQL94Dialect
  show_sql: true
  hbm2ddl.auto: validate #none validate create-drop update

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 2012-06-01
    • 2014-07-04
    • 2014-03-10
    • 2017-05-20
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多