【发布时间】:2016-10-28 15:07:08
【问题描述】:
我正在尝试在概念验证项目中使用带有 springboot 1.4 的 GORM6 并遇到问题。完整的项目代码是available here。
我使用 bootRun 运行项目并运行 curl
$ curl localhost:8080/persons/ -X PUT -d "lastName=doe&firstName=John"
当尝试将域类保存在从我的 rest 控制器调用的服务中时,hibernate/gorm 抛出 IllegalArgumentException
Servlet.service() 用于 servlet [dispatcherServlet] 在上下文中的路径 [] 抛出异常 [请求处理失败;嵌套异常是 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: object is not an 声明类的实例
真正奇怪的是,我有一个调用相同服务并通过的测试,所以我不确定我的 RestController/Service 是否设置不正确。
主要:
@SpringBootApplication
@EnableTransactionManagement
class Main {
public static void main(String[] args) {
SpringApplication.run(Main, args);
}
}
模型类:
@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)
}
}
最后我的测试通过了,这基本上与我的休息控制器中的 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
}
}
任何见解将不胜感激。
【问题讨论】:
-
不确定休眠。但可以肯定的是,
PersonService的方法似乎有问题。get(id)不是Person类的static方法。 -
这很时髦,GORM 动态地将这些方法添加到注释为 @Entity 的对象上。
-
id来自哪里?还是它也来自@Entity? -
是的 id 也来自@Entity
标签: hibernate groovy spring-boot grails-orm