【问题标题】:Child data from Parent using association in Extjs使用 Extjs 中的关联来自父级的子数据
【发布时间】:2014-10-06 13:10:01
【问题描述】:

我是 Extjs 4 的新手。

我有两个不同的类,父类和子类。 Parent hasMany Child and Child belongsTo Parent。

我使用过休眠映射。

我的模型在下面

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 中显示父数据。 如果我在父模型中为子字段指定映射,我可以获得子值。

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

但我需要动态子数据。

请指导我...

谢谢

【问题讨论】:

    标签: hibernate extjs grid


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-30
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2016-01-09
      相关资源
      最近更新 更多