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 all commits
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
1 change: 1 addition & 0 deletions data-jdbc/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ test-resources:
mssql:
accept-license: true
startup-timeout: 300s
image-name: mcr.microsoft.com/mssql/server:2022-latest
mariadb:
startup-timeout: 300s
mysql:
Expand Down
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
1 change: 1 addition & 0 deletions data-r2dbc/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ test-resources:
mssql:
accept-license: true
startup-timeout: 300s
image-name: mcr.microsoft.com/mssql/server:2022-latest
mariadb:
startup-timeout: 300s
mysql:
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class SqlServerHibernateTransactionSpec extends HibernateTransactionSpec impleme
"datasources.default.name" : "mymssqldb",
'jpa.default.properties.hibernate.hbm2ddl.auto' : 'create-drop',
'jpa.default.properties.hibernate.dialect' : 'org.hibernate.dialect.SQLServerDialect',
'test-resources.containers.mssql.accept-license' : 'true'
'test-resources.containers.mssql.accept-license' : 'true',
'test-resources.containers.mssql.image-name' : 'mcr.microsoft.com/mssql/server:2022-latest'
]
}

Expand Down
Loading