From 3d5478b3e4d89d7a70327f5d20aa54530730e95e Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Wed, 3 Jan 2024 17:37:44 +0100 Subject: [PATCH] Add panache to hibernate --- build.gradle.kts | 11 ++++++---- .../demo/controllers/UsersController.kt | 21 +++++++------------ .../kotlin/com/example/demo/entities/User.kt | 10 ++++----- .../demo/repositories/UserRepository.kt | 6 ------ src/main/resources/application.properties | 14 +++++++------ 5 files changed, 27 insertions(+), 35 deletions(-) delete mode 100644 src/main/kotlin/com/example/demo/repositories/UserRepository.kt diff --git a/build.gradle.kts b/build.gradle.kts index d889d89..b5ac257 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,14 +20,17 @@ repositories { } 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-web") - implementation("org.springframework.boot:spring-boot-starter-validation") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") 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") testImplementation("org.springframework.boot:spring-boot-starter-test") } diff --git a/src/main/kotlin/com/example/demo/controllers/UsersController.kt b/src/main/kotlin/com/example/demo/controllers/UsersController.kt index d432068..393461e 100644 --- a/src/main/kotlin/com/example/demo/controllers/UsersController.kt +++ b/src/main/kotlin/com/example/demo/controllers/UsersController.kt @@ -1,42 +1,37 @@ package com.example.demo.controllers import com.example.demo.entities.User -import com.example.demo.repositories.UserRepository import jakarta.validation.Valid -import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @RestController class UsersController { - @Autowired - lateinit var repo: UserRepository - @GetMapping("/users") - fun index(): MutableIterable { - return repo.findAll() + fun index(): List { + return User.findAll().list() } @GetMapping("/users/{id}") fun show(@PathVariable id: Long): User? { - return repo.findById(id).get() + return User.findById(id) } @PostMapping("/users") fun create(@Valid @RequestBody newUser: User): ResponseEntity { - val user = repo.save(newUser) - return ResponseEntity(user, HttpStatus.CREATED) + newUser.persistAndFlush() + return ResponseEntity(newUser, HttpStatus.CREATED) } @PatchMapping("/users/{id}") fun update(@PathVariable id: Long, @Valid @RequestBody user: User): ResponseEntity { - user.id = id - return ResponseEntity(repo.save(user), HttpStatus.OK) + user.persist() + return ResponseEntity(user, HttpStatus.OK) } @DeleteMapping("/users/{id}") fun destroy(@PathVariable id: Long) { - return repo.deleteById(id) + User.deleteById(id) } } diff --git a/src/main/kotlin/com/example/demo/entities/User.kt b/src/main/kotlin/com/example/demo/entities/User.kt index 740cc07..b9e752f 100644 --- a/src/main/kotlin/com/example/demo/entities/User.kt +++ b/src/main/kotlin/com/example/demo/entities/User.kt @@ -1,16 +1,14 @@ 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.GeneratedValue -import jakarta.persistence.Id import jakarta.validation.constraints.Email import jakarta.validation.constraints.NotEmpty @Entity -class User { - @Id - @GeneratedValue - var id: Long? = null; +class User : PanacheEntity() { + companion object : PanacheCompanion @NotEmpty(message = "Login can't be empty") @Email(message = "The email address is invalid.") diff --git a/src/main/kotlin/com/example/demo/repositories/UserRepository.kt b/src/main/kotlin/com/example/demo/repositories/UserRepository.kt deleted file mode 100644 index 8cfb644..0000000 --- a/src/main/kotlin/com/example/demo/repositories/UserRepository.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.example.demo.repositories - -import com.example.demo.entities.User -import org.springframework.data.repository.CrudRepository - -interface UserRepository : CrudRepository \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index fe3fff2..a38f6ec 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,8 @@ -spring.datasource.url=jdbc:sqlite:test.db -spring.datasource.driver-class-name=org.sqlite.JDBC -spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect -spring.datasource.username= -spring.datasource.password= -spring.jpa.hibernate.ddl-auto=update \ No newline at end of file +# configure your datasource +quarkus.datasource.db-kind=sqlite +quarkus.datasource.username= +quarkus.datasource.password= +quarkus.datasource.jdbc.url=jdbc:sqlite:///test.db + +# drop and create the database at startup (use `update` to only update the schema) +quarkus.hibernate-orm.database.generation=update \ No newline at end of file