2010-06-08 111 views
3

我有一個映射文件即, student.hbm.xml ..我需要從相同的生成Student.java。該文件是如下: -從休眠映射文件生成類

<?xml version="1.0" encoding="UTF-8"?> 
<hibernate-mapping> 
    <class name="org.hibernate.entity.ClassRoom" table="class_room"> 
     <id name="roomId" column="room_id" type="int"/> 
     <property name="roomClass" column="room_class" type="string"/> 
     <property name="floor" column="floor" type="int"/> 
     <property name="roomMaster" column="room_mast" type="string"/> 
    </class> 
</hibernate-mapping> 

有沒有什麼辦法可以創建從上述file.please幫助類文件...

+0

的可能重複[POJO來自HBM文件](http://stackoverflow.com/questions/ 2711408/pojo-from-hbm-file) – naXa 2014-12-02 13:30:44

回答

2

您需要Hibernate Tools(將其安裝在eclipse中)。

OR

開發定製的Maven插件...(以下提供的示例代碼)

/** 
* Generate POJO from *.hbm.xml 
* Example Usage: mvn prefix:hbm2pojo OR 
*    mvn prefix:hbm2pojo -Dexec.args="com.comp.Product,com.comp.Item" 
* 
* @goal hbm2pojo 
*/ 
public class GenerateHibernatePojoMojo extends AbstractMojo 
{ 
    /** Directory for hibernate mapping files 
    * @parameter expression="${basedir}/src/main/resources" 
    * @required 
    */ 
    private File hbmDirectory; 

    /** Output directory for POJOs 
    * @parameter expression="${project.build.sourceDirectory}" 
    * @required 
    */ 
    private File outputDirectory; 

    /** set to true if collections need to use generics. Default is false. 
    * @parameter expression="${jdk5}" default-value="false" 
    * @optional 
    */ 
    private String jdk5; 

    public void execute() throws MojoExecutionException, MojoFailureException 
    { 
     POJOExporter exporter = new POJOExporter(); 
     exporter.setOutputDirectory(outputDirectory); 

     Configuration config = new Configuration(); 
     config.setProperty("jdk5", jdk5); 

     String args = System.getProperty("exec.args"); 
     if (args != null && !"".equals(args)) 
     { 
      String[] entityNames = args.split(","); 
      for(String entityName : entityNames) 
      { 
       File hbmFile = new File(hbmDirectory + "/" + entityName.replace('.', '/') + ".hbm.xml"); 
       config.addFile(hbmFile); 
      } 
     } 
     else 
     { 
      config.addDirectory(hbmDirectory); 
     } 
     exporter.setConfiguration(config); 
     exporter.start(); 
     // TODO this guy also generates unwanted POJOs like POJO of component 
     // TODO Add support for Java 5 Generic 
    } 

} 
  • SE
+0

你能告訴我從哪裏得到maven jar。 – 2010-06-08 13:02:39

+0

你能否詳細說明你的問題? 順便說一句,你可以從http://repo1.maven.org/maven2/獲得所有大多數的罐子。 - - SE – dira 2010-06-09 04:43:49