ssh中hibernate模板的普通sql使用
SessionFactory sessionFactory = this.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
String sql = "select l.l_name,count(*) as count from tb_customer as c ,tb_level as l where c.l_id = l.l_id group by l.l_id";
SQLQuery query = session.createSQLQuery(sql);
query.setResultTransformer(Transformers.aliasToBean(HashMap.class));
List<Map<Object,Object>> list = query.list();
Hibernate配置文件配置
核心配置文件(默认设置文件名为hibernate.cfg.xml,放置在src目录下,如果不是需要加载配置时候设置)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 基本四大参数
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test_frame</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库方言
#hibernate.dialect org.hibernate.dialect.MySQLDialect
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 其他辅助配置 -->
<!-- 自动创建表设置
#hibernate.hbm2ddl.auto create-drop
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- SQL语句显示设置
#hibernate.show_sql true
#hibernate.format_sql true
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 配置数据库的事物隔离级别
1-Read uncommitted isolation 读未提交,所有隔离问题窦唯解决
2-Read cimmitted isolation 读已提交,解决了脏读
4-Repeatable read isolation 可重复读,解决了脏读、不可重复读,未解决幻读/MySQL的默认事物隔离级别
8-Serializable isolation 序列化/串行化,解决了所有问题,但效率极低
#hibernate.connection.isolation 4
-->
<property name="hibernate.connection.isolation">4</property>
<!-- 将Session与本地线程ThreadLocal绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="cn/zz/hibernate/domain/Customer.hbm.xml"/>
<mapping resource="cn/zz/hibernate/domain/Linkman.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射配置文件(建议放置在对应实体类包下,名字为实体类名.hbm.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.zz.hibernate.domain.Customer" table="customer">
<!-- 基础单表配置 -->
<id name="cid" column="cid">
<generator class="uuid"></generator>
</id>
<property name="cname"></property>
<property name="address"></property>
<property name="level"></property>
<property name="source"></property>
<property name="telephone"></property>
<!-- 多对一表配置,多的一方 name:一的一方对应的Set集合来表示多的一方的属性名 cascade:级联操作,save-update开始级联存储,delete开启级联删除
inverse:倒转(如果为true,则不维护当前外键,反之维护) -->
<set name="linkmanSet" cascade="save-update,delete" inverse="true">
<!-- 对应的外键的字段名 -->
<key column="cid"></key>
<one-to-many class="cn.zz.hibernate.domain.Linkman" />
</set>
<!-- 多对一表配置,一的一方
name:表示多的一方对象的属性名
column:外键字段名称
cascade:联及操作,save-update开始联及存储,delete开启联及删除
inverse:倒转(如果为true,则不维护当前外键,反之维护)
-->
<many-to-one name="customer" class="cn.zz.hibernate.domain.Customer" column="cid" >
name:表示另一方对象Set集合的属性名
key:column表示第三张表中指向本类的外键名,foreign-key外建名
cascade:联及操作,save-update开始联及存储,delete开启联及删除
inverse:倒转(如果为true,则不维护当前外键,反之维护)
many-to-many:class表示对应另一方的实体类全路径,column表示第三张表中指向该类的外键名
-->
<set name="roleSet" table="user_role" cascade="save-update" inverse="false">
<key column="uid" foreign-key="uid_fk"></key>
<many-to-many class="cn.zz.hibernate.domain.Role" column="rid"></many-to-many>
</set>
</class>
</hibernate-mapping>
hibernat配置联合主键
<composite-id>
<key-many-to-one name="customer" class="cn.zz.crm.domain.Customer" column="c_id"></key-many-to-one>
<key-many-to-one name="user" class="cn.zz.crm.domain.User" column="u_id"></key-many-to-one>
</composite-id>