requery / requery

requery - modern SQL based query & persistence for Java / Kotlin / Android
Apache License 2.0
3.13k stars 245 forks source link

Updating an entity is trying to insert values in its references #880

Closed steelxt closed 5 years ago

steelxt commented 5 years ago

i'm trying to update some null fields in a table, some of them are references to another tables:

@Entity @Table(name = "wuser") //@JsonIgnoreProperties(ignoreUnknown=true)

interface  User:Persistable,Serializable {

    @get:Key
    @get:Generated
     val id: Long?

     var email: String?

     var lastName: String?

     var username: String?

     var name: String?

     var password: String?

     var phone: String?

    @get:Column( name="datereg")
     var registrationDate:Long?

    var address:String?
    var birthday: Date?
    @get:ManyToOne
    @get:Column(name="idcity", foreignKey = [ ForeignKey(referencedColumn= "idcity"), ForeignKey(referencedColumn= "idcountry")])
    var city:City?

    @get:ManyToOne
    @get:ForeignKey
    @get:Column(name="idcountry")
    var country:Country?
}

and this is my log

INFO: preUpdate wuser [9, null, null, null, test3@email.com, com.cps.common.entity.CityEntity, com.cps.common.entity.CountryEntity, test3, test3, test3432, 3221341234, test3]
Jun 20, 2019 5:44:40 PM io.requery.sql.LoggingListener preUpdate
INFO: preUpdate City [0, 107, null, null]
Jun 20, 2019 5:44:40 PM io.requery.sql.LoggingListener beforeExecuteUpdate
INFO: beforeExecuteUpdate insert into City (idcountry, idcity, name, position) values (0, 107, NULL, '') on conflict (idcountry, idcity) do update set idcountry = EXCLUDED.idcountry, idcity = EXCLUDED.idcity, name = EXCLUDED.name, position = EXCLUDED.position sql:
insert into City (idcountry, idcity, name, position) values (?, ?, ?, ?) on conflict (idcountry, idcity) do update set idcountry = EXCLUDED.idcountry, idcity = EXCLUDED.idcity, name = EXCLUDED.name, position = EXCLUDED.position 
([0, 107, null, null])

I had to annotate some interfaces to avoid Jackson mapper error I.E

@Entity
@JsonDeserialize(`as` =  CityEntity::class)
interface City: Persistable {

this is json I converted back to entity(User entity)

{
    "id":9 ,
     "city": {
            "idcity": 107
             },
        "country": {
            "idcountry": 472
        },
    "email": "test3@email.com",
    "lastName": "test3",
    "username": "test3",
    "name": "test3",
    "password": "test3432",
    "phone": "3221341234"
}

what is the right way to update with inner referenced entities?

steelxt commented 5 years ago

Solved by adding

@get:ManyToOne(cascade = [CascadeAction.NONE])