1. #1
    Sencha User
    Join Date
    Jul 2012
    Posts
    16
    Answers
    1
    Vote Rating
    0
    kornicameister is on a distinguished road

      0  

    Default Unanswered: Updating entity in database (ExtJS -> GSON -> Hibernate -> DB)

    Unanswered: Updating entity in database (ExtJS -> GSON -> Hibernate -> DB)


    Hi I have model defined like this
    Code:
    Ext.define('WMS.model.entity.Unit', {
        extend: 'Ext.data.Model',
    
    
        requires: [
            'WMS.model.entity.Product'
        ],
    
    
        fields: [
            {
                name   : 'id',
                type   : 'int',
                persist: true
            },
            'name',
            'description',
            {
                name   : 'warehouse_id',
                type   : 'int',
                mapping: 'warehouse.id'
            },
            {
                name   : 'unittype_id',
                type   : 'int',
                mapping: 'type.id'
            },
            { name: 'size', type: 'int', defaultValue: 0},
            { name: 'maximumSize', type: 'int', defaultValue: 0}
        ],
    
    
        associations: [
            {
                type          : 'belongsTo',
                associationKey: 'warehouse',
                model         : 'WMS.model.entity.Warehouse',
                getterName    : 'getWarehouse',
                setterName    : 'setWarehouse',
                foreignKey    : 'warehouse_id'
            },
            {
                type          : 'hasOne',
                associationKey: 'type',
                model         : 'WMS.model.entity.UnitType',
                getterName    : 'getType',
                setterName    : 'setType',
                foreignKey    : 'unittype_id'
            },
            {
                type          : 'hasMany',
                associationKey: 'products',
                model         : 'WMS.model.entity.Product'
            }
        ],
    
    
        sorters: [
            'id', 'name'
        ],
        //TODO validations
    
    
        proxy: {
            type: 'wms',
            url : 'wms/agent/unit'
        }
    });
    which, is UPDATE action is used generate request to the server with following json object agrageted in request payload

    Code:
    {"data":[{"id":5,"name":"Unit 5","description":"Damn it","warehouse_id":11,"unittype_id":3,"size":0,"maximumSize":408}]}
    As you can see I have valid foreign keys attributes to warehouse and to unittype entities.
    And this is exactly the same form I receive on the server side. But Unit (Java-class) model (Hibernate) contains type and warehouse properties like this
    Code:
            @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JoinColumn(name = "warehouse_id", referencedColumnName = "idWarehouse")
        private Warehouse warehouse;
    
    
        @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JoinColumn(name = "unittype_id", referencedColumnName = "idUnitType")
        private UnitType type;
    So the names of properties in json payload object have the same names is name of join columns.

    And this is the problem description.
    How can I make GSON to convert object from payload to Unit (Java-class object) by using Hibernate in deserialization. I know I have to write custom deserializer but how ? This is the question I can not find answer to.

    Or maybe using some kind of typeAdapter ?

    Thanks in advance

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,330
    Answers
    3541
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    This doesn't sound like an Ext JS question
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

Thread Participants: 1

Tags for this Thread