菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
270
0

吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Hibernate使用@Lob、@Basic修饰大数类型的属性

原创
05/13 14:22
阅读数 14856
<?xml version="1.0" encoding="GBK"?>
<project name="hibernate" basedir="." default="">
    <property name="src" value="src"/>
    <property name="dest" value="classes"/>

    <path id="classpath">
        <fileset dir="../../lib">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${dest}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dest}"/>
        <mkdir dir="${dest}"/>
        <copy todir="${dest}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="${dest}" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
            <compilerarg value="-Xlint:deprecation"/>
        </javac>
    </target>

    <target name="run" description="Run the main class" depends="compile">
        <java classname="lee.PersonManager" fork="yes" failonerror="true">
            <classpath refid="classpath"/>
        </java>
    </target>

</project>
package lee;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;
import org.crazyit.app.domain.*;
import java.io.*;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class PersonManager
{
    public static void main(String[] args)
        throws Exception
    {
        // 实例化Configuration,
        Configuration conf = new Configuration()
            .configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(conf.getProperties()).build();
        // 以Configuration实例创建SessionFactory实例
        SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
        // 创建Session
        Session sess = sf.openSession();
        // 开始事务
        Transaction tx = sess.beginTransaction();
        // 创建Person对象
        Person person = new Person();
        // 为Person对象的属性设置值
        person.setName("crazyit.org");
        File file = new File("logo.jpg");
        byte[] content = new byte[(int)file.length()];
        new FileInputStream(file).read(content);
        person.setPic(content);
        // 保存Person对象
        sess.save(person);
        // 提交事务
        tx.commit();
        // 关闭Session
        sess.close();
        sf.close();
    }
}
package org.crazyit.app.domain;

import javax.persistence.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
@Entity
@Table(name="person_inf")
public class Person
{
    @Id // 用于修饰标识属性
    // 指定该主键列的主键生成策略
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    // @Column指定该属性映射的列信息,此处指定了列名、长度
    @Column(name="person_name" , length=50)
    private String name;
    @Lob
    @Basic(fetch=FetchType.LAZY)
    @Column(nullable=true)
    private byte[] pic;

    // id的setter和getter方法
    public void setId(Integer id)
    {
        this.id = id;
    }
    public Integer getId()
    {
        return this.id;
    }

    // name的setter和getter方法
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // pic的setter和getter方法
    public void setPic(byte[] pic)
    {
        this.pic = pic;
    }
    public byte[] getPic()
    {
        return this.pic;
    }
}

 

发表评论

0/200
270 点赞
0 评论
收藏
为你推荐 换一批