-
-
Notifications
You must be signed in to change notification settings - Fork 268
Closed
Description
only non-aggregation clauses from the select string should be added to the group by part of the query. this is not the case though:
@Entity
@DbName(TEST_DB_NAME)
data class TestEntity(
@Id
var id: String = UUID.randomUUID().toString(),
@Column
var name: String = "",
) {
@Aggregation("count(*)")
val count: Int = 0
@Aggregation("group_concat(id)")
val ids: String = ""
override fun hashCode() = id.hashCode()
override fun equals(other: Any?) = other is TestEntity && other.id == id
}
class MultipleAggregationsTest {
private val dsConfig = DataSourceConfig().also {
it.username = "sa"
it.password = "sa"
it.url = "jdbc:h2:mem:test"
it.driver = "org.h2.Driver"
}
private val dbConfig = DatabaseConfig().also {
it.name = TEST_DB_NAME
it.isDdlRun = true
it.isDdlGenerate = true
it.isDefaultServer = false
it.setDataSourceConfig(dsConfig)
it.addClass(TestEntity::class.java)
it.addClass(TestResourceWithLobAndAggregation::class.java)
}
private val db = DatabaseFactory.create(dbConfig)
@Test
fun `multiple aggregations`() {
db.insert(TestEntity(name = "1"))
db.insert(TestEntity(name = "1"))
db.insert(TestEntity(name = "2"))
LoggedSql.start()
val results = db.createQuery(TestEntity::class.java)
.select("name, count(*) as count, group_concat(id) as ids")
.findList()
val sql = LoggedSql.stop().joinToString("\n").also { println(it) }
println("results:")
results.forEach {
println("name: ${it.name} count: ${it.count} ids: ${it.ids}")
}
}
}the query in this case should be:
select t0.name, count(*) count, group_concat(t0.id) ids from test_entity t0 group by t0.name;but this is run instead:
select t0.name, count(*) count, group_concat(t0.id) ids from test_entity t0 group by t0.name, group_concat(t0.id);when
QTestEntity(db).select(QTestEntity._alias.name, QTestEntity._alias.count, QTestEntity._alias.ids)is used instead, it works fine (presumably because ebean knows both the count and ids properties represent aggregations), but sadly I can't do that for two reasons:
- there is @Aggregation is applied to all queries if entity also has a @Lob column #3682 and my entity does have a
@Lobcolumn - I need to be able to make this query platform agnostic, but only mysql and h2 support
group_concat, other dbs have for examplearray_agg, so I need to use a dynamic query here