Hibernate各种映射类型

来源:互联网 发布:乌鲁木齐软件开发公司 编辑:程序博客网 时间:2024/06/05 14:26

People:

package model;import java.sql.Date;import java.sql.Timestamp;public class People {private Long id;private String username;private String password;private int telphone;private char gender; //'M','F'private boolean graduation;//true or falseprivate Date birthday;private Timestamp marryTime;private byte[] file;//二进制public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public int getTelphone() {return telphone;}public void setTelphone(int telphone) {this.telphone = telphone;}public char getGender() {return gender;}public void setGender(char gender) {this.gender = gender;}public boolean isGraduation() {return graduation;}public void setGraduation(boolean graduation) {this.graduation = graduation;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Timestamp getMarryTime() {return marryTime;}public void setMarryTime(Timestamp marryTime) {this.marryTime = marryTime;}public byte[] getFile() {return file;}public void setFile(byte[] file) {this.file = file;}}


package model;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Timestamp;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class HibernateTest {private static SessionFactory sessionFactory;static {try {sessionFactory = new Configuration().configure().buildSessionFactory();}catch(Exception ex) {ex.printStackTrace();}}public static void main(String[] args) throws IOException {People people = new People ();people.setUsername("zhangsan");people.setPassword("123456");people.setGender('F');people.setBirthday(new java.sql.Date(new java.util.Date().getTime()));people.setTelphone(98765);people.setMarryTime(new Timestamp(new java.util.Date().getTime()));people.setGraduation(true);InputStream is = new FileInputStream ("d:/build.xml");int length = is.available();byte[]buffer = new byte[length];is.read(buffer);is.close();people.setFile(buffer);Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();try {session.save(people);tx.commit();}catch (Exception ex) {ex.printStackTrace();if (null != tx) {tx.rollback();}}finally{session.close();}}}

映射文件:

<?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 = "model.People" table = "People"><id name = "id" column = "id" type = "long"><generator class="increment"></generator></id><property name = "username" column = "username" type = "string"></property><property name = "password" column = "password" type = "string"></property><property name = "telphone" column = "telphone" type = "int"></property><property name = "gender" column = "gender" type = "character"></property><property name="graduation" column = "graduation" type = "boolean"></property><property name = "birthday" column = "birthday" type = "date"></property><property name = "marryTime" column = "marryTime" type = "timestamp"></property><property name = "file" column = "file" type = "binary"></property></class></hibernate-mapping>


对于分页操作来说,需要知道如下一些信息:

当前正在操作的是第几页,每一页显示多少条记录。

主要使用Query中的setFirstResutl和setMaxResults方法。


对于Query接口的list()方法与iterator()方法来说,都可以实现获取查询的

对象 ,但是list()方法返回的每个对象都是完整的(对象中的每个属性都被

表中的字段填上了),而iterator()方法所返回的对象中仅包含了主键(标识符),

只有当你对iterator()中的对象操作时,hibernate才会向数据库再次发送SQL语句来获取对象的的属性值。

原创粉丝点击