-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: positional parameters for Micronaut Data JPA #1824
Open
sdelamo
wants to merge
1
commit into
4.7.x
Choose a base branch
from
positionalparams
base: 4.7.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...pa/src/test/groovy/io/micronaut/data/hibernate/querygroupby/ProjecttRepositorySpec.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautProject.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MicronautTask> 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<MicronautTask> getTasks() { | ||
return tasks; | ||
} | ||
|
||
public void setTasks(Set<MicronautTask> tasks) { | ||
this.tasks = tasks; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Project [id=" + id + ", code=" + code + ", name=" + name + ", description=" + description + "]"; | ||
} | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/entities/MicronautTask.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + "]"; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...pa/src/test/java/io/micronaut/data/hibernate/querygroupby/MicronautProjectRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MicronautProject, Long> { | ||
@Query("select p from MicronautProject p where p.name=?1 and p.description=?2") | ||
List<MicronautProject> findWithNameAndDescriptionPositionalBind(String name, String description); | ||
} |
16 changes: 16 additions & 0 deletions
16
data-hibernate-jpa/src/test/java/io/micronaut/data/hibernate/querygroupby/TaskStatus.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fails with: