34
34
import io .micronaut .data .connection .ConnectionStatus ;
35
35
import io .micronaut .data .connection .annotation .Connectable ;
36
36
import io .micronaut .data .exceptions .DataAccessException ;
37
+ import io .micronaut .data .exceptions .NonUniqueResultException ;
37
38
import io .micronaut .data .jdbc .config .DataJdbcConfiguration ;
38
39
import io .micronaut .data .jdbc .convert .JdbcConversionContext ;
39
40
import io .micronaut .data .jdbc .mapper .ColumnIndexCallableResultReader ;
@@ -165,6 +166,7 @@ public final class DefaultJdbcRepositoryOperations extends AbstractSqlRepository
165
166
private final ColumnIndexCallableResultReader columnIndexCallableResultReader ;
166
167
private final Map <Dialect , List <SqlExceptionMapper >> sqlExceptionMappers = new EnumMap <>(Dialect .class );
167
168
169
+
168
170
/**
169
171
* Default constructor.
170
172
*
@@ -353,7 +355,8 @@ public <T, R> R findOne(@NonNull PreparedQuery<T, R> pq) {
353
355
}
354
356
355
357
private <T , R > R findOne (Connection connection , SqlPreparedQuery <T , R > preparedQuery ) {
356
- try (PreparedStatement ps = prepareStatement (connection ::prepareStatement , preparedQuery , false , true )) {
358
+ boolean limitToSingleResult = !jdbcConfiguration .isUniqueResultOnFindOne ();
359
+ try (PreparedStatement ps = prepareStatement (connection ::prepareStatement , preparedQuery , false , limitToSingleResult )) {
357
360
preparedQuery .bindParameters (new JdbcParameterBinder (connection , ps , preparedQuery ));
358
361
try (ResultSet rs = ps .executeQuery ()) {
359
362
SqlTypeMapper <ResultSet , R > mapper = createMapper (preparedQuery , ResultSet .class );
@@ -365,12 +368,19 @@ private <T, R> R findOne(Connection connection, SqlPreparedQuery<T, R> preparedQ
365
368
if (rs .next ()) {
366
369
oneMapper .processRow (rs );
367
370
}
368
- while (hasJoins && rs .next ()) {
369
- oneMapper .processRow (rs );
371
+ if (hasJoins ) {
372
+ while (rs .next ()) {
373
+ oneMapper .processRow (rs );
374
+ }
375
+ } else if (jdbcConfiguration .isUniqueResultOnFindOne () && rs .next ()) {
376
+ throw new NonUniqueResultException ("Multiple results found for query: " + preparedQuery .getQuery ());
370
377
}
371
378
result = oneMapper .getResult ();
372
379
} else if (rs .next ()) {
373
380
result = mapper .map (rs , preparedQuery .getResultType ());
381
+ if (jdbcConfiguration .isUniqueResultOnFindOne () && rs .next ()) {
382
+ throw new NonUniqueResultException ("Multiple results found for query: " + preparedQuery .getQuery ());
383
+ }
374
384
} else {
375
385
result = null ;
376
386
}
@@ -720,14 +730,6 @@ public <T> T persist(@NonNull InsertOperation<T> operation) {
720
730
final SqlStoredQuery <T , ?> storedQuery = getSqlStoredQuery (operation .getStoredQuery ());
721
731
JdbcOperationContext ctx = createContext (operation , connection , storedQuery );
722
732
JdbcEntityOperations <T > op = new JdbcEntityOperations <>(ctx , storedQuery , storedQuery .getPersistentEntity (), operation .getEntity (), true );
723
- // RuntimePersistentEntity<T> persistentEntity = getEntity(operation.getRootEntity());
724
- // if (persistentEntity.hasIdentity()) {
725
- // if (!persistentEntity.getIdentity().isAutoPopulated()
726
- // && persistentEntity.getIdentity().getProperty().get(operation.getEntity()) != null) {
727
- // op.update();
728
- // return op;
729
- // }
730
- // }
731
733
op .persist ();
732
734
return op ;
733
735
}, operation .getInvocationContext ()).getEntity ();
0 commit comments