From a362b4298a85f78cebfad36444f1c9638c4fcb8a Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Mon, 7 Nov 2022 10:24:30 +0100 Subject: [PATCH] feat: positional parameters --- .../ProjecttRepositorySpec.groovy | 34 +++++ .../hibernate/entities/MicronautProject.java | 101 ++++++++++++++ .../hibernate/entities/MicronautTask.java | 130 ++++++++++++++++++ .../MicronautProjectRepository.java | 14 ++ .../hibernate/querygroupby/TaskStatus.java | 16 +++ 5 files changed, 295 insertions(+) create mode 100644 data-hibernate-jpa/src/test/groovy/io/micronaut/data/hibernate/querygroupby/ProjecttRepositorySpec.groovy create mode 100644 data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautProject.java create mode 100644 data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautTask.java create mode 100644 data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/MicronautProjectRepository.java create mode 100644 data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/TaskStatus.java diff --git a/data-hibernate-jpa/src/test/groovy/io/micronaut/data/hibernate/querygroupby/ProjecttRepositorySpec.groovy b/data-hibernate-jpa/src/test/groovy/io/micronaut/data/hibernate/querygroupby/ProjecttRepositorySpec.groovy new file mode 100644 index 0000000000..be5ed8e518 --- /dev/null +++ b/data-hibernate-jpa/src/test/groovy/io/micronaut/data/hibernate/querygroupby/ProjecttRepositorySpec.groovy @@ -0,0 +1,34 @@ +package io.micronaut.data.hibernate.querygroupby + +import io.micronaut.context.annotation.Property +import io.micronaut.data.hibernate.entities.MicronautProject +import io.micronaut.test.extensions.spock.annotation.MicronautTest +import jakarta.inject.Inject +import spock.lang.PendingFeature +import spock.lang.Specification + +@MicronautTest(startApplication = false, packages = "io.micronaut.data.hibernate.entities") +@Property(name = "datasources.default.name", value = "mydb") +@Property(name = 'jpa.default.properties.hibernate.hbm2ddl.auto', value = 'create-drop') +class ProjecttRepositorySpec extends Specification { + + @Inject + MicronautProjectRepository projectRepository + + @PendingFeature + void "@Query with positional parameters"() { + given: + MicronautProject p1 = projectRepository.save(new MicronautProject("P1", "Project 1", "Description of Project 1")) + MicronautProject p2 = projectRepository.save(new MicronautProject("P2", "Project 2", "Description of Project 2")) + MicronautProject p3 = projectRepository.save(new MicronautProject("P3", "Project 3", "Description of Project 3")) + + expect: + 1 == projectRepository.findWithNameAndDescriptionPositionalBind("P2", "Project 2").size() + + cleanup: + projectRepository.delete(p3) + projectRepository.delete(p2) + projectRepository.delete(p1) + } + +} diff --git a/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautProject.java b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautProject.java new file mode 100644 index 0000000000..10b3dc4fa0 --- /dev/null +++ b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautProject.java @@ -0,0 +1,101 @@ +package io.micronaut.data.hibernate.entities; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +public class MicronautProject { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @Column(unique = true, nullable = false, updatable = false) + private String code; + private String name; + private String description; + @OneToMany(mappedBy = "project", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL) + private Set tasks = new HashSet<>(); + public MicronautProject() { + } + + public MicronautProject(String code, String name, String description) { + this.code = code; + this.name = name; + this.description = description; + } + + @Override + public int hashCode() { + return Objects.hashCode(code); + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MicronautProject other = (MicronautProject) obj; + if (code == null) { + if (other.code != null) + return false; + } else if (!code.equals(other.code)) + return false; + return true; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Set getTasks() { + return tasks; + } + + public void setTasks(Set tasks) { + this.tasks = tasks; + } + + @Override + public String toString() { + return "Project [id=" + id + ", code=" + code + ", name=" + name + ", description=" + description + "]"; + } + +} diff --git a/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautTask.java b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautTask.java new file mode 100644 index 0000000000..d1a32daee0 --- /dev/null +++ b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautTask.java @@ -0,0 +1,130 @@ +package io.micronaut.data.hibernate.entities; + +import io.micronaut.data.hibernate.querygroupby.TaskStatus; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import java.time.LocalDate; +import java.util.Objects; +import java.util.UUID; + +@Entity +public class MicronautTask { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @Column(unique = true, nullable = false, updatable = false) + private String uuid = UUID.randomUUID().toString(); + private String name; + private String description; + private LocalDate dueDate; + + private TaskStatus status; + + @ManyToOne(optional = false) + private MicronautProject project; + + + public MicronautTask() { + } + public MicronautTask(String name, String description, LocalDate dueDate, MicronautProject project) { + this(name, description, dueDate, project, TaskStatus.TO_DO); + } + + public MicronautTask(String name, String description, LocalDate dueDate, MicronautProject project, TaskStatus status) { + this.name = name; + this.description = description; + this.dueDate = dueDate; + this.status = status; + this.project = project; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MicronautTask other = (MicronautTask) obj; + if (uuid == null) { + if (other.uuid != null) + return false; + } else if (!uuid.equals(other.uuid)) + return false; + return true; + } + @Override + public int hashCode() { + return Objects.hash(uuid); + } + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LocalDate getDueDate() { + return dueDate; + } + + public void setDueDate(LocalDate dueDate) { + this.dueDate = dueDate; + } + + public TaskStatus getStatus() { + return status; + } + + public void setStatus(TaskStatus status) { + this.status = status; + } + + public MicronautProject getProject() { + return project; + } + + public void setProject(MicronautProject project) { + this.project = project; + } + + + @Override + public String toString() { + return "Task [id=" + id + ", name=" + name + ", description=" + description + ", dueDate=" + dueDate + ", status=" + status + ", project=" + project + "]"; + } + +} diff --git a/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/MicronautProjectRepository.java b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/MicronautProjectRepository.java new file mode 100644 index 0000000000..70403c23a6 --- /dev/null +++ b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/MicronautProjectRepository.java @@ -0,0 +1,14 @@ +package io.micronaut.data.hibernate.querygroupby; + +import io.micronaut.data.annotation.Query; +import io.micronaut.data.annotation.Repository; +import io.micronaut.data.hibernate.entities.MicronautProject; +import io.micronaut.data.repository.CrudRepository; + +import java.util.List; + +@Repository +public interface MicronautProjectRepository extends CrudRepository { + @Query("select p from MicronautProject p where p.name=?1 and p.description=?2") + List findWithNameAndDescriptionPositionalBind(String name, String description); +} diff --git a/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/TaskStatus.java b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/TaskStatus.java new file mode 100644 index 0000000000..daef995f80 --- /dev/null +++ b/data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/TaskStatus.java @@ -0,0 +1,16 @@ +package io.micronaut.data.hibernate.querygroupby; + +public enum TaskStatus { + TO_DO("To Do"), + IN_PROGRESS("In Progress"), + ON_HOLD("On Hold"), + DONE("Done"); + private final String label; + + private TaskStatus(String label) { + this.label = label; + } + public String getLabel() { + return label; + } +}