Hibernate @ManyToOne和@OneToMany将数据作为List(记录数)获取

时间:2016-08-10 14:03:04

标签: hibernate spring-mvc hibernate-mapping entity-relationship hbmxml

  • 我有2张桌子 1.Org [ORG_ID,PARENT_ID,LANG_ID(REFERENCES LANG)] 2.Lang [LANG_ID,LANG_NAME]

  • 我的JAVA实体文件

    Lang.java

    @Entity
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "langId")
    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @SuppressWarnings("serial")
    public class Lang implements java.io.Serializable {
        private BigDecimal langId;
        private String langName;
        private Set<Org> orgs = new HashSet<Org>(0);
        public Lang() {
        }
        public Lang(BigDecimal langId, Set<Org> orgs) {
            this.langId = langId;
            this.orgs = orgs;
        }
        public BigDecimal getLangId() {
            return this.langId;
        }
        public void setLangId(BigDecimal langId) {
            this.langId = langId;
        }
        public String getLangName() {
            return this.langName;
        }
        public void setLangName(String langName) {
            this.langName = langName;
        }public void setOrgs(Set<Org> orgs) {
            this.orgs = orgs;
        }
    }
    

    Org.java

        @Entity
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "orgId")
    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @SuppressWarnings("serial")
    public class Org implements java.io.Serializable {
        @Id
        @Column(name = "orgId")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private BigDecimal orgId;   
        private BigDecimal parentOrgId;
        @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "langId",scope=Lang.class)
        public Org() {
        }
        public Org(BigDecimal orgId, BigDecimal parentOrgId, Lang lang) {
            this.orgId = orgId;
            this.parentOrgId = parentOrgId;
            this.lang = lang;
        }
        public BigDecimal getOrgId() {
            return this.orgId;
        }
        public void setOrgId(BigDecimal orgId) {
            this.orgId = orgId;
        }
        public BigDecimal getParentOrgId() {
            return this.parentOrgId;
        }
        public void setParentOrgId(BigDecimal parentOrgId) {
            this.parentOrgId = parentOrgId;
        }
        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
        public Lang getLang() {
            return this.lang;
        }
        public void setLang(Lang lang) {
            this.lang = lang;
        }
    }
    
  • 我的HBM Xml文件

    Org.hbm.xml

        <hibernate-mapping package="com.test.entity">
        <class name="com.test.entity.Org" table="ORG">
        <id name="orgId" type="big_decimal">
            <column name="ORG_ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>        
        <property name="parentOrgId" type="big_decimal">
            <column name="PARENT_ORG_ID" precision="22" scale="0" not-null="true" />
        </property>
        <many-to-one name="lang" class="com.test.entity.Lang" fetch="select">
            <column name="LANG_ID" precision="22" scale="0" not-null="true" />
        </many-to-one>        
        </class>
    </hibernate-mapping>
    

    Lang.hbm.xml

        <hibernate-mapping package="com.test.entity">
        <class name="com.test.entity.Lang" table="LANG">
        <id name="langId" type="big_decimal">
            <column name="LANG_ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>
        <property name="langName" type="string">
            <column name="LANG_NAME" length="32" not-null="true" unique="true" />
        </property>
        <set name="orgs" table="ORG" inverse="true" lazy="true" fetch="select">
            <key>
            <column name="LANG_ID" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.test.entity.Org" />
        </set>
        </class>
    </hibernate-mapping>
    
  • 我的Hibernate查询是:&#34; FROM Org as org WHERE org.parentOrgId =:parentOrgId org.orgId =:parentOrgId&#34;

  • DataBase DATABASE

  • 的图片
  • 当我从控制器运行时列表listOrganization = organizationService.getAllChildWithParetOrgs(parentId); // parentId = 0

  • 我得到了结果ObjectList / JSON Result

        [
      {
        "orgId": 1,
        "parentOrgId": 0,    
        "lang": {
          "langId": 1,
          "langName": "EN",
          "orgs": [
        {
          "orgId": 2,
          "parentOrgId": 1,
          "lang": 1,
          "currency": {
            "currencyId": 1,
            "currencyCode": "INR",
            "currencyName": "INR",
            "currencyDescription": "INDIAN RUPEE",
            "orgs": [
              2,
              1,
              {
            "orgId": 3,
            "parentOrgId": 1,
            "lang": 1,
            "currency": 1,                
              }
            ]
          }
        },
        1,
        3
          ]
        },
        "currency": 1
      },
      2, //---> Here No 2nd Organization Data
      3  //---> Here No 3rd Organization Data
    ]
    
  • 此处货币也与 Lang 表相同,其Currency_Id参考货币
  • 我无法获得第二名&amp;数组中的第三个组织数据,它在Lang.Orgs和Lang.Orgs.Currency中可用

  • 请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

请提供您的Controller方法getAllChildWithParetOrgs(parentId);的代码。那会更有帮助。另外,根据我对hibernate查询连接和数据库快照的理解,我不认为JSON输出应该再次登记记录。请在下面找到解释:

由于您传递的parentId为0.并且基于hibernate查询连接和数据库快照,似乎 OrgId = 0 没有单独的记录。 从子句的 parentOrgId = 0 部分开始,它指的是子:orgId为1的记录。 随后,它指的是 orgId = 1 的子记录,即 parentOrgId = 1 的记录,这些记录是 orgId = 2 &amp; orgId = 3 。我不认为你的JSON对象应该在那之后再获得更多的结果