Android Room Select @Relation Annotation With Conditions one-to-many Issue












0















I have 2 Entities: User and Messages. Every User can have some Messages.



Class User



@Entity(tableName = DBConstants.TABLE_USER)
open class User(
@field:PrimaryKey(autoGenerate = true)
var id: Int = 0,
var appUserId: Int = 0,
var name: String = "",
var phoneNumber: String = "",
var appPackage: @AppPackage String = IN_APP,
var userType: @UserType Int = CUSTOMER
) : Serializable ,BaseObservable(){
@field:Ignore
var messages: ArrayList<Messages> = ArrayList()

@Bindable
fun getNumMessages() : Int {
return messages.size
}
}


And Class Messages



@Entity(tableName = TABLE_USER_MESSAGES,
foreignKeys = [ForeignKey(entity = User::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("userId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE)])
data class Messages(@field:PrimaryKey(autoGenerate = true) var id: Int = 0,
@field:ColumnInfo(name = "userId")
var userId: Int = 0,
var appUserId: Int = 0,
var appPackage: @AppPackage String = IN_APP,
var ticker: String = "",
var title: String = "",
var text: String = "",
var date : String)


For Select One User - Many Messages. I Created Class UserMessagesView.kt



class UserMessagesView {
@Embedded
var user : User = User()
@Relation(parentColumn = "id", entityColumn = "userId",entity = Messages::class)
var msg: List<Messages> = ArrayList()
}


I use Select Inner Join with Conditions follow this



@Dao
abstract class UserMessagesDao : BaseDao<Messages>() {
@Query("SELECT * FROM user INNER JOIN messages ON messages.user_id = user.id WHERE messages.date like '%18-11-2018%'")
abstract fun getAllMessagesByToday(): List<UserMessagesView>
}


In databse, I have 1 user in table users with id = 1
and 4 record messages in table messages foreign key with id user = 1. Follow 2 image
TableUser
[Table Messages][2]



My Issue is When I call method




getAllMessagesByToday




I received List is size = 4, But First element 'UserMessagesView ' object 'msg' have size = 4 and Others Element object 'msg' size = 0.
And object User in UserMessagesView in List They are the same all field and difference each ID
Follow Image Result



In this case I think Room will return 1 record containing a user object and a msg of 4 elements. But it's not exactly what I thought. And I do not know where I went wrong.
Please help me. It took me 3 days to find out but it did not work. Maybe Room is new and no one has problems like me. Thanks everyone.
Sorry my bad English.










share|improve this question

























  • Please help me !!!!

    – Công Nguyễn
    Nov 27 '18 at 10:10
















0















I have 2 Entities: User and Messages. Every User can have some Messages.



Class User



@Entity(tableName = DBConstants.TABLE_USER)
open class User(
@field:PrimaryKey(autoGenerate = true)
var id: Int = 0,
var appUserId: Int = 0,
var name: String = "",
var phoneNumber: String = "",
var appPackage: @AppPackage String = IN_APP,
var userType: @UserType Int = CUSTOMER
) : Serializable ,BaseObservable(){
@field:Ignore
var messages: ArrayList<Messages> = ArrayList()

@Bindable
fun getNumMessages() : Int {
return messages.size
}
}


And Class Messages



@Entity(tableName = TABLE_USER_MESSAGES,
foreignKeys = [ForeignKey(entity = User::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("userId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE)])
data class Messages(@field:PrimaryKey(autoGenerate = true) var id: Int = 0,
@field:ColumnInfo(name = "userId")
var userId: Int = 0,
var appUserId: Int = 0,
var appPackage: @AppPackage String = IN_APP,
var ticker: String = "",
var title: String = "",
var text: String = "",
var date : String)


For Select One User - Many Messages. I Created Class UserMessagesView.kt



class UserMessagesView {
@Embedded
var user : User = User()
@Relation(parentColumn = "id", entityColumn = "userId",entity = Messages::class)
var msg: List<Messages> = ArrayList()
}


I use Select Inner Join with Conditions follow this



@Dao
abstract class UserMessagesDao : BaseDao<Messages>() {
@Query("SELECT * FROM user INNER JOIN messages ON messages.user_id = user.id WHERE messages.date like '%18-11-2018%'")
abstract fun getAllMessagesByToday(): List<UserMessagesView>
}


In databse, I have 1 user in table users with id = 1
and 4 record messages in table messages foreign key with id user = 1. Follow 2 image
TableUser
[Table Messages][2]



My Issue is When I call method




getAllMessagesByToday




I received List is size = 4, But First element 'UserMessagesView ' object 'msg' have size = 4 and Others Element object 'msg' size = 0.
And object User in UserMessagesView in List They are the same all field and difference each ID
Follow Image Result



In this case I think Room will return 1 record containing a user object and a msg of 4 elements. But it's not exactly what I thought. And I do not know where I went wrong.
Please help me. It took me 3 days to find out but it did not work. Maybe Room is new and no one has problems like me. Thanks everyone.
Sorry my bad English.










share|improve this question

























  • Please help me !!!!

    – Công Nguyễn
    Nov 27 '18 at 10:10














0












0








0








I have 2 Entities: User and Messages. Every User can have some Messages.



Class User



@Entity(tableName = DBConstants.TABLE_USER)
open class User(
@field:PrimaryKey(autoGenerate = true)
var id: Int = 0,
var appUserId: Int = 0,
var name: String = "",
var phoneNumber: String = "",
var appPackage: @AppPackage String = IN_APP,
var userType: @UserType Int = CUSTOMER
) : Serializable ,BaseObservable(){
@field:Ignore
var messages: ArrayList<Messages> = ArrayList()

@Bindable
fun getNumMessages() : Int {
return messages.size
}
}


And Class Messages



@Entity(tableName = TABLE_USER_MESSAGES,
foreignKeys = [ForeignKey(entity = User::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("userId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE)])
data class Messages(@field:PrimaryKey(autoGenerate = true) var id: Int = 0,
@field:ColumnInfo(name = "userId")
var userId: Int = 0,
var appUserId: Int = 0,
var appPackage: @AppPackage String = IN_APP,
var ticker: String = "",
var title: String = "",
var text: String = "",
var date : String)


For Select One User - Many Messages. I Created Class UserMessagesView.kt



class UserMessagesView {
@Embedded
var user : User = User()
@Relation(parentColumn = "id", entityColumn = "userId",entity = Messages::class)
var msg: List<Messages> = ArrayList()
}


I use Select Inner Join with Conditions follow this



@Dao
abstract class UserMessagesDao : BaseDao<Messages>() {
@Query("SELECT * FROM user INNER JOIN messages ON messages.user_id = user.id WHERE messages.date like '%18-11-2018%'")
abstract fun getAllMessagesByToday(): List<UserMessagesView>
}


In databse, I have 1 user in table users with id = 1
and 4 record messages in table messages foreign key with id user = 1. Follow 2 image
TableUser
[Table Messages][2]



My Issue is When I call method




getAllMessagesByToday




I received List is size = 4, But First element 'UserMessagesView ' object 'msg' have size = 4 and Others Element object 'msg' size = 0.
And object User in UserMessagesView in List They are the same all field and difference each ID
Follow Image Result



In this case I think Room will return 1 record containing a user object and a msg of 4 elements. But it's not exactly what I thought. And I do not know where I went wrong.
Please help me. It took me 3 days to find out but it did not work. Maybe Room is new and no one has problems like me. Thanks everyone.
Sorry my bad English.










share|improve this question
















I have 2 Entities: User and Messages. Every User can have some Messages.



Class User



@Entity(tableName = DBConstants.TABLE_USER)
open class User(
@field:PrimaryKey(autoGenerate = true)
var id: Int = 0,
var appUserId: Int = 0,
var name: String = "",
var phoneNumber: String = "",
var appPackage: @AppPackage String = IN_APP,
var userType: @UserType Int = CUSTOMER
) : Serializable ,BaseObservable(){
@field:Ignore
var messages: ArrayList<Messages> = ArrayList()

@Bindable
fun getNumMessages() : Int {
return messages.size
}
}


And Class Messages



@Entity(tableName = TABLE_USER_MESSAGES,
foreignKeys = [ForeignKey(entity = User::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("userId"),
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE)])
data class Messages(@field:PrimaryKey(autoGenerate = true) var id: Int = 0,
@field:ColumnInfo(name = "userId")
var userId: Int = 0,
var appUserId: Int = 0,
var appPackage: @AppPackage String = IN_APP,
var ticker: String = "",
var title: String = "",
var text: String = "",
var date : String)


For Select One User - Many Messages. I Created Class UserMessagesView.kt



class UserMessagesView {
@Embedded
var user : User = User()
@Relation(parentColumn = "id", entityColumn = "userId",entity = Messages::class)
var msg: List<Messages> = ArrayList()
}


I use Select Inner Join with Conditions follow this



@Dao
abstract class UserMessagesDao : BaseDao<Messages>() {
@Query("SELECT * FROM user INNER JOIN messages ON messages.user_id = user.id WHERE messages.date like '%18-11-2018%'")
abstract fun getAllMessagesByToday(): List<UserMessagesView>
}


In databse, I have 1 user in table users with id = 1
and 4 record messages in table messages foreign key with id user = 1. Follow 2 image
TableUser
[Table Messages][2]



My Issue is When I call method




getAllMessagesByToday




I received List is size = 4, But First element 'UserMessagesView ' object 'msg' have size = 4 and Others Element object 'msg' size = 0.
And object User in UserMessagesView in List They are the same all field and difference each ID
Follow Image Result



In this case I think Room will return 1 record containing a user object and a msg of 4 elements. But it's not exactly what I thought. And I do not know where I went wrong.
Please help me. It took me 3 days to find out but it did not work. Maybe Room is new and no one has problems like me. Thanks everyone.
Sorry my bad English.







android android-room






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:45







Công Nguyễn

















asked Nov 18 '18 at 1:58









Công NguyễnCông Nguyễn

85




85













  • Please help me !!!!

    – Công Nguyễn
    Nov 27 '18 at 10:10



















  • Please help me !!!!

    – Công Nguyễn
    Nov 27 '18 at 10:10

















Please help me !!!!

– Công Nguyễn
Nov 27 '18 at 10:10





Please help me !!!!

– Công Nguyễn
Nov 27 '18 at 10:10












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357249%2fandroid-room-select-relation-annotation-with-conditions-one-to-many-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357249%2fandroid-room-select-relation-annotation-with-conditions-one-to-many-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()