2016-08-03 57 views
0

我是新來的休眠(有c背景),雖然經歷了一些書籍和例子,我總是看到組件嵌入類映射。例如hibernate可以組件是全局的嗎?由其他實體共享?

<hibernate-mapping> 
    <class name="com.mkyong.customer.Customer" table="customer"> 
     <!-other entity mapping --> 
     <component name="Address" class="com.mkyong.customer.Address"> 
      <property name="address1" type="string"> 
       <column name="ADDRESS1" not-null="true" /> 
      </property> 
      <property name="address2" type="string"> 
       <column name="ADDRESS2" not-null="true" /> 
      </property> 

     </component> 
     . 
     . 
    </class> 
</hibernate-mapping> 

我的問題是可以組件是全局的,並由任何實體共享?像@Embeddable JPA註釋

我指的是在例如:http://www.mkyong.com/hibernate/hibernate-component-mapping-example/

+0

你問是否有其他組件可以引用這個組件?如果是這樣,那麼是的。 –

+0

任何其他組件或其他實體,如果是的話可以請提供一些例子? – stultus

+0

已發佈一個答案 –

回答

0

放置在自己的XML文件中的分量。只有有分量,像這樣:

<component name="Address" class="com.mkyong.customer.Address"> 
     <property name="address1" type="string"> 
      <column name="ADDRESS1" not-null="true" /> 
     </property> 
     <property name="address2" type="string"> 
      <column name="ADDRESS2" not-null="true" /> 
     </property> 

</component> 

然後,每當你想重新使用這個組件,只需使用這條線:

<xi:include href="your-component.xml" /> 

一定要在你的XML的頂部添加這是能夠使用喜標籤:

xmlns:xi="w3.org/2001/XInclude" 

還是有它都在同一行,像這樣:

<xi:include href="your-component.xml" xmlns:xi="w3.org/2001/XInclude"/> 
相關問題