2014-08-29 156 views
0

我想知道是否有可能從休眠映射文件生成實體。 我的最終目標是編程創建一個映射文件,然後從中創建數據庫。 我知道,我可以使用:休眠,從映射文件生成實體和數據庫

<prop key="hibernate.hbm2ddl.auto">create</prop> 

要創建數據庫形式的實體,但有什麼辦法,(沒有日食的工具,我需要自動執行的應用程序),以從映射實體? (甚至直接從映射數據庫)。

我不知道其他工具是否可以做到這一點,但我雖然會因爲交叉數據庫的兼容性而使用Hibernate。

謝謝! Guillaume

+0

只是添加更多的信息,我將永遠不會在應用程序中使用實體,我只需要基於其他數據庫生成數據庫,描述數據庫模式。 – Chaps 2014-08-29 09:05:15

回答

1

您可以使用maven在mojo plugins的幫助下通過提供您的hibernate映射文件來生成Java實體。

下面是一個簡單的Maven項目:

創建這種結構的你的Maven項目:

¦pom.xml 
¦ 
+---src 
¦ +---main 
¦  +---java 
¦  +---resources 
¦   ¦ hibernate.cfg.xml 
¦   ¦ 
¦   +---hbmFiles 
¦     Person.hbm.xml 

Hibernate的配置文件hibernate.cfg.xml放在YourProject/src/main/java/resources

映射文件應放置在resourcesresources文件夾的子目錄。

Person.hbm.xml文件的內容:hibernate.cfg.xml文件

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="com.mypackage.Person" table="person"> 
    <id name="id" column="id" type="int"/> 
    <property name="name" column="name" type="string"/> 
</class> 
</hibernate-mapping> 

內容:pom.xml文件

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <mapping resource="hbmFiles/Person.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

內容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>org.hibernate.tutorials</groupId> 
    <artifactId>MyProject</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <name>MyProject</name> 
    <url>http://maven.apache.org</url> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>hibernate3-maven-plugin</artifactId> 
      <version>2.2</version> 
      <executions> 
      <execution> 
       <phase>default</phase> 
       <goals> 
       <goal>hbm2java</goal> 
       </goals> 
      </execution> 
      </executions> 
      <configuration> 
      <components> 
       <component> 
       <name>hbm2java</name> 
       <implementation>configuration</implementation> 
       <outputDirectory>generated-sources/hibernate3</outputDirectory> 
       </component> 
      </components> 
      <componentProperties> 
       <drop>true</drop> 
       <ejb3>true</ejb3> 
       <jdk5>true</jdk5> 
      </componentProperties> 
      </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

酒店 - ejb3=true如果你需要的註釋是很有用的在你身上r生成java實體文件。如果不需要註解那麼你可以刪除行 - <ejb3>true</ejb3>

創建這3個文件後,可以運行命令:

mvn clean hibernate3:hbm2java 

現在行家在路徑生成的Java實體文件 -

YourProject/generated-sources/hibernate3/..package_mentioned_in_hbm_files../Student.java

+0

完美!我不知道那個pugin。 非常感謝! – Chaps 2014-08-29 12:44:56

+0

歡迎您! – Chaitanya 2014-08-29 12:48:40