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语句来获取对象的的属性值。
- Hibernate各种映射类型
- Hibernate各种映射类型
- Hibernate映射普通属性、Hibernate中的各种类型
- Hibernate映射类型
- Hibernate映射类型
- Hibernate的映射类型
- Hibernate的映射类型
- hibernate之映射类型
- hibernate mysql映射类型
- Hibernate类型映射
- Hibernate 映射类型
- Hibernate类型映射
- hibernate映射list类型
- Hibernate的映射类型
- Hibernate映射类型
- Hibernate映射值类型
- Hibernate映射类型
- Hibernate 内置映射类型
- SpringMVC配置,简单实例,文件上传与下载,ajax请求
- 单片机之IIC--AT24C256 多页读写关键程序段
- Excel中怎样用HYPERLINK函数设置连接
- 设计模式 - 工厂方法模式
- 字符串按单词(term)反转(C代码)
- Hibernate各种映射类型
- C#中使用WebBrowser显示嵌入资源
- Java.Web - Response、Request 随笔
- 拥抱大APP时代的到来-2013年5月江西IDC排行榜与综述(含汪照发先生专访)
- 我来啦嘻嘻
- 关于模型层编码
- MFC消息循环
- 开发者需知的10类工具
- linux 播放wav文件