views:

72

answers:

3

hi, i have one mapping file viz. student.hbm.xml.. i need to generate Student.java from the same. the file is below :-

<?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>

is there any way i can create the class file from the above file.please help...

+1  A: 

Try hibernate tools using hbm2java ant target. Example 1 and 2

n002213f
+2  A: 

You need Hibernate Tools (install it in eclipse).

OR

Develop custom maven plugin... (sample code available below)

/**
 * 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
becomputer06
can u please tell me from where to get the maven jar.
Mrityunjay
Can you please elaborate your question?Btw, you can get all most all jars from http://repo1.maven.org/maven2/.- SE
becomputer06
+1  A: 

Use Hibernate tools with documentation

Mark