Created
April 4, 2014 08:21
-
-
Save graemerocher/9970349 to your computer and use it in GitHub Desktop.
Using GORM for Hibernate 4 in Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab("org.grails:gorm-hibernate4-spring-boot:1.0.0.RC2") | |
@Grab("com.h2database:h2:1.3.173") | |
import grails.persistence.* | |
import org.springframework.http.* | |
import static org.springframework.web.bind.annotation.RequestMethod.* | |
@RestController | |
class GreetingController { | |
@RequestMapping(value="/person/greet", method = GET) | |
String greet(String firstName) { | |
def p = Person.findByFirstName(firstName) | |
return p ? "Hello ${p.firstName}!" : "Person not found" | |
} | |
@RequestMapping(value = '/person/add', method = POST) | |
ResponseEntity addPerson(String firstName) { | |
Person.withTransaction { | |
def p = new Person(firstName: firstName).save() | |
return new ResponseEntity( p ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST) | |
} | |
} | |
} | |
@Entity | |
class Person { | |
String firstName | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Graeme!
Call me dumb, but should ResponseEntity, HttpStatus be with[In]Transaction?