Hibernate的generator
1、identity:用于MySql/db2数据库。特点:递增
<id name="id" column="id"> < generator class="identity"/> </id>
2、sequence:用于Oracle数据库
<id name="id" column="id"> <generator class="sequence"> <param name="sequence">序列名</param> </generator> </id>
3、assigned:用户自定义id;
<id name="id" column="id"> <generator class="assigned"/> </id>
4、foreign
<hibernate-mapping package="com.entity"><!-- 实体类所在的包名 -->
<class name="Cart" table="t_cart">
<id name="cartId" >
<generator class="foreign">
<param name="property">vipId</param>
</generator>
</id>
<property name="cartNum"></property>
<property name="cartPrice"></property>
<one-to-one name="vipId" class="com.entity.VipInfo" constrained="true" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
Hibernate配置
<?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.joyin.ticm.dc.repay.model.DcTrusRepay" table="DC_TRUS_REPAY">
<comment>委托本息处理与投资主体信息表</comment>
<id name="seqid" type="java.lang.Integer">
<column name="SEQID" />
<generator class="sequence">
<param name="sequence">S_DC_TRUS_REPAY</param>
</generator>
</id>
<timestamp name="lstmntdate" column="LSTMNTDATE" source="db" />
<property name="linkid" type="java.lang.String">
<column name="LINKID" length="40" />
</property>
<property name="reqid" type="java.lang.String">
<column name="REQID" length="40" />
</property>
<property name="pepaytype" type="java.lang.String">
<column name="PEPAYTYPE" length="10" />
</property>
<property name="recint" type="java.math.BigDecimal">
<column name="RECINT" precision="26" scale="4" />
</property>
<property name="trustamt" type="java.math.BigDecimal">
<column name="TRUSTAMT" precision="26" scale="4" />
</property>
<property name="totalamt" type="java.math.BigDecimal">
<column name="TOTALAMT" precision="26" scale="4" />
</property>
<property name="lstmntuser" type="java.lang.String">
<column name="LSTMNTUSER" length="20" />
</property>
<property name="effectflag" type="java.lang.String">
<column name="EFFECTFLAG" length="1" />
</property>
<property name="orgid" type="java.lang.String">
<column name="ORGID" length="20" />
</property>
<property name="orgname" type="java.lang.String">
<column name="ORGNAME" length="50" />
</property>
<property name="repayamt" type="java.math.BigDecimal">
<column name="REPAYAMT" precision="26" scale="4" />
</property>
<property name="field1" type="java.lang.String">
<column name="FIELD1" length="100" />
</property>
<property name="field2" type="java.lang.String">
<column name="FIELD2" length="100" />
</property>
<property name="field3" type="java.lang.String">
<column name="FIELD3" length="100" />
</property>
</class>
</hibernate-mapping>
BaseDao(重点是继承HibernateDaoSupport)
//全部是都是HibernateDaoSupport方法 Query query = this.getSession().createQuery(hql); query.setFirstResult(0); query.setMaxResults(max); return query.list(); Session session = getSession(); Object o = session.get(clazz, id); session.delete(o); session.flush(); getHibernateTemplate().save(object); getHibernateTemplate().flush(); getHibernateTemplate().saveOrUpdate(object); this.getSession().update(o); this.getSession().flush(); //根据对象ID查找实体对象 this.getHibernateTemplate().get(clazz, id);