Use spring Data JPA in Hibernate

Source: Internet
Author: User

1, Pom.xml in the introduction of relevant dependencies

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < Maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</ Maven.compiler.target> </properties> <dependencies><!--Spring data JPA dependent -<dependency> <groupId>org.springframework.data</groupId> &LT;ARTIFACTID&GT;SPRING-DATA-JPA </artifactId> <version>1.11.4.RELEASE</version> </dependency><!--hibernate dependency --<dependency> <groupId>org.hibernate</groupId> &LT;ARTIFACTID&GT;HIBERNATE-CORE&LT;/ARTIFAC tid> <version>5.1.7.Final</version> </dependency> <dependency> <groupid& Gt;org.hibernate</groupid> <artifactId>hibernate-entitymanager</artifactId> <version&gt ;5.1.7.final</version> </dependency><!--mysql dependency --<dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</artifacti d> <version>5.1.42</version> </dependency> <dependency> <groupid>ju nit</groupid> <artifactId>junit</artifactId> <version>3.8.1</version> <sco Pe>test</scope> </dependency> </dependencies>

2. JPA Configuration Mate-inf/persistence.xml

<?xml version= "1.0" encoding= "UTF-8"? ><persistence version= "2.1" xmlns= "http://xmlns.jcp.org/xml/ns/ Persistence "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://xmlns.jcp.org/xml/ Ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd "> <persistence-unit name="MYJPA"Transaction-type=" resource_local "> <provider>Org.hibernate.jpa.HibernatePersistenceProvider</provider> <class>com.wyl.bean.user</class> <properties> <property name= "Javax.persistence.jdbc.driver" value= "Com.mysql.jdbc.Driver"/ > <property name= "javax.persistence.jdbc.url" value= "Jdbc:mysql://127.0.0.1:3306/test"/> &L T;property name= "Javax.persistence.jdbc.user" value= "root"/> <property name= "JAVAX.PERSISTENCE.JDBC.PASSW Ord "value=" root "/> <property name=" Hibernate.show_sql "value=" true "/> <pro Perty name= "Hibernate.hbm2ddl.auto" value= "Update"/> </properties> </persistence-unit></persis Tence>

3. Create an Entity object User.class

 PackageCom.wyl.bean;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "Users") Public classUser {@Id @GeneratedValue (strategy=generationtype.identity)PrivateInteger ID; @ColumnPrivateString UserName; @ColumnPrivateString Phone;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicString Getphone () {returnphone; }     Public voidSetphone (String phone) { This. Phone =phone; } @Override PublicString toString () {//TODO auto-generated Method Stub        return Super. toString (); }}

4. Create a test class

 PackageCom.wyl;ImportJavax.persistence.EntityManager;Importjavax.persistence.EntityManagerFactory;Importjavax.persistence.Persistence;Importorg.junit.Test;ImportCom.wyl.bean.User; Public classJpatest {PrivateEntitymanagerfactory factory = Persistence.createentitymanagerfactory ("MYJPA"); @Test Public voidTestadd () {Entitymanager em=Factory.createentitymanager (); User User=NewUser (); User.setusername ("Wang"); User.setphone ("123422222222"); Em.gettransaction (). Begin (); //Transaction Open                Try{em.persist (user); Persisted data em.gettransaction (). commit (); //Transaction Commit}Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Em.gettransaction (). rollback (); //Transaction Rollback}finally{em.close ();//Close em}} @Test Public voidTestfind () {Entitymanager em=Factory.createentitymanager (); User User= Em.find (User.class, 1);        System.out.println (User.getusername ()); Em.gettransaction (). Begin (); //Transaction Open        Try{em.merge (user); Em.gettransaction (). commit (); //Transaction Commit}Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Em.gettransaction (). rollback (); //Transaction Rollback}finally{em.close ();//Close em}} @Test Public voidTestdelete () {Entitymanager em=Factory.createentitymanager (); User User= Em.find (User.class, 1);//Query to entityem.gettransaction (). Begin ();//Transaction Open        Try{em.remove (user); //Delete EntityEm.gettransaction (). commit ();//Transaction Commit}Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Em.gettransaction (). rollback (); //Transaction Rollback}finally{em.close ();//Close em        }    }}

Using spring Data JPA

in hibernate

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.