Fixed
Status Update
Comments
am...@gmail.com <am...@gmail.com> #2
Any feedback on this? I'm no SQL expert, so I could be going at this wrong, but I'm finding M:N in Room to be so convoluted it isn't worth it. Manual joins create a ton of redundant object models and multiple queries create some sketchy nested LiveData. Can you guys manage M:N with Room at scale or is there any work in progress?
am...@gmail.com <am...@gmail.com> #3
So I finally figured something manageable out, though I suspect the onChanged observation won't work perfectly and there's probably a more efficient method:
https://gist.github.com/rcketscientist/53cbdab925fcaa7dc9850375160e023b
yb...@google.com <yb...@google.com>
ap...@google.com <ap...@google.com> #4
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 5c597749f35557501041bce0c62f055e5afe9d72
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Thu May 30 11:11:21 2019
Support many-to-many with @Relation using @Junction
Expand @Relation to contain a new method, 'associateBy' of type
@Junction that can be used to specify a junction table for resolving
relationships, mainly many-to-many.
Upon using @Junction the query
generated by the RelationCollector is of the form of:
SELECT *, <junction-parent-column>
FROM <junction-table> AS _junction
JOIN <entity-table> ON (<junction-entity-column> = <entity-column>)
WHERE <junction-parent-column> IN (:args)
@Junction supports both an @Entity or @DatabaseView, the Junction table
itself doesn't have to strictly model a many-to-many relation. Both
parentColumn and entityColumn are optional and will be assumed to be
the same as the ones defined in @Relation, otherwise those methods can
be used to specify arbitrary columns in the junction table.
Room will also give a warning for those columns in the junction table
that are not part of a foreign key.
Bug: 63736353
Bug: 69201917
Test: ManyToManyRelationTest, PojoProcessorTest, and others...
Change-Id: I967cf77252391379377ebe0ebfeb23a4fd881ca5
A room/common/api/2.2.0-alpha01.ignore
M room/common/api/2.2.0-alpha01.txt
M room/common/api/current.txt
A room/common/src/main/java/androidx/room/Junction.java
M room/common/src/main/java/androidx/room/Relation.java
M room/compiler/src/main/kotlin/androidx/room/ext/element_ext.kt
M room/compiler/src/main/kotlin/androidx/room/processor/EntityProcessor.kt
M room/compiler/src/main/kotlin/androidx/room/processor/PojoProcessor.kt
M room/compiler/src/main/kotlin/androidx/room/processor/ProcessorErrors.kt
A room/compiler/src/main/kotlin/androidx/room/vo/Junction.kt
M room/compiler/src/main/kotlin/androidx/room/vo/Relation.kt
M room/compiler/src/main/kotlin/androidx/room/vo/Warning.kt
M room/compiler/src/main/kotlin/androidx/room/writer/RelationCollectorMethodWriter.kt
M room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTest.kt
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/MusicTestDatabase.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/TestDatabase.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/MusicDao.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/UserDao.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/UserPetDao.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/LiveDataQueryTest.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/ManyToManyRelationTest.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/PojoWithRelationTest.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/FriendsJunction.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/MultiSongPlaylistWithSongs.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/Playlist.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistMultiSongXRefView.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistSongXRef.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistWithSongTitles.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistWithSongs.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/UserAndAllPetsViaJunction.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/UserAndFriends.java
https://android-review.googlesource.com/973527
https://goto.google.com/android-sha1/5c597749f35557501041bce0c62f055e5afe9d72
Branch: androidx-master-dev
commit 5c597749f35557501041bce0c62f055e5afe9d72
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Thu May 30 11:11:21 2019
Support many-to-many with @Relation using @Junction
Expand @Relation to contain a new method, 'associateBy' of type
@Junction that can be used to specify a junction table for resolving
relationships, mainly many-to-many.
Upon using @Junction the query
generated by the RelationCollector is of the form of:
SELECT *, <junction-parent-column>
FROM <junction-table> AS _junction
JOIN <entity-table> ON (<junction-entity-column> = <entity-column>)
WHERE <junction-parent-column> IN (:args)
@Junction supports both an @Entity or @DatabaseView, the Junction table
itself doesn't have to strictly model a many-to-many relation. Both
parentColumn and entityColumn are optional and will be assumed to be
the same as the ones defined in @Relation, otherwise those methods can
be used to specify arbitrary columns in the junction table.
Room will also give a warning for those columns in the junction table
that are not part of a foreign key.
Bug: 63736353
Bug: 69201917
Test: ManyToManyRelationTest, PojoProcessorTest, and others...
Change-Id: I967cf77252391379377ebe0ebfeb23a4fd881ca5
A room/common/api/2.2.0-alpha01.ignore
M room/common/api/2.2.0-alpha01.txt
M room/common/api/current.txt
A room/common/src/main/java/androidx/room/Junction.java
M room/common/src/main/java/androidx/room/Relation.java
M room/compiler/src/main/kotlin/androidx/room/ext/element_ext.kt
M room/compiler/src/main/kotlin/androidx/room/processor/EntityProcessor.kt
M room/compiler/src/main/kotlin/androidx/room/processor/PojoProcessor.kt
M room/compiler/src/main/kotlin/androidx/room/processor/ProcessorErrors.kt
A room/compiler/src/main/kotlin/androidx/room/vo/Junction.kt
M room/compiler/src/main/kotlin/androidx/room/vo/Relation.kt
M room/compiler/src/main/kotlin/androidx/room/vo/Warning.kt
M room/compiler/src/main/kotlin/androidx/room/writer/RelationCollectorMethodWriter.kt
M room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTest.kt
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/MusicTestDatabase.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/TestDatabase.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/MusicDao.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/UserDao.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/UserPetDao.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/LiveDataQueryTest.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/ManyToManyRelationTest.java
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/PojoWithRelationTest.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/FriendsJunction.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/MultiSongPlaylistWithSongs.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/Playlist.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistMultiSongXRefView.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistSongXRef.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistWithSongTitles.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistWithSongs.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/UserAndAllPetsViaJunction.java
A room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/UserAndFriends.java
da...@google.com <da...@google.com>
ap...@google.com <ap...@google.com> #5
Project: platform/frameworks/support
Branch: androidx-master-dev
commit 0afda8aec8143c7dd58d0be6a5dc8a90a150cf75
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Mon Jun 10 13:13:21 2019
Improve warning messages caused by @Relation.
1. Warn about column affinity mismatch when using a junction table.
2. Don't warn about cursor mismatch when finding an adapter for the
relation field. The query is generated by the compiler and it might
use more columns than those needed for the object (specifically the
key column), therefore force suppress the warning to avoid the noise.
3. When noting about using a Cursor adapter because the relation type
is simple, then include the field element, otherwise these messages
are printed without knowing which relation is being noted.
Bug: 63736353
Bug: 69201917
Test: ManyToManyRelationTest
Change-Id: Ie3975ac31a88296afddbab51fe48302ac2693513
M room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
M room/compiler/src/main/kotlin/androidx/room/processor/ProcessorErrors.kt
M room/compiler/src/main/kotlin/androidx/room/vo/RelationCollector.kt
M room/compiler/src/main/kotlin/androidx/room/writer/RelationCollectorMethodWriter.kt
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistSongXRef.java
https://android-review.googlesource.com/980058
https://goto.google.com/android-sha1/0afda8aec8143c7dd58d0be6a5dc8a90a150cf75
Branch: androidx-master-dev
commit 0afda8aec8143c7dd58d0be6a5dc8a90a150cf75
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Mon Jun 10 13:13:21 2019
Improve warning messages caused by @Relation.
1. Warn about column affinity mismatch when using a junction table.
2. Don't warn about cursor mismatch when finding an adapter for the
relation field. The query is generated by the compiler and it might
use more columns than those needed for the object (specifically the
key column), therefore force suppress the warning to avoid the noise.
3. When noting about using a Cursor adapter because the relation type
is simple, then include the field element, otherwise these messages
are printed without knowing which relation is being noted.
Bug: 63736353
Bug: 69201917
Test: ManyToManyRelationTest
Change-Id: Ie3975ac31a88296afddbab51fe48302ac2693513
M room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
M room/compiler/src/main/kotlin/androidx/room/processor/ProcessorErrors.kt
M room/compiler/src/main/kotlin/androidx/room/vo/RelationCollector.kt
M room/compiler/src/main/kotlin/androidx/room/writer/RelationCollectorMethodWriter.kt
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/vo/PlaylistSongXRef.java
Description