您的位置:首页 > 其它

Hibernate (三) 配置文件详解

2017-02-28 14:26 351 查看

主键生成策略详解:

先看代码入门demo中的

User.hbm.xml 的配置文件:

<hibernate-mapping package="cn.itcast.a_helloworld">
<!-- table 属性不写默认为类的简单名称 -->
<class name="User" table="t_user">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" type="string" column="name" length="20"/>
</class>
</hibernate-mapping>


这里主要讲解 id 的
<generator >
标签。

<generator class="native"/>


这里的 class=”native” 表示生成标签的策略:

hibernate提供了 如下的标签: (更加情况自己来选择使用)

uuid:由Hibernate自动生成UUID并指定为主键值

hilo,使用高低位算法生成主键值。只需要一张额外表,所有的数据都支持。

<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>


native:根据底层数据库的能力选择 identity、sequence 或者 hilo中的一个。

identity:使用数据库的自动增长策略,不是所有数据库都支持,比如oracle就不支持

sequence:在 DB2,PostgreSQL,Oracle,SAP DB,McKoi 中使用序列(sequence)

increment:由Hibernate维护的自动增长。 先查询当前最大的id值,再加1使用 (不推荐使用,因为在多线程下会问题)。

assigned:手工指定主键值

其他属性详解

针对下面的对象属性,写一个User.hbm.xml 配置

User.java

public class User {
private int id;
private String name;
private Integer age;
private Date birthday; // 生日
private String desc; // 一大段说明
private byte[] photo; // 头像图片
。。省略 get() set()。。。


User.hbm.xml配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.zll.hbm_property">

<!-- name属性:哪个类,如果上面的 package填写了包路径,这里可以填只填简单名称
table属性:对应哪个表,如果不写,默认的表名就是类的简单名称
-->
<class name="User" table="t_user">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<!-- 普通的属性(数据库中的基本类型,如字符串、日期、数字等)
name属性:对象中的属性名,必须要有。
type属性:类型,如果不写,Hibernate会自动检测。
可以写Java中类的全名。
或是写hibernate类型。
column属性:对应表中的列名,如果没有,默认为属性名。
length属性:长度,不是所有的类型都有长度属性,比如varchar有,但int没有,如果不写默认为255
not-null属性:非空约束,默认为false
-->
<!--
<property name="name"/>
-->
<property name="name" type="string" column="name" length="20" not-null="true"/>

<property name="age" type="int" column="age_"/>

<property name="birthday" type="date" column="birthday_"/>

<!-- 注意:
1、type类型 一般在大文本和 二进制的时候指定,其他情况用默认就可以。
2、当列表与关键字冲突时,可以通过column属性指定一个其他的列名。或是使用反引号包围起来。
3、指定使用text类型时,最好再指定length,以确定生成的SQL类型是能够存放指定数量的字符的。

<property name="desc">
<column name="desc_" length="5000" sql-type="text"/>
</property>
-->
<property name="desc" type="text" length="5000" column="`desc`" ></property>

<!-- 头像,二进制类型,最好指定长度 -->
<property name="photo" type="binary" length="102400"></property>
</class>
</hibernate-mapping>


测试代码:这里主要针对 图片二进制代码的存取:

public class TestImage {

private static SessionFactory sessionFactory;

static {
sessionFactory = new Configuration()//
.configure()// 读取配置文件
.addClass(User.class)//
.buildSessionFactory();
}

@Test
public void testSave() throws Exception {
// 读取图片文件
InputStream in = new FileInputStream( "c:/test.png");
byte[] photo = new byte[in.available()];
in.read(photo);
in.close();

// 创建对象实例
User user = new User();
user.setName("张三");
user.setAge(20);
user.setBirthday(new Date());
user.setDesc("一大段的说明,此处省略5000字……");
user.setPhoto(photo);

// 保存
Session session = sessionFactory.openSession(); // 打开一个新的Session
Transaction tx = session.beginTransaction(); // 开始事务

session.save(user);

tx.commit(); // 提交事务
session.close(); // 关闭Session,释放资源
}

@Test
public void testGet() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

User user = (User) session.get(User.class, 4); // 获取
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getDesc());
System.out.println(user.getPhoto());

OutputStream out = new FileOutputStream("c:/copy.png");
out.write(user.getPhoto());
out.close();

tx.commit();
session.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate