Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate,从映射文件生成实体和数据库_Java_Hibernate - Fatal编程技术网

Java Hibernate,从映射文件生成实体和数据库

Java Hibernate,从映射文件生成实体和数据库,java,hibernate,Java,Hibernate,我想知道是否可以从hibernate映射文件生成实体。 我的最终目标是通过编程创建一个映射文件,然后从中创建数据库。 我知道我可以使用: <prop key="hibernate.hbm2ddl.auto">create</prop> 创建 创建数据库表单实体,但是有没有一种方法(没有eclipse工具,我需要在应用程序中自动创建)从映射生成实体?(甚至可以直接从映射数据库) 我不知道其他工具是否可以做到这一点,但我想我会使用Hibernate,因为它具有跨数据库兼容

我想知道是否可以从hibernate映射文件生成实体。 我的最终目标是通过编程创建一个映射文件,然后从中创建数据库。 我知道我可以使用:

<prop key="hibernate.hbm2ddl.auto">create</prop>
创建
创建数据库表单实体,但是有没有一种方法(没有eclipse工具,我需要在应用程序中自动创建)从映射生成实体?(甚至可以直接从映射数据库)

我不知道其他工具是否可以做到这一点,但我想我会使用Hibernate,因为它具有跨数据库兼容性

谢谢大家!!
Guillaume

您可以在的帮助下使用maven,通过提供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

映射文件应放在
resources
resources
文件夹的子目录中

Person.hbm.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>
<?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>
pom.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>
<?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>
现在,maven在路径-


YourProject/generated sources/hibernate3/.package\u在\u hbm\u文件中提到了\u../Student.java

添加更多信息,我将永远不会在应用程序中使用实体,我只需要基于其他数据库生成一个描述数据库模式的数据库。太完美了!我不知道那个呕吐物。非常感谢!