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