由hibernate映射文件生产对应的pojo类举例

这里仅仅是学习总结,请大家接纳。

由hibernate映射文件生产对应的pojo类:

使用工具:  hbm2java

hbm文件:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="org.redsaga.quickstart.addressAssigner" >
    <class name="AddressDataEntry" table="ADDRESS_DATA_T" dynamic-update="true"  lazy="false">
        <id name="ipAddressAsLong" column="id" type="long">
            <generator class="assigned"/>
        </id>
        <property name="macAddress" column="macAddress" type="string" length="255" not-null="true"/>
        <property name="releaseTime" column="releaseTime" type="long"/>
        <property name="interfaceId" column="interfaceId" type="long" not-null="true"/>
        <property name="serviceInfoId" column="serviceInfoId" type="integer"/>
    </class>
</hibernate-mapping>

现在利用hbm2java工具自动生成 AddressDataEntry.java 文件:
这里使用在 ant 文件中加入 hbm2java 来完成,需要在工程的build.xml文件中加入如下代码
   <target name="hbm2java" description="Generate .java from .hbm files.">
      <taskdef name="hbm2java" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
         classpathref="lib.class.path"/>
		
		<hbm2java output="${build.gen-src.dir}">
			<fileset dir="${build.gen-src.dir}">
				<include name="**/*.hbm.xml"/>
			</fileset>
		</hbm2java>
   </target>
<!-- 这里需要定义一个 build.gen-src.dir目录,一般可以为:../../src ,还有定义一个classpath: lib.class.path -->

之后运行build.xml文件的 hbm2java 在定义的 package 里将会生成对应的 AddressDataEntry.java 文件 。其实这里将会生成在build.gen-src.dir目录下所有hbm.xml对应的 xxx.java 文件。这里生成的 AddressDataEntry.java 如下:
package org.redsaga.quickstart.addressAssigner;

import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class AddressDataEntry implements Serializable {

    /** identifier field */
    private long ipAddressAsLong;

    /** persistent field */
    private String macAddress;

    /** nullable persistent field */
    private Long releaseTime;

    /** persistent field */
    private long interfaceId;

    /** nullable persistent field */
    private Integer serviceInfoId;

    /** full constructor */
    public AddressDataEntry(long ipAddressAsLong, String macAddress, Long releaseTime, long interfaceId, Integer serviceInfoId) {
        this.ipAddressAsLong = ipAddressAsLong;
        this.macAddress = macAddress;
        this.releaseTime = releaseTime;
        this.interfaceId = interfaceId;
        this.serviceInfoId = serviceInfoId;
    }

    /** default constructor */
    public AddressDataEntry() {
    }

    /** minimal constructor */
    public AddressDataEntry(long ipAddressAsLong, String macAddress, long interfaceId) {
        this.ipAddressAsLong = ipAddressAsLong;
        this.macAddress = macAddress;
        this.interfaceId = interfaceId;
    }

    public long getIpAddressAsLong() {
        return this.ipAddressAsLong;
    }

    public void setIpAddressAsLong(long ipAddressAsLong) {
        this.ipAddressAsLong = ipAddressAsLong;
    }

    public String getMacAddress() {
        return this.macAddress;
    }

    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }

    public Long getReleaseTime() {
        return this.releaseTime;
    }

    public void setReleaseTime(Long releaseTime) {
        this.releaseTime = releaseTime;
    }

    public long getInterfaceId() {
        return this.interfaceId;
    }

    public void setInterfaceId(long interfaceId) {
        this.interfaceId = interfaceId;
    }

    public Integer getServiceInfoId() {
        return this.serviceInfoId;
    }

    public void setServiceInfoId(Integer serviceInfoId) {
        this.serviceInfoId = serviceInfoId;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("ipAddressAsLong", getIpAddressAsLong())
            .toString();
    }

}




你可能感兴趣的:(java,apache,Hibernate,xml,ant)