来自父级的子数据使用Extjs中的关联

时间:2014-08-13 08:39:00

标签: hibernate extjs grid

我是Extjs 4的新手。

我有两个不同的类Parent和Child相关。父母有很多孩子和孩子属于父母。

我使用过hibernate映射。

我的模型低于

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'}
});

以下是我的商店

var storeTsTrain = new Ext.data.Store({
    storeId: 'parentStore',
    model: 'com.sample.model.Parent',
    autoLoad: true,
});

我的Hibernate制图文件

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>

我的Java POJO课程在

之下

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;
    }
}

我在父POJO班中有子列表。我们成功地在Java Obects中获取父子数据,我们可以在Extjs Grid中显示父数据。 如果我在子模型的父模型中指定映射,我可以获得Child值。

{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'}

但我需要动态子数据。

请指导我......

由于

1 个答案:

答案 0 :(得分:0)