micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

JpaRepository adds "limit 1" to query

Open Stianhn opened this issue 2 years ago • 3 comments

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?

Stianhn avatar Nov 14 '23 23:11 Stianhn

I think it is the intended behavior. You could get all:

fun findAllByRandom(random: String): List<DataEntity>

sdelamo avatar Nov 25 '23 14:11 sdelamo

@dstepanov @radovanradic This is the intended behaviour. is not It?

sdelamo avatar Jan 26 '24 12:01 sdelamo

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.

radovanradic avatar Jan 26 '24 12:01 radovanradic

Would it be useful to clarify this in the documentation?

Stianhn avatar Mar 02 '24 17:03 Stianhn