开发者

generating class from hibernate mapping file

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>
    <clas开发者_高级运维s 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...


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


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


Use Hibernate tools with documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜