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);
}
}
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);
}
}
相关文章推荐
- PHP—水印
- 学习笔记:Maven构造版本号的方法解决浏览器缓存问题
- Xcode快速排错
- Android.mk文件分析
- 23种设计模式
- Vim配置文件
- 插入排序C++实现
- Unity中使用Json解析字符串
- L1 L2 L3 cache
- 安卓ADB常用命令
- 《项目架构那点儿事》——Hibernate泛型Dao,让持久层简洁起来
- Unity 下载
- grep环境变量常用配置
- 三种存储类型比较-文件、块、对象存储
- 【HDOJ】【1005】Number Sequence
- UIBezierPath + bezierPathWithArcCenter
- libusb移植
- awk命令
- spring *-servlet.xml
- spring *-servlet.xml