您的位置:首页 > 其它

hibernate组件属性

2015-10-27 10:23 218 查看
Address.java

public class Address {

private String postcode;

private String phone;

private String address;

public Address() {

}

public Address(String postcode, String phone, String address) {

this.postcode = postcode;

this.phone = phone;

this.address = address;

}

public String getPostcode() {

return postcode;

}

public void setPostcode(String postcode) {

this.postcode = postcode;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

}

Students.java

import java.sql.Blob;

import java.util.Date;

public class Students {

private int sid;

private String sname;

private String gender;

private Date birthday;

//private String address;

private Blob picture;

private Address address;

public Students() {

}

public Students(int sid, String sname, String gender, Date birthday) {

this.sid = sid;

this.sname = sname;

this.gender = gender;

this.birthday = birthday;

}

public int getSid() {

return sid;

}

public void setSid(int sid) {

this.sid = sid;

}

public String getSname() {

return sname;

}

public void setSname(String sname) {

this.sname = sname;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public Address getAddress() {

return address;

}

public void setAddress(Address address) {

this.address = address;

}

public Blob getPicture() {

return picture;

}

public void setPicture(Blob picture) {

this.picture = picture;

}

@Override

public String toString() {

return "Students [sid=" + sid + ", sname=" + sname + ", gender="

+ gender + ", birthday=" + birthday + ", address=" + address

+ "]";

}

}

Students.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2015-10-27 10:10:52 by Hibernate Tools 3.4.0.CR1 -->

<hibernate-mapping>

<class name="Students" table="STUDENTS">

<id name="sid" type="int">

<column name="SID" />

<generator class="assigned" />

</id>

<property name="sname" type="java.lang.String">

<column name="SNAME" />

</property>

<property name="gender" type="java.lang.String">

<column name="GENDER" />

</property>

<property name="birthday" type="java.util.Date">

<column name="BIRTHDAY" />

</property>

<property name="picture" type="java.sql.Blob">

<column name="PICTURE" />

</property>

<component name="address" class="Address">

<property name="postcode" column="POSTCODE"/>

<property name="phone" column="PHONE" />

<property name="address" column="ADDRESS"/>

</component>

</class>

</hibernate-mapping>

StudentsTest.java

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.sql.Blob;

import java.util.Date;

import javax.print.DocFlavor.BYTE_ARRAY;

import org.hibernate.Hibernate;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;

import org.junit.After;

import org.junit.Before;

import org.junit.Ignore;

import org.junit.Test;

//测试类

public class StudentsTest {

private SessionFactory sessionFactory;

private Session session;

private Transaction transaction;

@Before

public void init(){

Configuration config = new Configuration().configure();

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();

sessionFactory = config.buildSessionFactory(serviceRegistry);

session = sessionFactory.openSession();

transaction = session.beginTransaction();

}

@After

public void destroy(){

transaction.commit();

session.close();

sessionFactory.close();

}

@Test

@Ignore

public void testSaveStudents(){

Students s = new Students(3,"张三丰","男",new Date());

session.save(s);

}

@Test

@Ignore

public void testSaveBlob() throws Exception{

Students s = new Students(1,"张三丰","男",new Date());

File file = new File("d:"+File.separator+"student.jpg");

InputStream input = new FileInputStream(file);

Blob image = Hibernate.getLobCreator(session).createBlob(input, input.available());

s.setPicture(image);

session.save(s);

}

@Test

@Ignore

public void testReadBlob() throws Exception{

Students s = (Students) session.get(Students.class,1);//将主键为1的记录从数据库中的记录读出

Blob image = s.getPicture();

InputStream input = image.getBinaryStream();//将图片转变为二进制输入流

File destFile = new File("e:"+File.separator+"dest.jpg");//定位目标文件

OutputStream output = new FileOutputStream(destFile);//将管道插到输出文件上

byte[] buff = new byte[input.available()];//创建缓冲区

input.read(buff);//将图片流先读入缓冲区

output.write(buff);//将缓冲区的内容输出

input.close();

output.close();

}

@Test

public void testComponent(){

Students s = new Students(1,"张三丰","男",new Date());

Address address = new Address("230000","18656981582","中国通用院");

s.setAddress(address);

session.save(s);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: