Hibernate 使用Gorm6和;Springboot 1.4

Hibernate 使用Gorm6和;Springboot 1.4,hibernate,groovy,spring-boot,gorm,Hibernate,Groovy,Spring Boot,Gorm,我试图在一个概念验证项目中使用GORM6和springboot 1.4,但遇到了一个问题。完整的项目代码是 我使用bootRun和runcurl运行项目 $curl localhost:8080/persons/-X PUT-d“lastName=doe&firstName=John” 当尝试在从我的rest控制器调用的服务中保存域类时,hibernate/gorm抛出了一个IllegalArgumentException Servlet[dispatcherServlet]在具有路径的上下文中

我试图在一个概念验证项目中使用GORM6和springboot 1.4,但遇到了一个问题。完整的项目代码是

我使用bootRun和runcurl运行项目

$curl localhost:8080/persons/-X PUT-d“lastName=doe&firstName=John”

当尝试在从我的rest控制器调用的服务中保存域类时,hibernate/gorm抛出了一个IllegalArgumentException

Servlet[dispatcherServlet]在具有路径的上下文中的Servlet.service() []引发异常[请求处理失败;嵌套异常为 org.springframework.orm.hibernate5.HibernateSystemException: 调用的getter时发生IllegalArgumentException gorm.springboot.demo.model.Person.id;嵌套异常为 org.hibernate.PropertyAccessException:IllegalArgumentException 使用调用gorm.springboot.demo.model.Person.id]的getter时发生 根本原因java.lang.IllegalArgumentException:对象不是 声明类的实例

真正奇怪的是,我有一个调用同一服务并通过的测试,所以我不确定我的RestController/服务是否设置错误

主要内容:

模型类:

@Entity
class Person {
    String lastName
    String firstName
}
控制器等级:

@RestController()
@RequestMapping("/persons")
@Slf4j
class PersonController {

    @Autowired
    PersonService personService

    @RequestMapping(value="/", method = RequestMethod.GET)
    def list() {
        return personService.getAllPersons()
    }

    @RequestMapping(value="/", method= RequestMethod.PUT)
    def add(@RequestParam String lastName,@RequestParam String firstName) {
        log.info("received request for l=${lastName}, f=${firstName}")
        personService.save(new Person(lastName:lastName, firstName:firstName))

    }
}
服务类别:

@Service('personService')
@Transactional
class PersonService {

    def getAllPersons() {
        return Person.list()
    }

    def save(Person p) {
        p.save(true)
    }

    def getPerson(long id) {
        return Person.get(id)
    }
}
最后,我的测试通过了,这与我的rest控制器中的PUT方法基本上是一样的:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
class PersonSpec extends Specification {

    @Autowired PersonService personService

    def 'Persist a person'() {
        when: "Person to add"
        def p = new Person(lastName:"Rizzo", firstName:"Anthony")

        then: "Persist that person"
        personService.save(p)

        expect: "Person to be saved"
        personService.getAllPersons().size() > 0
        personService.getPerson(p.id).lastName == p.lastName
    }
}

如果您有任何见解,我们将不胜感激。

6.0.4的文档确定了该问题。使用springboot+gorm时需要添加此注释

@JsonIgnoreProperties(['dirtyPropertyNames','errors','dirty','attached','version'])


此外,SpringBootDevTools在Hibernate5中存在一个持续的问题。我在github上更新了我的项目,使之成为一个工作版本。

6.0.4的文档发现了这个问题。使用springboot+gorm时需要添加此注释

@JsonIgnoreProperties(['dirtyPropertyNames','errors','dirty','attached','version'])


此外,SpringBootDevTools在Hibernate5中存在一个持续的问题。我在github上更新了我的项目,使之成为一个工作版本。

不确定hibernate。但肯定的是,
PersonService
的方法似乎有问题
get(id)
不是
Person
类的
static
方法。它是groovy,GORM在注释为@Entity的对象上动态添加这些方法。
id
来自哪里?或者它也是来自
@Entity
?是的id也是来自@Entity不确定hibernate。但肯定的是,
PersonService
的方法似乎有问题
get(id)
不是
Person
类的
static
方法。它是groovy,GORM在注释为@Entity的对象上动态添加这些方法。
id
来自哪里?或者它也来自
@Entity
?是的id也来自@Entity
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
class PersonSpec extends Specification {

    @Autowired PersonService personService

    def 'Persist a person'() {
        when: "Person to add"
        def p = new Person(lastName:"Rizzo", firstName:"Anthony")

        then: "Persist that person"
        personService.save(p)

        expect: "Person to be saved"
        personService.getAllPersons().size() > 0
        personService.getPerson(p.id).lastName == p.lastName
    }
}