I'm new to Extjs 4.
I have two different classes Parent and Child In relation. Parent hasMany Child and Child belongsTo Parent.
I have used hibernate mappings.
My models are below
Parent.js
Ext.define('com.sample.model.Parent', {
extend: 'Ext.data.Model',
fields: [
{type: 'integer', name: 'parent_id'},
{type: 'string', name: 'parent_name'},
],
hasMany: {
foreignKey: 'parent_id',
model: 'com.sample.model.Child',
name: 'child'
},
proxy: {
idProperty: 'id',
totalProperty: 'total',
successProperty: 'success',
type: 'ajax',
url: 'view.action',
reader: new Ext.data.JsonReader({
root: 'data'
})
}
});
Child.js
Ext.define('com.sample.model.Child', {
extend: 'Ext.data.Model',
fields: [
{type: 'integer', name: 'child_id'},
{type: 'string', name: 'child_name'},
{type: 'integer', name: 'parent_id'},
],
belongsTo: {model:'com.sample.model.Parent'}
});
Below is my Store
var storeTsTrain = new Ext.data.Store({
storeId: 'parentStore',
model: 'com.sample.model.Parent',
autoLoad: true,
});
My Hibernate Mapping files
Parent.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sample.model">
<class name="Parent" table="PARENT">
<id name="parent_id" column="PARENT_ID">
<generator class="native" />
</id>
<bag name="childList" inverse="true" lazy="false" cascade="all">
<key>
<column name="PARENT_ID" not-null="true"/>
</key>
<one-to-many class="com.sample.model.Child" />
</bag>
<property name="parent_id" type="integer" column="PARENT_ID"/>
<property name="parent_name" type="string" column="PARENT_NAME" />
</class>
</hibernate-mapping>
Child.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sample.model">
<class name="Child" table="CHILD">
<id name="child_id" column="CHILD_ID">
<generator class="native" />
</id>
<property name="child_id" type="integer" column="CHILD_ID"/>
<property name="child_name" type="string" column="CHILD_NAME" />
<property name="parent_id" type="integer" column="PARENT_ID"/>
</class>
</hibernate-mapping>
My Java POJO Classes are below
Parent.java
package com.sample.model;
import java.util.List;
public class Parent implements java.io.Serializable{
private Integer parentId;
private String parentName;
private List<Child> childList;
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public List<Child> getChildList() {
return childList;
}
public void setChildList(List<Child> childList) {
this.childList = childList;
}
}
Child.java
package com.sample.model;
import java.util.List;
public class Child implements java.io.Serializable{
private Integer childId;
private String childName;
private Integer parentId;
public Integer getChildId() {
return childId;
}
public void setChildId(Integer childId) {
this.childId = childId;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getParentName() {
return parentName;
}
}
I have Child list in Parent POJO Class. We are successfully getting the Parent and Child data in Java Obects and we can able to show Parent data in Extjs Grid. If I specify mappings in parent model for child fields, I can get Child values.
{name : 'child_id', type: 'integer', mapping: 'child[0].child_id'} ,
{name : 'child_name', type: 'string', mapping: 'child[0].child_name'},
{name : 'parent_id', type: 'integer', mapping: 'child[0].parent_id'}
But I need dynamic child data.
Please guide me...
Thanks