当前位置: 首页 > 工具软件 > Composite > 使用案例 >

composite-id使用

田曜瑞
2023-12-01

 

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;

        }

 

    }

 

}

 

 

 类似资料: