JpaRepository adds "limit 1" to query
Expected Behavior
When the return type of the query is a single object and there are multiple objects in the db I would expect the app to throw an exception.
Actual Behaviour
It adds a limit to the query and returns the first object.
Steps To Reproduce
@Controller
class Controller(private val dataRepository: ARepository) {
@Get("/")
fun get(): DataEntity {
return dataRepository.findByRandom("test")
}
}
@Repository
interface ARepository : JpaRepository<DataEntity, Long> {
fun findByRandom(random: String): DataEntity
}
@Serdeable
@Entity
data class DataEntity(@Id @GeneratedValue val id: Long, val random: String)
Results in query like this
select d1_0.id,d1_0.random from data_entity d1_0 where (d1_0.random="test") limit 1
Environment Information
No response
Example Application
No response
Version
4.0.4
If this is intended behavior, is there a way to configure this?
I think it is the intended behavior. You could get all:
fun findAllByRandom(random: String): List<DataEntity>
@dstepanov @radovanradic This is the intended behaviour. is not It?
Yes. The user probably expects the behavior like this https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#uniqueResult() but don't think it worked that way in Micronaut and we shouldn't change the behavior.
Would it be useful to clarify this in the documentation?