HQL 常用

public List<Permissions> findPermissionsByRoleId(int roleId) { String sql="select role.Permissionss from Role role where role.ro_id=:roleId"; return sessionFactory.getCurrentSession().createQuery(sql).setParameter("roleId", roleId).list(); } Permissions 与 Role 是多对多的关系 查出一个Role 下有一个list<Permissions > hibernate 使用hql 做删除操作 /**删除公司行业中间映射记录 */ public void deleteCompanyIndustryRef(int companyId){ String hql="delete CompanyIndustryRef as cir where cir.company.companyId=?"; this.companyIndustryRefDao.getSessionFactory().getCurrentSession().createQuery(hql).setParameter(0, companyId).executeUpdate(); } 删除一个公司和公司行业中间表的信息记录。一定要写executeUpdate(); 否则不会发送sql 语句  

hibernate 命名查询

Student.hbm.xml

<?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 name="com.hkrt.domain.Student" table="Student"> <id name="id" column="id"> <generator class="identity" /> </id> <property name="name" /> <property name="age" /> <many-to-one name="school" column="school_id" not-null="true" ></many-to-one> </class> <!--hibernate命名查询 --> <query name="findStus" ><!--[CDATA[SELECT s FROM Student s where s.name like :sName ]]></query> <sql-query name="findStus1" ><![CDATA[SELECT * FROM Student s where s.name like :sName ]]--> <return class="com.hkrt.domain.Student"></return> </sql-query> </hibernate-mapping> 

 

@Test public void getList(){ SessionFactory sf = SessionFactoyUtil.getSessionFactory(); Session session = sf.openSession(); List<Student> stus = session.getNamedQuery("findStus1").setString("sName","joms").list(); System.out.println(stus.size()); for (Student student : stus) { System.out.println(student); } session.close(); } 

结果:

Hibernate: 

    SELECT

        * 

    FROM

        Student s 

    where

        s.name like ?

2

Id:3 name:joms age:23

Id:4 name:joms age:23

你可能感兴趣的:(Hibernate,session,String,generator,encoding,permissions)