PDA

View Full Version : Updating entity in database (ExtJS -> GSON -> Hibernate -> DB)



kornicameister
13 Oct 2012, 9:39 AM
Hi I have model defined like this


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



{"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


@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

mitchellsimoens
15 Oct 2012, 8:41 AM
This doesn't sound like an Ext JS question