Hibernate实现Oracle BLOB的数据读写(2)

这次采用的是EJB3的配置

测试环境与前次一样:Hibernate3.6.7,Oracle 10g Express,JDK7,Win7

1,数据库脚本

 

create table TUser  (
   ID                   char(32)                        not null,
   name               varchar(10char)                 not null,
   photo               blob,                              --头像
   constraint PK_TUser primary key (ID)
);

 

 2,配置文件,即META-INF/persistence.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	version="2.0">
	<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE" />
			<property name="javax.persistence.jdbc.user" value="tanlan" />
			<property name="javax.persistence.jdbc.password" value="tanlan" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
		</properties>
	</persistence-unit>
</persistence>

 

 3,编写实体类User.java

 

package com.tanlan.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "TUser")
public class User {
	@Id
	@GenericGenerator(name = "uid", strategy = "uuid")
	@GeneratedValue(generator = "uid")
	private String id;
	@Column
	private String name;
	@Lob
	@Column
	private byte[] photo;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public byte[] getPhoto() {
		return photo;
	}
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
}

 

 4.测试代码

package com.tanlan.hibernate.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import com.tanlan.hibernate.entity.User;

public class TestUser {

	public static void main(String[] args) {
		addUser();
		//getUserById();
	}

	private static void addUser() {
		User user = new User();
		user.setName("谭岚");
		File photo = new File("D:\\test.jpg");
		try {
			FileInputStream is = new FileInputStream(photo);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			byte[] temp = new byte[512];
			int i = 0;
			while ((i = is.read(temp, 0, temp.length)) != -1) {
				os.write(temp, 0, temp.length);
			}
			os.close();
			is.close();
			user.setPhoto(os.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		EntityManager em = createEntityManager();
		EntityTransaction transaction = em.getTransaction();
		transaction.begin();
		em.persist(user);
		transaction.commit();
	}

	private static void getUserById() {
		EntityManager em = createEntityManager();
		User user = (User) em.find(User.class,"402881e432aad3340132aad33c210000");
		em.close();
		byte[] photo = user.getPhoto();
		try {
			FileOutputStream os = new FileOutputStream("E:\\333.jpg");
			os.write(photo);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static EntityManager createEntityManager() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
		return emf.createEntityManager();
	}

}
 

你可能感兴趣的:(Hibernate)