How does hibernate process big data resources?

Source: Internet
Author: User
Bean file packagetk. blank_hibernate.bean; importjava. io. Serializable; importjava. SQL. Blob; publicclassImageimplementsSerializable {***** success; privateIntegerid; privateBlobimage; publicImage

Bean file package tk. blank_hibernate.bean; import java. io. serializable; import java. SQL. blob; public class Image implements Serializable {/*****/private static final long serialVersionUID = 1L; private Integer id; private Blob image; public Image

Bean File

package tk.blank_hibernate.bean;import java.io.Serializable;import java.sql.Blob;public class Image implements Serializable{/** *  */private static final long serialVersionUID = 1L;private Integer id;private Blob image;public Image() {super();// TODO Auto-generated constructor stub}public Image(Integer id, Blob image) {super();this.id = id;this.image = image;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Blob getImage() {return image;}public void setImage(Blob image) {this.image = image;}@Overridepublic String toString() {return "Image [id=" + id + ", image=" + image + "]";}}
Write the ing File
 
 
  
   
   
    
   
   
  
 
Basic dao interface for handling all common operations
Package tk. blank_hibernate.dao; import java. io. serializable; import java. util. list; import java. util. set; public interface BaseDao {/*** add data ** @ param entity * @ return */void saveObject (Object entity ); /*** delete data ** @ param entity */void deleteObject (Object entity ); /*** delete data by ID ** @ param clazz * @ param id */void deleteObject (Class clazz, Serializable id ); /***** change data ** @ param entity */void updateObject (Object entity ); /*** query data by ID ** @ param clazz * @ param id * @ return */Object getObject (Class clazz, Serializable id ); /*** query data by ID ** @ param clazz * @ param id * @ return */Object loadObject (Class clazz, Serializable id ); /*** query all data in the data table * @ param clazz * @ return */List getObjectAll (Class clazz);} the image processing interface
package tk.blank_hibernate.dao;public interface ImageDao extends BaseDao {}
Class that implements the processing method
Package tk. blank_hibernate.dao.impl; import java. io. serializable; import java. util. hashSet; import java. util. list; import java. util. set; import org. hibernate. session; import org. hibernate. transaction; import tk. blank_hibernate.bean.Goods; import tk. blank_hibernate.dao.BaseDao; import tk. blank_hibernate.util.BaseHibernateDaoImpl; public class BaseDaoImpl extends BaseHibernateDaoImpl implements BaseDao {@ Overridepublic void saveObject (Object entity) {System. out. println ("start execution of methods in BaseDaoImpl ========================== saveObject "); session session = getSessionObject (); Transaction transaction = session. beginTransaction (); session. save (entity); transaction. commit () ;}@ Overridepublic void deleteObject (Object entity) {System. out. println ("start execution of methods in BaseDaoImpl ========================== deleteObject "); session session = getSessionObject (); Transaction transaction = session. beginTransaction (); session. delete (entity); transaction. commit () ;}@ Overridepublic void deleteObject (Class clazz, Serializable id) {System. out. println ("start execution of methods in BaseDaoImpl ========================== deleteObject "); session session = getSessionObject (); Transaction transaction = session. beginTransaction (); session. delete (getObject (clazz, id); transaction. commit () ;}@ Overridepublic void updateObject (Object entity) {System. out. println ("start execution of methods in BaseDaoImpl ======================== updateObject "); session session = getSessionObject (); Transaction transaction = session. beginTransaction (); session. update (entity); transaction. commit () ;}@ Overridepublic Object getObject (Class clazz, Serializable id) {System. out. println ("start execution of methods in BaseDaoImpl ======================== getObject "); session session = getSessionObject (); Transaction transaction = session. beginTransaction (); Object object = session. get (clazz, id); return object;} @ Overridepublic Object loadObject (Class clazz, Serializable id) {System. out. println ("start execution of methods in BaseDaoImpl ====================== loadObject"); return null ;} @ Overridepublic List getObjectAll (Class clazz) {System. out. println ("start execution of methods in BaseDaoImpl ======================== getObjectAll "); transaction transaction = getSessionObject (). beginTransaction (); List list = getSessionObject (). createQuery ("from" + clazz. getName ()). list (); transaction. commit (); return list ;}}

Class implementation for image processing
package tk.blank_hibernate.dao.impl;import tk.blank_hibernate.dao.ImageDao;public class ImageDaoImpl extends BaseDaoImpl implements ImageDao {}

Interfaces for processing all services that work together
Package tk. blank_hibernate.service; import java. io. serializable; import java. util. list; public interface BaseService {/*** add data ** @ param entity * @ return */void saveObject (Object entity ); /*** delete data ** @ param entity */void deleteObject (Object entity ); /*** delete data by ID ** @ param clazz * @ param id */void deleteObject (Class clazz, Serializable id ); /***** change data ** @ param entity */void updateObject (Object entity ); /*** query data by ID ** @ param clazz * @ param id * @ return */Object getObject (Class clazz, Serializable id ); /*** query data by ID ** @ param clazz * @ param id * @ return */Object loadObject (Class clazz, Serializable id ); /*** query all data in a data table ** @ param clazz * @ return */List getObjectAll (Class clazz );}
Image Processing service Interface
Package tk. blank_hibernate.service; public interface ImageService extends BaseService {} implement services that process all common methods
Package tk. blank_hibernate.service.impl; import java. io. serializable; import java. util. list; import tk. blank_hibernate.dao.BaseDao; import tk. blank_hibernate.dao.impl.BaseDaoImpl; import tk. blank_hibernate.service.BaseService; public class BaseServiceImpl implements BaseService {BaseDao baseDao = new BaseDaoImpl (); @ Overridepublic void saveObject (Object entity) {System. out. println ("Start the BaseServiceImpl execution method ================ saveObject"); baseDao. saveObject (entity) ;}@ Overridepublic void deleteObject (Object entity) {System. out. println ("Start the BaseServiceImpl execution method =============== deleteObject"); baseDao. deleteObject (entity) ;}@ Overridepublic void deleteObject (Class clazz, Serializable id) {System. out. println ("Start the BaseServiceImpl execution method =============== deleteObject"); baseDao. deleteObject (clazz, id) ;}@ Overridepublic void updateObject (Object entity) {System. out. println ("start to execute methods in BaseServiceImpl ============== updateObject"); baseDao. updateObject (entity) ;}@ Overridepublic Object getObject (Class clazz, Serializable id) {System. out. println ("start to execute methods in BaseServiceImpl ============== getObject"); return baseDao. getObject (clazz, id) ;}@ Overridepublic Object loadObject (Class clazz, Serializable id) {System. out. println ("start to execute methods in BaseServiceImpl ============== loadObject"); return baseDao. loadObject (clazz, id) ;}@ Overridepublic List getObjectAll (Class clazz) {System. out. println ("start to execute methods in BaseServiceImpl ============== getObjectAll"); return baseDao. getObjectAll (clazz );}}
Implementation of Image Processing service
package tk.blank_hibernate.service.impl;import tk.blank_hibernate.service.ImageService;public class ImageServiceImpl extends BaseServiceImpl implements ImageService {}

An API that independently generates a session

Package tk. blank_hibernate.util;

Import org. hibernate. Session;

Public interface IHibernateConnection {
Public Session getSessionObject ();
}


Implementation class for separately generated sessions

Package tk. blank_hibernate.util;

Import org. hibernate. Session;

Public class BaseHibernateDaoImpl implements IHibernateConnection {

@ Override
Public Session getSessionObject (){
Return HiberUtil. openSession ();
}

}

Substantive methods for generating sessions

Package tk. blank_hibernate.util; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. boot. registry. standardServiceRegistryBuilder; import org. hibernate. cfg. configuration; import org. hibernate. service. serviceRegistry; public class HiberUtil {static Configuration cfg; static ServiceRegistry serviceRegistry; static SessionFactory sessionFactory; static {cfg = new Configuration (). configure (); serviceRegistry = new StandardServiceRegistryBuilder (). applySettings (cfg. getProperties ()). build (); sessionFactory = cfg. buildSessionFactory (serviceRegistry);} public static Session openSession () {// return the connection object of the current session return sessionFactory. getCurrentSession ();}}
Hibernate. cfg. xml
 
 
  
   
    
Com. mysql. jdbc. Driver
   
   
    
Jdbc: mysql: // localhost: 3306/hiber_jd
   
   
    
Root
   
   
    
Admin
   
   
   
    
Org. hibernate. dialect. MySQLDialect
   
   
   
    
Thread
   
   
   
    
True
   
   
   
    
False
   
   
   
    
Update
   
   
  
 

Test code

Package tk. blank_hibernate.junit; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. filterInputStream; import java. io. IOException; import java. io. inputStream; import java. SQL. blob; import java. SQL. SQLException; import org. hibernate. hibernate; import org. junit. test; import tk. blank_hibernate.bean.Image; import tk. blank_hiber Nate. service. imageService; import tk. blank_hibernate.service.impl.ImageServiceImpl; import tk. required; public class ImageTest {// create ImageService processing object ImageService imageService = new ImageServiceImpl (); @ Testpublic void save () {// create an img object Image = new image (); // read the File file = new File ("F: \ webprogect \ hibernate_jd \ src \ ni.jpg"); try {// create the input stream of the File, load the file to the stream. New FileInputStream (file); // create a blob Big Data Object |||| get Blob = Hibernate in this way after 4. getLobCreator (HiberUtil. openSession ()). createBlob (FCM, file. length (); // store big data to an image. setImage (blob); imageService. saveObject (image);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}@ Testpublic void getImage () throws SQLException {Image image = (Image) imageService. getObject (Ima Ge. class, 1); // determines whether the obtained data is null if (image! = Null) {InputStream is = image. getImage (). getBinaryStream (); File file = new File ("D: \ a.jpg"); try {FileOutputStream fos = new FileOutputStream (file ); byte buffer [] = new byte [1024]; int len = 0; while (len = is. read (buffer ))! =-1) {fos. write (buffer, 0, len);} fos. close (); is. close ();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.