dtd:约束xml标签名字
<hibernate-mapping
auto-import="true|false" //hql的实体类是否自动导入,默认自动导入
package="package.name" //全局的包
/>
<class
name="ClassName" pojo类名
table="tableName" 表名 discriminator-value="discriminator_value" 鉴别值
//update语句时包括所有字段,有些没有改变的也都放在里面,如果设置了,只显示改变的字段。但是效率不是很好,parameterStatement每此操作不能复用了.
dynamic-update="true|false" (8)
//如果字段为null不会放到insert中
dynamic-insert="true|false" (9)
batch-size="N" (14)
optimistic-lock="none|version|dirty|all" (15)
lazy="true|false" (16)
abstract="true|false" (21)
/>
<id
name="propertyName" //实体类的属性
type="typename" //Hibernate类型 的string: 从java.lang.String 到 VARCHAR (或者 Oracle的 VARCHAR2)的映射。 5.2.2
column="column_name"// 给字段重命名
length=”32”//设置表字段的长度
<generator class="generatorClass"/>//生成策略uuid(32位) native assigned increment(集群的时候不能用这个) identity(自增)
</id>
* Identity:DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的自增
* sequence :在DB2,PostgreSQL, Oracle, SAP DB, McKoi的自增
* Uuid:32位不重复字符串,hibernate做的
* Native:根据底层数据库的能力选择identity, sequence 或者hilo中的一个
* assigned:手动分配主键
* foreign :使用另外一个相关联的对象的标识符。通常和<one-to-one>联合起来使用。
*guid:只能在mysql mssql中使用 16位字符串
*select:与触发器有关
*foreign:ref....
*5.1.9
<property
name="propertyName"
column="column_name"
type="typename"
update="true|false" //配置了这个字段永远不会出现在update中
insert="true|false"
lazy="true|false"
unique="true|false"//唯一性
not-null="true|false"//设置非空
length="L"
/>
*uuid:此时id必须为字符串类型,hibernate做的
public void testSave1(){
Session session = null;
User1 user = null;
Transaction tx = null;
try{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
//Transient 瞬时状态
user = new User1();
user.setName("uuid,unique");
user.setPassword("123456");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
<!-- 默认存的表和实体类名字一样 -->
<class name="User1" table="t_user1">
<!-- 单一主键映射 id name="" -->
<!-- id 必须配在第一个 两个及以上主键不用id -->
<!-- column 给字段重命名 type给字段指定类型 注意:小写 string lang-->
<id name="id" column="user1_id" length="32">
<!-- uuid全局唯一标识,即自动生成不会重复的 -->
<!-- uuid效率比较高,32位字符串是由hiernate生成的 -->
<generator class="uuid"></generator>
</id>
<!-- 普通属性用property,字段的属性Hibernate自动生成 -->
<!-- unique:不能重复,not-null:不能为空,length:允许的字段长度 -->
<property name="name" unique="true" not-null="true" length="20"/>
<property name="password" not-null="true" length="10"/>
<property name="createTime" column="create_Time"/>
<property name="expireTime" column="expire_Time"/>
</class>
姓名相同的就存不进了
*native:此时id必须为数值型,数据库做的
<class name="User2" table="t_user2">
<!-- 单一主键映射 id name="" -->
<!-- id 必须配在第一个 两个及以上主键不用id -->
<!-- column 给字段重命名 type给字段指定类型 注意:小写 string lang-->
<id name="id" column="user2_id" >
<!-- native自增 -->
<!-- 自增是由数据库完成:效率比较低,hibernate完成的效率比较高,例如uuid -->
<generator class="native"></generator>
</id>
<!-- 普通属性用property,字段的属性Hibernate自动生成 -->
<!-- unique:不能重复,not-null:不能为空,length:允许的字段长度 -->
<property name="name" unique="true" not-null="true" length="20"/>
<property name="password" not-null="true" length="10"/>
<property name="createTime" column="create_Time"/>
<property name="expireTime" column="expire_Time"/>
</class>
public void testSave2(){
Session session = null;
User2 user = null;
Transaction tx = null;
try{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
//Transient 瞬时状态
user = new User2();
user.setName("unique不能重复dd");
user.setPassword("123456");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
*assigned:
<class name="User3" table="t_user3">
<!-- 单一主键映射 id name="" -->
<!-- id 必须配在第一个 两个及以上主键不用id -->
<!-- column 给字段重命名 type给字段指定类型 注意:小写 string lang-->
<id name="id" column="user3_id">
<!-- assigned 手动分配 -->
<generator class="assigned"></generator>
</id>
<!-- 普通属性用property,字段的属性Hibernate自动生成 -->
<!-- unique:不能重复,not-null:不能为空,length:允许的字段长度 -->
<property name="name" unique="true" not-null="true" length="20"/>
<property name="password" not-null="true" length="10"/>
<!-- 字段第二次改名字时,当配了update 则原来的字段放在一边 -->
<property name="createTime" column="createTime"/>
<property name="expireTime" column="expireTime"/>
</class>
public void testSave3(){
Session session = null;
User3 user = null;
Transaction tx = null;
try{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
//Transient 瞬时状态
user = new User3();
user.setId("0001");
user.setName("unique不能重复dd");
user.setPassword("123456");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
tx.commit();
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
*注意:如果实体类和实体类中的属性和sql中的关键字重复,必须采用table或column重新命名
*实体类的设计原则:
实现一个默认的(即无参数的)构造方法(constructor)
提供一个标识属性(identifier property)(可选)
使用非final的类 (可选)
为持久化字段声明访问器(accessors)
*配置文件中:
<!-- 表没有时 自动创建表,不用ExportDB了 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 字段第二次改名字时,当配了update(就是上面的配置) 则原来的字段放在一边 -->
<property name="createTime" column="createTime2"/>