Hibernate单表操作

来源:互联网 发布:阿里云邮箱人工客服 编辑:程序博客网 时间:2024/05/21 10:25

1、主键的设置

这里写图片描述

2、时间类型的对应关系

<property name="date" type="java.util.Date">     <column name="DATE" /></property>

在配置文件中时间的类型可以写java文件中所设定的类型,也可以写映射类型

<property name="date" type=date>      <column name="DATE" /></property>

这里写图片描述

3、存储大文件,如图片的对象类型映射

这里写图片描述
存储图片的代码如下:

Student s = new Student("王五", 93, new Date());//file.separator代表分割符File file = new File("d:"+File.separator+"1.jpg");InputStream input = new FileInputStream(file);//input.available(),表示图片在流中的长度Blob image = Hibernate.getLobCreator(session)                    .createBlob(input, input.available());s.setPicture(image);session.save(s);

读取图片的代码如下:

Student student = session.get(Student.class, 1);Blob image = student.getPicture();InputStream input = image.getBinaryStream();File file = new File("d:"+File.separator+"2.jpg");OutputStream output = new FileOutputStream(file);byte[] buff = new byte[input.available()];//把数据写进buff数组中input.read(buff);//从buff数组中把信息读出来output.write(buff);

4、组件属性

这里写图片描述
组件也可以看做是one-to-one,但如果使用one-to-one,就需要再建立一张表,建立外键的参照关系,而建立表的连接,在查询时会更耗时。所以才将组件看做该对象的一个属性,但组件是自己建立的,不是基本数据类型,所以配置时用component标签。

原创粉丝点击