composite-id使用

 

composite-id

第一要对model进行如下改造:

将staffskillId,staffId 封装成composite-id 形成class SkillTableKey

如下所示:

package com.sunnyever.model;

 

public class SkillTableKey extends com.sunnyever.model.BaseObject implements java.io.Serializable {

    private long staffskillId;

 

    private long staffId;

 

    public SkillTableKey() {

 

    }

/**

 * @hibernate.property name="staffId" type="java.long.long"  column="STAFF_ID"

 */

    public long getStaffId() {

       return staffId;

    }

 

    public void setStaffId(long staffId) {

       this.staffId = staffId;

    }

    /**

     * @hibernate.property name="staffskillId" type="java.long.long"  column="staffskill_id"

     */

    public long getStaffskillId() {

       return staffskillId;

    }

 

    public void setStaffskillId(long staffskillId) {

       this.staffskillId = staffskillId;

    }

 

    @Override

    public int hashCode() {

       final int PRIME = 31;

       int result = 1;

       result = PRIME * result + (int) (staffId ^ (staffId >>> 32));

       result = PRIME * result + (int) (staffskillId ^ (staffskillId >>> 32));

       return result;

    }

 

    @Override

    public boolean equals(Object obj) {

       if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (getClass() != obj.getClass())

           return false;

       final SkillTableKey other = (SkillTableKey) obj;

       if (staffId != other.staffId)

           return false;

       if (staffskillId != other.staffskillId)

           return false;

       return true;

    }

    @Override

    public String toString() {

       // TODO Auto-generated method stub

       return null;

    }

}
第二,将composite-id导入 class SkillTable中设为主键

 

/**

 * @hibernate.id class="com.sunnyever.model.SkillTableKey"

 *

 */

 

    public SkillTableKey getKey() {

       return key;

    }

 

下面为class SkillTable:

**

 * @struts.form include-all="true" extends="BaseForm"

 * @hibernate.class

 *         table="SKILL_TABLE"

 *    

 */

 

public class SkillTable extends com.sunnyever.model.BaseObject implements java.io.Serializable {

 

 

    // Fields   

    private SkillTableKey  key;

 

    private String skill;

    private String level1;

 

 

    // Constructors

 

    /** default constructor */

    public SkillTable() {

    }

 

 

    /** full constructor */

    public SkillTable(SkillTableKey  key, String skill, String level1) {

        this.key=key;

        this.skill = skill;

        this.level1 = level1;

    }

 

/**

 * @hibernate.id class="com.sunnyever.model.SkillTableKey"

 *

 */

 

    public SkillTableKey getKey() {

       return key;

    }

 

 

    public void setKey(SkillTableKey key) {

       this.key = key;

    }

 

 

    /** 

     *        

     *                @hibernate.property

     *                 column="SKILL"

     *                 type="java.lang.string"

     */

 

    public String getSkill() {

        return this.skill;

    }

 

    public void setSkill(String skill) {

        this.skill = skill;

    }

    /**  

     *        

     *                @hibernate.property

     *                 column="LEVEL1"

     *             type="java.lang.string"

     */

 

    public String getLevel1() {

        return this.level1;

    }

 

    public void setLevel1(String level1) {

        this.level1 = level1;

    }

 

 

    @Override

    public int hashCode() {

       final int PRIME = 31;

       int result = 1;

       result = PRIME * result + ((key == null) ? 0 : key.hashCode());

       result = PRIME * result + ((level1 == null) ? 0 : level1.hashCode());

       result = PRIME * result + ((skill == null) ? 0 : skill.hashCode());

       return result;

    }

第三 在dao增加public List findByIds( SkillTableKey key)

通过SkillTableKey 作为 papm获取

package com.sunnyever.dao;

 

import java.util.List;

 

import com.sunnyever.dao.Dao;

import com.sunnyever.model.SkillTable;

import com.sunnyever.model.SkillTableKey;

 

public interface SkillTableDao extends Dao {

 

    /**

     * Retrieves all of the skillTables

     */

    public List getSkillTables(SkillTable skillTable);

 

    /**

     *find only staff's all skills

     *

     * SkillTableKey is a papm that is  the skillTable's foregin key

     * @return skillTable populated skillTable object

     */

    public List findByIds( SkillTableKey key);

}

第四在class SkillTableDaoHibernate 中实现List findByIds( SkillTableKey key);

 

package com.sunnyever.dao.hibernate;

 

import java.util.List;

 

import com.sunnyever.dao.hibernate.BaseDaoHibernate;

import com.sunnyever.model.SkillTable;

import com.sunnyever.model.SkillTableKey;

import com.sunnyever.dao.SkillTableDao;

 

import org.hibernate.Query;

import org.springframework.orm.ObjectRetrievalFailureException;

 

public class SkillTableDaoHibernate extends BaseDaoHibernate implements SkillTableDao {

 

    /**

     * @see com.sunnyever.dao.SkillTableDao#getSkillTables(com.sunnyever.model.SkillTable)

     */

    public List getSkillTables(final SkillTable skillTable) {

        return getHibernateTemplate().find("from SkillTable");

 

        /* Remove the line above and uncomment this code block if you want

           to use Hibernate's Query by Example API.

        if (skillTable == null) {

            return getHibernateTemplate().find("from SkillTable");

        } else {

            // filter on properties set in the skillTable

            HibernateCallback callback = new HibernateCallback() {

                public Object doInHibernate(Session session) throws HibernateException {

                    Example ex = Example.create(skillTable).ignoreCase().enableLike(MatchMode.ANYWHERE);

                    return session.createCriteria(SkillTable.class).add(ex).list();

                }

            };

            return (List) getHibernateTemplate().execute(callback);

        }*/

    }

 

    /**

     * @see com.sunnyever.dao.SkillTableDao#getSkillTable(long staffskillId)

     */

    public List findByIds( SkillTableKey key) {

        log.debug("getting SkillTable instance with id: " + key);

        try {

 

 

            String hql="from SkillTable m where m.id.staffId=:staffId ";

            Query query= getSession().createQuery(hql);

            query.setProperties(key);

 

            return query.list();

        } catch (RuntimeException re) {

            log.error("get failed", re);

            throw re;

        }

 

    }

 

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值