Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

  • Hibernate One-to-Many Not loading related data

    I'm trying to set up 2 ListGrids that show one-to-many parent/child data. I think I've followed all of the documentation correctly, however there are no related records being returned in the fetch, the list of related records is null.

    Here's my parent data source:
    Code:
    <DataSource ID="BuildingType" 
        serverType="hibernate"
        dropExtraFields="true"
        sendExtraFields="false"
        beanClassName="com.assaabloy.aaos.shared.entities.ads.BuildingType"
        configBean="adsSessionFactory">
        <fields>
            <field name="id" type="sequence" hidden="true" primaryKey="true"/>
            <field name="locale" type="text" hidden="true" required="true"/>
            <field name="productName" type="text" length="100" required="true"/>
            <field name="alternatePicture" type="text" length="100" required="false"/>
            <field name="briefDesc" type="text" title="Brief Description" length="100" required="false"/>
            <field name="keyWords" type="text" required="false"/>
            <field name="revitTypes" type="text" length="800" required="false"/>
            <field name="aaosTypes" type="text" length="800" required="false"/>
            <field name="sortOrder" type="integer" required="true"/>
            <field name="projectTypeList" multiple="true" type="AAOSProjectType" foreignKey="AAOSProjectType.id"/>
        </fields>
    </DataSource>
    The child data source:
    Code:
    <DataSource ID="AAOSProjectType" 
        serverType="hibernate"
        beanClassName="com.assaabloy.aaos.shared.entities.ads.AAOSProjectType"
        configBean="adsSessionFactory">
        <fields>
            <field name="id" type="sequence" hidden="true" primaryKey="true"/>
            <field name="locale" type="text" hidden="true" required="true"/>
            <field name="name" type="text" length="100" required="true"/>
        </fields>
    </DataSource>
    Parent/child classes (getters and setters omitted for brevity):
    Code:
    @Entity
    @XmlRootElement
    public class BuildingType implements Serializable {
    
        private static final long     serialVersionUID = 1L;
        private Integer               sortOrder;
        @Column(length = 100)
        private String                productName;
        @Column(length = 100)
        private String                alternatePicture;
        @Column(length = 2147483647)
        private String                briefDesc;
        @Column(length = 2147483647)
        private String                fullDesc;
        @Lob
        private byte[]                image;
        @Column(length = 2147483647)
        private String                keyWords;
        private Boolean               hide;
        @Column(length = 800)
        private String                revitTypes;
        @Column(length = 800)
        private String                aAOSTypes;
        @Column(length = 10)
        private String                locale;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(nullable = false)
        private Integer               id;
        @OneToMany(mappedBy = "marketSegmentID")
        private List<AAOSProjectType> projectTypeList;
    
        /** Getters and setters **/
    }
    Code:
    @Entity
    @XmlRootElement
    public class AAOSProjectType implements Serializable {
    
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(nullable = false)
        private Integer           id;
        @Column(length = 100)
        private String            name;
        @Column(length = 200)
        private String            description;
        @Column(length = 2147483647)
        private String            definition;
        @Column(length = 10)
        private String            locale;
        @JoinColumn(name = "MarketSegmentID", referencedColumnName = "ID")
        @ManyToOne
        private BuildingType      marketSegmentID;
    
        /** Getters and setters **/
    }
    Here's what I'm getting in the RPC fetch:
    Code:
    {
        affectedRows:0, 
        data:[
            {
                projectTypeList:null, 
                keyWords:"RES\\", 
                aaosTypes:"Apartments/Condominiums 1-3 Stories|Apartments/Condominiums 4+ Stories|", 
                locale:"en_US", 
                productName:"Apartments/Condominiums", 
                briefDesc:null, 
                sortOrder:1, 
                alternatePicture:"None", 
                id:1, 
                revitTypes:"Multi Family|Single Family|"
            }, 
            {
                projectTypeList:null, 
                keyWords:null, 
                aaosTypes:null, 
                locale:"en_GB", 
                productName:"Residential", 
                briefDesc:null, 
                sortOrder:1, 
                alternatePicture:null, 
                id:101, 
                revitTypes:null
            }, 
    ...
    Is there something I'm doing wrong with my data sources?

  • #2
    See JPA / Hibernate relations overview. You either declare the type of the field as another DataSource to cause loading of nested structures, or you use the foreignKey declaration to allow loading as a separate request (the recommended approach), but placing both settings on the same field is incorrect.

    Comment


    • #3
      Ok I'm confused now, the JPA Hibernate Relations document and the HB One-to-Many Relation example in the EE showcase have both a type and a foreignKey property on the same field. I'm basically trying to copy the example in the EE showcase exactly, just with my 2 tables.

      https://www.smartclient.com/smartgwt...Relations.html

      One-to-Many Relations

      An example of One-To-Many relation is that One "Country" has Many "City"'s. Each "Country" has a list of cities within it, which may be declared as a Java bean property of Collection type (or List, Set, etc).
      To specify a one-to-many relation, declare a DataSourceField that:
      • is named after the Java field that declares the OneToMany relation (whose type is a Collection of the related entities)
      • declares its "type" to be the ID of the related DataSource
      • declares a foreignKey pointing to the related DataSource's primaryKey field
      • sets multiple="true"
      For example, for a Country bean that has a Collection of City beans:
      <field name="cities" type="city" multiple="true" foreignKey="city.cityId"/>

      Comment


      • #4
        Sorry, your use of both FK and type is correct if you want to load nested data (again, not the recommended approach for most scenarios). If you want to continue with that approach, the next step is to see the server log for the failing request. Please note that we always need this as well as several other pieces of information (including the product and version you're using - see FAQ) so next time please take care to post all needed information.

        Comment


        • #5
          Thanks for the reply. I was able to get this working as a separate request using the foreignKey field. I was just getting confused because the list of child objects in the parent was coming back null in the initial fetch. Once I added a:
          Code:
          ListGrid.fetchRelatedData()
          everything started working.

          Comment

          Working...
          X