2
  • リストのようなデータをフェッチI持っ2つのテーブル
    1.Org [ORG_IDは、PARENT_ID、LANG_IDは(LANGを参照)] 2.Lang [LANG_ID、LANG_NAME]休止@ManyToOneと@OneToManyは(レコードのNUM)

  • マイ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クエリは次のとおりです。"組織FROM ORG WHERE org.parentOrgId =よう:parentOrgIdまたはorg.orgId =:parentOrgId" データベース私は実行DATABASE

  • 画像from controller リストの構成= organizationService.getAllChildWithParetOrgs(parentId); // parentId = 0

  • 私はresultオブジェクトを取得しました。一覧/ JSONの結果はラングテーブルとしても同じ

    [ 
        { 
        "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 
    ] 
    
  • ここ通貨通貨
  • へのCurrency_Id参照は、アレイ内の第二&第三組織のデータを取得することはできないのですが、それはで利用可能ですLang.Orgs and Lang.Orgs.Currency

  • これは助けてください。ありがとうございます。

答えて

0

これで、あなたはHibernateでSpring JPA2とJacksonを使い始めました。

私は、Hibernateにエンティティからテーブルを作成させました。

Springアプリケーションコンテキストは次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> 

    <context:component-scan base-package="com.greg" /> 
    <tx:annotation-driven /> 
    <jpa:repositories base-package="com.greg.repository" /> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.h2.Driver" /> 
     <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" /> 
     <property name="username" value="sa" /> 
     <property name="password" value="" /> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="com.greg.entity" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
       <prop key="hibernate.show_sql">false</prop> 
       <prop key="hibernate.format_sql">false</prop> 
       <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

</beans> 

ラングエンティティは次のようになります。

@Entity 
public class Lang { 

    @Id 
    @Column(name = "lang_id") 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long langId; 

    @Column(name = "lang_name") 
    private String langName; 

    @OneToMany(fetch = FetchType.EAGER) 
    @JoinColumn(name = "lang_id", referencedColumnName = "lang_id") 
    private Set<Org> orgs; 

組織エンティティは次のようになります。

@Entity 
public class Org { 

    @Id 
    @Column(name = "org_id") 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long orgId; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "lang_id", referencedColumnName = "lang_id", updatable = false, insertable = false, nullable = false) 
    private Lang lang; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "parent_id", referencedColumnName = "org_id", updatable = false, insertable = false, nullable = false) 
    private Org parent; 

これはあなたの中にテーブルを作成します質問。

春JPAリポジトリ単純です:

@Repository 
@Transactional 
public interface OrgRepository extends CrudRepository<Org, Long> { 
} 

@Repository 
@Transactional 
public interface OrgRepository extends CrudRepository<Lang, Long> { 
} 

私はジャクソンがシリアライズさせた場合、私は得る:

{ 
    "orgId": 4, 
    "lang": { 
     "langId": 1, 
     "langName": "EN", 
     "orgs": null 
    }, 
    "parent": { 
     "orgId": 3, 
     "lang": { 
      "langId": 1, 
      "langName": "EN", 
      "orgs": null 
     }, 
     "parent": { 
      "orgId": 2, 
      "lang": { 
       "langId": 1, 
       "langName": "EN", 
       "orgs": null 
      }, 
      "parent": null 
     } 
    } 
} 

は私のテストでは、次のとおりです。

@Test 
@Transactional 
public void test1() { 

    try { 
     Lang lang = makeLang("EN"); 
     langRepository.save(lang); 
     assertNotNull(lang.getLangId()); 

     Org org1 = new Org(); 
     org1.setLang(lang); 
     orgRepository.save(org1); 

     Org org2 = new Org(); 
     org2.setLang(lang); 
     org2.setParent(org1); 
     orgRepository.save(org2); 

     Org org3 = new Org(); 
     org3.setLang(lang); 
     org3.setParent(org2); 
     orgRepository.save(org3); 

     assertEquals("EN", orgRepository.findOne(org1.getOrgId()).getLang().getLangName()); 
     assertNull(orgRepository.findOne(org1.getOrgId()).getParent()); 

     assertEquals("EN", orgRepository.findOne(org2.getOrgId()).getLang().getLangName()); 
     assertEquals(org1.getOrgId().longValue(), orgRepository.findOne(org2.getOrgId()).getParent().getOrgId().longValue()); 

     assertEquals("EN", orgRepository.findOne(org3.getOrgId()).getLang().getLangName()); 
     assertEquals(org2.getOrgId().longValue(), orgRepository.findOne(org3.getOrgId()).getParent().getOrgId().longValue()); 

     System.out.println(new ObjectMapper().writeValueAsString(org3)); 

     showTables("ORG"); 
     showTables("LANG"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     fail(); 
    } 
} 

private Lang makeLang(String name) { 
    Lang lang = new Lang(); 
    lang.setLangName(name); 
    return lang; 
} 
0

コントローラメソッドgetAllChildWithParetOrgs(parentId);のコードを入力してください。それはもっと役に立つでしょう。また、ハイバネートクエリの結合とデータベーススナップショットからの私の理解によると、JSON出力はもうレコードを追加する必要はないと思います。

あなたが渡した親ディレクトリは0です。そして、休止状態のクエリ結合とデータベーススナップショットに基づいて、OrgId = 0の個々のレコードは存在しないようです。 そしてparentOrgId句の= 0部分から、それは子供に言及している:1。 としてORGIDたレコードはその後、それはそれは、記録され、ORGID = 1の子レコードを参照していますparentOrgId = 1 orgId = 2 & orgId = 3のレコードである、である。私はあなたのJSONオブジェクトがそれ以降の結果を追加する必要はないと思います

関連する問題