<?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; } }