动态模型

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);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风九天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值