Use hibernate to apply a lasting layer

xiaoxiao2021-03-05  24

After attempting to have a number of Entity Beans fails, I decided to give up the cumbersome configuration of EJB and replace it with Hibernate.

Learning Hibernate is not very difficult, it should be easier to get started, watching Hibernate Reference and Hibernate In Action, write a sustainable class.

It is necessary to do a few things to do with Hibernate to implement objects: 1. The tempting of Person.java Hibernate is that there is no special requirement or writing format and class inheritance for the class of persistence. Structural requirements. The average JavaBean style can use HeberNate to last, and even do not implement the seriazable interface. Package Hi; Public Class Person {Private String ID; Private String Name; Private String Addr;

Public string getaddr () {return addr;} public void setaddr (safww) {Return Age;}} public void setage (int Age) {THIS.AGE; Public string getName () {return name;} public void setName (String name) {this.name = name;} public string getId () {return id;} public void setId (String ID) {this.id = ID; }

2. Write the corresponding hibernate profile of the class Person.hbm.xml to tell Hibernate what you need to persistence and what is the field in this class to the field in the database, what is the type. Another power of Hibernate is that there is no need to create Table in the database, and Hibernate can create through Person.hbm.xml (the red code of the red code in the following) is automatically crete Table. 3. Writing Hibernate and Using the database for profile hibernate.cfg.xml This configuration file is to tell Hibernate how to connect with the database, and which In this database.

true net.sf.hibernate.dialect.MySQLDialect < Property name = "connection.driver_class"> com.mysql.jdbc.driver JDBC: mysql: // localhost: 3306 / hi root

At this time, for the basic work of Person's persistent, the following is to write Test using the Person class to see the Hibernate's efficacy. For the convenience of convenience, we abstract a Hibernate tool class hibernateutil.java It is to maintain a session, and there is a static method to get, close this session, save each test, to write. package hi; import net.sf.hibernate *;. import net.sf.hibernate.cfg *;. import net.sf.hibernate.tool.hbm2ddl.SchemaUpdate; public class HibernateUtil {private static final SessionFactory sessionFactory; static {try { Configuration config = new Configuration ();. sessionFactory = config.configure () buildSessionFactory (); new SchemaUpdate (config) .execute (true, true);} catch (Throwable ex) {ex.printStackTrace (); throw new ExceptionInInitializerError ( ex);}} public static final ThreadLocal session = new ThreadLocal (); public static Session currentSesion () throws HibernateException {Session s = (Session) session.get (); if (s == null) {s = sessionFactory.openSession (); Session.set (s);} return s;} shRows HibernateException {session s = (session) session.get (); session.set (null); if (s! = Null) S. close ();}} The following is the TEST class: package hi;

Import net.sf.hibernate. *; import net.sf.hibernate.cfg. *;

Import hi.hibernateutil; import hi.Person; import net.sf.hibernate.hibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.TransAt;

Public class test {

Public static void main (string [] args) {Try {session session = hibernateutil.currentSEsion (); transaction tx = session.begintransaction (); person person1 = new person (); person1.setage (20); person1.setname "apple1"); session.save (Person1); tx.commit (); hibernateutil.closesis ();} catch (hibernateException E) {E.PrintStackTrace ();}}} Theoretically, you can run Test, But there may be doubts in your heart, what JAR needs to be needed to use Hibernate to configure environmental energy. The answer is very simple, and it is very clear in Hibernate Reference, but it is necessary to put J2EE.jar in ClassPath in addition to the needed JAR listed in Reference. Hibernate Reference Downloads: www.hibernate.org/5.html

转载请注明原文地址:https://www.9cbs.com/read-35152.html

New Post(0)