Hibernate ing implementation

Source: Internet
Author: User

Hibernate ing implementation

In fact, the previous example also involves ing. Hibernate uses Hibernate. cfg. xml to configure database information, and xxx. hbm. xml to configure the ing between objects and database tables.

This example involves the storage of large text information and the storage of image information in the database, which is of practical value.

 

Entity Information

 

Package test. hibernate. domain; import java. util. date; public class User {private int id; private String name; private int age; private Date birthday; private String desc; // private byte [] photo; // profile picture public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public Date getBirthday () {return birthday;} public void setBirthday (Date birthday) {this. birthday = birthday;} public byte [] getPhoto () {return photo;} public void setPhoto (byte [] photo) {this. photo = photo;} public String getDesc () {return desc;} public void setDesc (String desc) {this. desc = desc ;}@ Overridepublic String toString () {// TODO Auto-generated method stubreturn [User: id = + id +, name: + name +];}

Test class

 

Package test. hibernate. domain; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStream; import java. io. outputStream; import java. util. date; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. hibernate. classic. session; import org. junit. test; public class App {private static SessionFactory sessionFactory; s Tatic {Configuration cfg = new Configuration (); cfg. configure (hibernate. cfg. xml); sessionFactory = cfg. buildSessionFactory () ;}@ Testpublic void testSave () throws Exception {// read the image InputStream in = new FileInputStream (E:/icon/my02.png ); byte [] photo = new byte [in. available ()]; in. read (photo); in. close (); // create Object User user = new User (); user. setName (zhangsan); user. setAge (21); user. setBirthday (new Date (); user. setDesc (A thesis ........................); User. setPhoto (photo); Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction (); session. save (user); transaction. commit (); session. close (); // close the session and release the resource} @ Testpublic void testGet () throws Exception {Session session = sessionFactory. openSession (); Transaction transaction = session. beginTransaction (); User user User = (User) session. get (User. class, 1); System. out. println (user. getId (); System. out. println (user. getName (); System. out. println (user. getBirthday (); System. out. println (user. getDesc (); // write the image to copy.png OutputStream outputStream = new FileOutputStream (F:/copy.png) of drive F; outputStream. write (user. getPhoto (); outputStream. close (); transaction. commit (); session. close ();}}

 


Main configuration file

 
  
       
   
    
Org. hibernate. dialect. MySQLDialect
   
   
    
Com. mysql. jdbc. Driver
   
   
    
Jdbc: mysql: // localhost: 3306/hibernate_2015
   
   
   
    
Root
   
   
    
686175
   
   
   
    
True
   
   
    
Update
   
   
  
 

Added the hbm2ddl. auto attribute to update the table structure.

View in MySQL-Front

 

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.