3

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

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

can u please tell me from where to get the maven jar.
Can you please elaborate your question? Btw, you can get all most all jars from repo1.maven.org/maven2. - SE
1

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

Comments

1

Use Hibernate tools with documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.