Skip to content
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

Fix update by field when entity parameter is present #3140

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ protected <T> Predicate interceptPredicate(MethodMatchContext matchContext,
final SourcePersistentEntity rootEntity = (SourcePersistentEntity) root.getPersistentEntity();
Predicate predicate = null;
if (entityParameter != null) {
if (rootEntity.getVersion() != null) {
if (rootEntity.getVersion() != null && existingPredicate == null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears this is used for void update(Entity entity) too but also for void updateByField(String field, Entity entity) so when there is existing predicate with entity parameter we shouldn't add id and version criteria.

predicate = cb.and(
cb.equal(root.id(), cb.entityPropertyParameter(entityParameter, new PersistentPropertyPath(rootEntity.getIdentity()))),
cb.equal(root.version(), cb.entityPropertyParameter(entityParameter, new PersistentPropertyPath(rootEntity.getVersion())))
);
} else {
} else if (existingPredicate == null) {
predicate = cb.equal(root.id(), cb.entityPropertyParameter(entityParameter, new PersistentPropertyPath(rootEntity.getIdentity())));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.micronaut.data.intercept.async.UpdateAsyncInterceptor
import io.micronaut.data.intercept.reactive.UpdateReactiveInterceptor
import io.micronaut.data.model.DataType
import io.micronaut.data.processor.visitors.AbstractDataSpec
import io.micronaut.data.tck.entities.Person
import spock.lang.PendingFeature
import spock.lang.Unroll

Expand Down Expand Up @@ -186,6 +187,28 @@ interface PersonRepository extends CrudRepository<Person, Long> {
}


void "test update by field with entity parameter"() {
given:
def repository = buildRepository('test.PersonRepository', """
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.GenericRepository;
import io.micronaut.data.tck.entities.Person;

@JdbcRepository(dialect = Dialect.MYSQL)
interface PersonRepository extends GenericRepository<Person, Long> {

void updateByName(String name, Person person);
}
""")

def method = repository.findMethod("updateByName", String, Person).get()
def updateQuery = getQuery(method)

expect:
updateQuery == 'UPDATE `person` SET `name`=?,`age`=?,`enabled`=?,`income`=? WHERE (`name` = ?)'
}

void "test AutoGenerated update method"() {
given:
def repository = buildRepository('test.StudentRepository', """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,17 @@ abstract class AbstractRepositorySpec extends Specification {
then:
personRepository.findByName("Jack") == null
personRepository.findByName("Jeffrey").age == 30

when:"Update by using entity with null id"
def jeffrey = personRepository.findByName("Jeffrey")
def initialAge = jeffrey.age
jeffrey.id = null
jeffrey.age = 31
personRepository.updateByName("Jeffrey", jeffrey)
def updatedJeffrey = personRepository.findByName("Jeffrey")
then:"Entity is updated"
initialAge == 30
updatedJeffrey.age == 31
}

void "test update by multiple fields"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public interface PersonRepository extends CrudRepository<Person, Long>, Pageable

long updateByName(String name, int age);

void updateByName(String name, Person person);

List<Person> list(Pageable pageable);

int count(String name);
Expand Down
Loading