hibernate 过滤器

<hibernate-mapping>
    <class name="cn.com.transonline.data.HoProductCategory" table="HO_ProductCategory" catalog="HO">
        <id name="id" type="java.lang.Integer">
            <column name="Id" />
            <generator class="assigned" />
        </id>
        <property name="classId" type="java.lang.Integer">
            <column name="ClassID" />
        </property>
        <property name="parentId" type="java.lang.Integer">
            <column name="ParentID" />
        </property>
        <property name="name" type="java.lang.String">
            <column name="Name" length="40" />
        </property>
        <property name="isShow" type="java.lang.String">
            <column name="IsShow" length="1" />
        </property>
        <property name="industry" type="java.lang.String">
            <column name="Industry" length="1" />
        </property>
        <property name="levels" type="java.lang.Integer">
            <column name="Levels" />
        </property>
        <set name="productNames" inverse="true" cascade="all" >
            <key>
                <column name="Id" />
            </key>
            <one-to-many class="cn.com.transonline.data.HoProductParaLanguage" />
              <filter name="language" condition="languageId=:myFilterParam"> </filter>
        </set>
    </class>
    <filter-def name="language">
      <filter-param name="myFilterParam" type="string"/>
  	</filter-def>
</hibernate-mapping>



<hibernate-mapping>
    <class name="cn.com.transonline.data.HoProductParaLanguage" table="HO_ProductPara_language" catalog="HO">
        <composite-id>
            <key-property name="idType" type="java.lang.String" column="IdType"/>
        </composite-id>
        <property name="name" type="java.lang.String">
            <column name="Name" length="40" />
        </property>
        <property name="languageId" type="java.lang.String">
            <column name="LanguageId" length="2" />
        </property>
    </class>



public class HoProductCategory implements java.io.Serializable {

	// Fields

	private Integer id;
	private Integer classId;
	private Integer parentId;
	private String name;
	private String isShow;
	private String industry;
	private Integer levels;
	private Set productNames = new HashSet(0);
         get , set 方法...



public class HoProductParaLanguage implements java.io.Serializable {
	private String idType;
	private String name;
	private String languageId;
         get , set 方法...


public void query(){
		Configuration configuration = new Configuration();
		configuration.configure("/hibernate.cfg.xml");
	 	SessionFactory sessionFactory = configuration.buildSessionFactory();
	 	String HQL = "from HoProductCategory hy where hy.id=17";
		Session session = sessionFactory.openSession();
		try {
			session.enableFilter("language").setParameter("myFilterParam", "zh");
			System.out.println("----------------------------------");
			List list = session.createQuery(HQL).list();
			if(list.size()>0){
				Iterator iterator =  ((HoProductCategory)list.get(0)).getProductNames().iterator();
				while(iterator.hasNext()){
					HoProductParaLanguage anguage = (HoProductParaLanguage)iterator.next();
					System.out.println("type:"+anguage.getIdType());
					System.out.println("languageId:"+anguage.getLanguageId());
					System.out.println("name:"+anguage.getName());
				}
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
	}
</hibernate-mapping>

相关推荐