The understanding of Hibernate Entitymanager

Source: Internet
Author: User
Tags gettext

Hibernate Entitymanager is a wrapper around the hibernate core that provides the JPA programming interface, supports the lifecycle of JPA entity instances, and allows you to write queries in a standard Java Persistence Query language.

1. Basic JPA configuration (ENTITYMANAGERFACTORY--EMF configuration)

Persistence.xml, the file must be placed in the Meta-inf directory of the persistent unit being deployed, because I am building Java project here, so I put the Meta-inf directory in the bin directory

<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_1_0.xsd"version= "1.0" > <persistence-unit name= "HelloWorld" > <provider>org.hibernate.ejb.hibernatepersi Stence</provider> <!--<class>hello. message</class> <properties> <property name= "hibernate.archive.autodetection" value= "C              LASS,HBM "/> <property name=" Hibernate.connection.username "value=" root "/> <property name= "Hibernate.connection.url" value= "Jdbc:mysql://localhost:3306/myhibernate"/> <pro Perty name= "Hibernate.dialect" value= "Org.hibernate.dialect.MySQLDialect"/> <property name= "Hibernat E.connection.driver_class "value=" Com.mysql.jdbc.Driver "/> <property name=" Hibernate.hbm2ddl.auto "Valu E= "Create"/> <property name= "hibernate.c3p0.min_size" value= "5"/> <property name= "Hi              Bernate.c3p0.max_size "value="/> <property name= "hibernate.c3p0.timeout" value= "/>" <property name= "hibernate.c3p0.max_statements" value= "/> <property name=" hibernate.c3p0.idle_t Est_period "value=" 3000 "/> <property name= "Hibernate.show_sql" value= "true"/> <property name= "Hibernate.conn Ection.password "value=" "/> <property name=" myeclipse.connection.profile "value=" MySQL "/> & Lt;/properties> </persistence-unit> </persistence>

Entity class Message.java

     PackageHello; ImportJavax.persistence.CascadeType; ImportJavax.persistence.Column; Importjavax.persistence.Entity; ImportJavax.persistence.GeneratedValue; Importjavax.persistence.Id; ImportJavax.persistence.JoinColumn; ImportJavax.persistence.ManyToOne; Importjavax.persistence.Table; @Entity @Table (Name= "Message")       Public classMessage {@Id @GeneratedValue @Column (name= "ID")          PrivateLong ID; @Column (Name= "Message_text")          PrivateString text; @ManyToOne (Cascade=cascadetype.all) @JoinColumn (name= "next_message_id")          PrivateMessage Nextmessage;  PublicLong getId () {returnID; }                 Public voidsetId (Long id) { This. ID =ID; }                 PublicString GetText () {returntext; }                 Public voidSetText (String text) { This. Text =text; }                 PublicMessage getnextmessage () {returnNextmessage; }                 Public voidsetnextmessage (Message nextmessage) { This. Nextmessage =Nextmessage; }      }  

Test class Helloworld.java

     PackageHello; ImportJava.util.Iterator; Importjava.util.List; ImportJavax.persistence.EntityManager; Importjavax.persistence.EntityManagerFactory; Importjavax.persistence.EntityTransaction; Importjavax.persistence.Persistence;  Public classHelloWorld {@SuppressWarnings ("Unchecked")           Public Static voidMain (string[] args) {entitymanagerfactory emf=persistence. Createentitymanagerfactory ("HelloWorld"); /*----------1------*/Entitymanager em=Emf.createentitymanager (); Entitytransaction TX=em.gettransaction ();                    Tx.begin (); Message Message=NewMessage (); Message.settext ("Hello World");                    Em.persist (message);              Tx.commit ();              Em.close (); /*----------2------*/Entitymanager Newem=Emf.createentitymanager (); Entitytransaction NEWTX=newem.gettransaction ();                    Newtx.begin (); List Messages= Newem.createquery ("Select m from Message m order by m.text ASC"). Getresultlist (); System.out.println ("Messages.size () =" +messages.size ());  for(Iterator iter =messages.iterator (); Iter.hasnext ();) {Message loadedmsg=(Message) iter.next ();              System.out.println (Loadedmsg.gettext ());              } newtx.commit ();                    Newem.close ();          Emf.close (); }            }  

Run results

Messages.size () = 1
Hello World

Description

Javax.persistence.Persistence provides a startup class for the creation of a static method for Entitymanagerfactory

Javax.persistence.EntityManagerFactory equivalent to Hibernate's sessionfactory

Javax.persistence.EntityManager quite with Hibernate's session

Javax.persistence.Query is quite the same as Hibernate's query, as hibernate uses HQL, as well as using an object-only querying language

Javax.persistence.EntityTransaction equivalent to Hibernate's transaction

The understanding of Hibernate Entitymanager

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.