hibernate基本类型映射

时间:2021-01-13 22:45:20

hibernate基本类型映射

hibernate基本类型映射

model包:

Book.java:

package model;

import java.sql.Blob;
import java.util.Date;

public class Book {
    private int id;
    private String bookName;//图书名称
    private float price;//图书价格
    private boolean specialPrice;//是否是特价
    private Date publishDate;//发布日期
    private String author;//作者
    private String introduction;//简介
    private Blob bookPic;//图书图片
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public boolean isSpecialPrice() {
        return specialPrice;
    }
    public void setSpecialPrice(boolean specialPrice) {
        this.specialPrice = specialPrice;
    }
    public Date getPublishDate() {
        return publishDate;
    }
    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getIntroduction() {
        return introduction;
    }
    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }
    public Blob getBookPic() {
        return bookPic;
    }
    public void setBookPic(Blob bookPic) {
        this.bookPic = bookPic;
    }
    
    
}

Book.hbm.xml:

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

<hibernate-mapping package="model">

<class name="Book" table="t_book">
        <id name="id" column="bookId">
            <generator class="native"></generator>
        </id>
        <property name="bookName" column="bookName" length="40"></property>
        <property name="price" column="price" type="float"></property>
        <property name="specialPrice" column="specialPrice" type="boolean"></property>
        <property name="publishDate" column="publishDate" type="date"></property>
        <property name="author" column="author" length="20"></property>
        <property name="introduction" column="introduction" type="text"></property>
        <property name="bookPic" column="bookPic" type="blob"></property>
    </class>

</hibernate-mapping>

service包:

BookTest.java:

package service;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.text.SimpleDateFormat;

import model.Book;

import org.hibernate.LobHelper;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class BookTest {
    public static void main(String[] args) throws Exception{
        SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        
        Book book=new Book();
        book.setBookName("java编程思想");
        book.setPrice(100);
        book.setSpecialPrice(true);
        book.setPublishDate(new SimpleDateFormat("yyyy-MM-dd").parse("2013-1-1"));
        book.setAuthor("埃克尔");
        book.setIntroduction("简介。。。。。");
        
        LobHelper lobHelper=session.getLobHelper();
        InputStream in=new FileInputStream("c://123.jpg");
        Blob bookPic=lobHelper.createBlob(in,in.available());
        book.setBookPic(bookPic);
        
        session.save(book);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }
}