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 {
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")
}

View file

@ -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<User> {
return repo.findAll()
fun index(): List<User> {
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<User> {
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> {
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)
}
}

View file

@ -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<User>
@NotEmpty(message = "Login can't be empty")
@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
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
# 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