1 动态模型

1.1 说明

动态模型是指模型的属性是不固定的,可以添加和变更;也指不同模型,可以增加模型。不同模型在存储上为了性能和隔离,使用不同的表。但逻辑编写为了复用,一般使用同一个java类型。以下介绍如何对应。

1.2 java类

java类分固定属性和动态属性,固定属性直接建立属性即可,动态属性使用map类型。例如下面例子:

public class DynamicEntity extends BaseEntity {

    /**
     * 
     */
    private static final long serialVersionUID = 5907806428272321127L;

    /**
     * 
     */
    private Map<String, Object> dynamicProperties = new HashMap<String, Object>();

    /**
     * 获取dynamicProperties.
     * 
     * @return the dynamicProperties
     */
    public Map<String, Object> getDynamicProperties() {
        return dynamicProperties;
    }

    /**
     * 设置dynamicProperties.
     * 
     * @param dynamicProperties
     *            the dynamicProperties to set
     */
    public void setDynamicProperties(Map<String, Object> dynamicProperties) {
        this.dynamicProperties = dynamicProperties;
    }

}


public class YwEntity extends DynamicEntity {

    /**
     * 
     */
    private static final long serialVersionUID = -7655851107507073267L;


    private Integer gid;

    /**
     * billid .
     * 
     */
    private Integer billid;

    /**
     * 获取billid.
     * 
     * @return the billid
     */
    public Integer getBillid() {
        return billid;
    }

    /**
     * 设置billid.
     * 
     * @param billid
     *            the billid to set
     */
    public void setBillid(Integer billid) {
        this.billid = billid;
    }


}

1.3 hbm配置文件

由于一个模型对应多个数据库表,需使用 entityName 查询。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class entity-name="entity1001" name="com.demo.YwEntity"
        table="entity1001">
        <id name="gid" type="java.lang.Integer">
            <generator class="assigned" />
        </id>
        <property name="gid" type="java.lang.String">
            <column name="gid" />
        </property>
        <property name="billid" type="java.lang.Integer">
            <column name="billid" />
        </property>
        <dynamic-component insert="true" name="dynamicProperties"
            optimistic-lock="true" unique="false" update="true">
            <property name="rq" type="java.util.Date">
                <column name="rq" />
            </property>
            <property name="ZBXM1" type="java.lang.String">
                <column name="ZBXM1" />
            </property>
            <property name="ZBXM2" type="java.lang.String">
                <column name="ZBXM2" />
            </property>
            <property name="ZBXM4" type="java.lang.String">
                <column name="ZBXM4" />
            </property>
            <property name="ZBXM5" type="java.lang.String">
                <column name="ZBXM5" />
            </property>
            <set name="ZB" cascade="save-update" inverse="true" lazy="false">
                <key>
                    <column name="gid" />
                </key>
                <one-to-many entity-name="entity1001.ZB" />
            </set>
            <set name="ZB1" cascade="save-update" inverse="true" lazy="false">
                <key>
                    <column name="gid" />
                </key>
                <one-to-many entity-name="entity1001.ZB1" />
            </set>
        </dynamic-component>
    </class>
    <class entity-name="entity1001.ZB" name="com.demo.Sub"
        table="entity1001_ZB">
        <composite-id>
            <key-property name="gid" type="java.lang.Integer"></key-property>
            <key-property name="billid1" type="java.lang.Integer"></key-property>
        </composite-id>
        <property name="gid" type="java.lang.String">
            <column name="gid" />
        </property>
        <property name="billid" type="java.lang.Integer">
            <column name="billid" />
        </property>
        <dynamic-component insert="true" name="dynamicProperties"
            optimistic-lock="true" unique="false" update="true">
            <property name="BH" type="java.lang.String">
                <column name="BH" />
            </property>
        </dynamic-component>
    </class>
    <class entity-name="entity1001.ZB1" name="com.demo.Sub"
        table="entity1001_ZB1">
        <composite-id>
            <key-property name="gid" type="java.lang.Integer"></key-property>
            <key-property name="billid1" type="java.lang.Integer"></key-property>
        </composite-id>
        <property name="gid" type="java.lang.String">
            <column name="gid" />
        </property>
        <property name="billid" type="java.lang.Integer">
            <column name="billid" />
        </property>
        <dynamic-component insert="true" name="dynamicProperties"
            optimistic-lock="true" unique="false" update="true">
            <property name="ZXM1" type="java.lang.String">
                <column name="ZXM1" />
            </property>
            <property name="ZXM3" type="java.lang.String">
                <column name="ZXM3" />
            </property>
            <property name="ZXM4" type="java.lang.String">
                <column name="ZXM4" />
            </property>
        </dynamic-component>
    </class>
</hibernate-mapping>

1.4 保存查询

//保存
String entityName = EntityUtil.getEntityName(po);
hibernateDao.getPojoTemplate().saveOrUpdate(entityName, po);

//查询
String entityName = EntityUtil.getEntityName(classId, typeId);
return hibernateDao.getPojoTemplate().get(entityName, key);

1.5 子表复合主键

子表模型实现 hashCode, equals方法。

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((billid1 == null) ? 0 : billid1.hashCode());
        result = prime * result + ((gid== null) ? 0 : gid.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Sub other = (Sub) obj;
        if (billid1 == null) {
            if (other.billid1 != null)
                return false;
        } else if (!billid1.equals(other.billid1))
            return false;
        if (ywid == null) {
            if (other.gid!= null)
                return false;
        } else if (!gid.equals(other.gid))
            return false;
        return true;
    }

查询如下

        Sub key = new Sub();
        key.setBillid(vo.getBillid());
        key.setGid(vo.getGid());

        Sub po = (Sub) EntityUtil.get(vo, key);
Logo

获取更多汽车电子技术干货

更多推荐