Add panache to hibernate

This commit is contained in:
Florian RICHER 2024-01-03 17:37:44 +01:00
parent b0467ec6ad
commit 3d5478b3e4
5 changed files with 27 additions and 35 deletions

View file

@ -20,14 +20,17 @@ repositories {
} }
dependencies { dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.xerial:sqlite-jdbc")
implementation("org.hibernate.orm:hibernate-community-dialects") // For DB
implementation("io.quarkus:quarkus-hibernate-orm:3.6.4")
implementation("io.quarkus:quarkus-hibernate-orm-panache-kotlin:3.6.4")
implementation("io.quarkiverse.jdbc:quarkus-jdbc-sqlite:3.0.7")
implementation("io.quarkus:quarkus-hibernate-validator:3.6.4") // Entity validatator
developmentOnly("org.springframework.boot:spring-boot-devtools") developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.boot:spring-boot-starter-test")
} }

View file

@ -1,42 +1,37 @@
package com.example.demo.controllers package com.example.demo.controllers
import com.example.demo.entities.User import com.example.demo.entities.User
import com.example.demo.repositories.UserRepository
import jakarta.validation.Valid import jakarta.validation.Valid
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
@RestController @RestController
class UsersController { class UsersController {
@Autowired
lateinit var repo: UserRepository
@GetMapping("/users") @GetMapping("/users")
fun index(): MutableIterable<User> { fun index(): List<User> {
return repo.findAll() return User.findAll().list()
} }
@GetMapping("/users/{id}") @GetMapping("/users/{id}")
fun show(@PathVariable id: Long): User? { fun show(@PathVariable id: Long): User? {
return repo.findById(id).get() return User.findById(id)
} }
@PostMapping("/users") @PostMapping("/users")
fun create(@Valid @RequestBody newUser: User): ResponseEntity<User> { fun create(@Valid @RequestBody newUser: User): ResponseEntity<User> {
val user = repo.save(newUser) newUser.persistAndFlush()
return ResponseEntity(user, HttpStatus.CREATED) return ResponseEntity(newUser, HttpStatus.CREATED)
} }
@PatchMapping("/users/{id}") @PatchMapping("/users/{id}")
fun update(@PathVariable id: Long, @Valid @RequestBody user: User): ResponseEntity<User> { fun update(@PathVariable id: Long, @Valid @RequestBody user: User): ResponseEntity<User> {
user.id = id user.persist()
return ResponseEntity(repo.save(user), HttpStatus.OK) return ResponseEntity(user, HttpStatus.OK)
} }
@DeleteMapping("/users/{id}") @DeleteMapping("/users/{id}")
fun destroy(@PathVariable id: Long) { fun destroy(@PathVariable id: Long) {
return repo.deleteById(id) User.deleteById(id)
} }
} }

View file

@ -1,16 +1,14 @@
package com.example.demo.entities package com.example.demo.entities
import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanion
import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity
import jakarta.persistence.Entity import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.Id
import jakarta.validation.constraints.Email import jakarta.validation.constraints.Email
import jakarta.validation.constraints.NotEmpty import jakarta.validation.constraints.NotEmpty
@Entity @Entity
class User { class User : PanacheEntity() {
@Id companion object : PanacheCompanion<User>
@GeneratedValue
var id: Long? = null;
@NotEmpty(message = "Login can't be empty") @NotEmpty(message = "Login can't be empty")
@Email(message = "The email address is invalid.") @Email(message = "The email address is invalid.")

View file

@ -1,6 +0,0 @@
package com.example.demo.repositories
import com.example.demo.entities.User
import org.springframework.data.repository.CrudRepository
interface UserRepository : CrudRepository<User, Long>

View file

@ -1,6 +1,8 @@
spring.datasource.url=jdbc:sqlite:test.db # configure your datasource
spring.datasource.driver-class-name=org.sqlite.JDBC quarkus.datasource.db-kind=sqlite
spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect quarkus.datasource.username=
spring.datasource.username= quarkus.datasource.password=
spring.datasource.password= quarkus.datasource.jdbc.url=jdbc:sqlite:///test.db
spring.jpa.hibernate.ddl-auto=update
# drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation=update