JPA Instances-Reprint

Source: Internet
Author: User

Specific examples

After completion of the project structure such as:

(Note: The location of the Persistence.xml file determines the root of persistence (persistence root). The root of the persistence is the jar file or the directory containing the Meta-inf directory (provided that Persistence.xml is located here). This persistence.xml file is generally placed in the Meta-inf under SRC. The name and position are not changed)

A), import the relevant jar package (see) and create the Meta-inf and Persistence.xml files.

The Persistence.xml is configured as follows: (my data is MySQL, different implementations of different databases and JPA will result in different configuration content)

[XHTML]View PlainCopy
  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemalocation="Http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ Persistence_version=1 ">
  4. <persistence-unit name="MYSQLJPA" transaction-type="resource_local">
  5. <properties>
  6. <property name= "hibernate.dialect" value="Org.hibernate.dialect.MySQL5Dialect" / >
  7. <property name= "hibernate.connection.driver_class" value="Com.mysql.jdbc.Driver" / >
  8. <property name= "hibernate.connection.username" value="root" />
  9. <property name= "hibernate.connection.password" value="123456" />
  10. <property name= "hibernate.connection.url" value="JDBC:MYSQL://LOCALHOST:3306/DB1" />
  11. <property name= "hibernate.max_fetch_depth" value="3" />
  12. <property name= "hibernate.hbm2ddl.auto" value="Update" />
  13. </Properties>
  14. </persistence-unit>
  15. </Persistence>

b), write the entity bean, as follows:

[C-sharp]View PlainCopy
  1. Package Com.hmk.bean;
  2. Import Javax.persistence.Column;
  3. Import javax.persistence.Entity;
  4. Import Javax.persistence.GeneratedValue;
  5. Import Javax.persistence.Id;
  6. @Entity
  7. Public class Person {
  8. private int id;
  9. private String name;
  10. @Id @GeneratedValue
  11. public int getId () {
  12. return ID;
  13. }
  14. public void setId (int id) {
  15. this.id = ID;
  16. }
  17. @Column (length=12)
  18. Public String GetName () {
  19. return name;
  20. }
  21. public void SetName (String name) {
  22. this.name = name;
  23. }
  24. }

c), write the JUnit test code as follows:

[Java]View PlainCopy
  1. Package junit.test;
  2. Import Javax.persistence.EntityManager;
  3. Import Javax.persistence.EntityManagerFactory;
  4. Import javax.persistence.Persistence;
  5. Import Org.junit.BeforeClass;
  6. Import Org.junit.Test;
  7. Import Com.hmk.bean.Person;
  8. Public class Jpatest {
  9. @BeforeClass
  10. public static void Setupbeforeclass () throws Exception {
  11. }
  12. @Test public void CreateTable () {
  13. //Can verify that the build table is correct
  14. Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
  15. Factory.close ();
  16. }
  17. @Test public Void Save () {
  18. Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
  19. Entitymanager em = Factory.createentitymanager ();
  20. Em.gettransaction (). Begin ();
  21. Person person = new Person (); //person to new state
  22. Person.setname ("Zhang San");
  23. Em.persist (person); //Persistent entities
  24. Em.gettransaction (). commit ();
  25. Em.close ();
  26. Factory.close ();
  27. }
  28. //new, escrow, removal, removal
  29. @Test public Void Update () {
  30. Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
  31. Entitymanager em = Factory.createentitymanager ();
  32. Em.gettransaction (). Begin ();
  33. Person person = em.find (person.   class, 1);
  34. Person.setname ("HMK"); //person is a managed state
  35. Em.gettransaction (). commit ();
  36. Em.close ();
  37. Factory.close ();
  38. }
  39. @Test public void Update2 () {
  40. Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
  41. Entitymanager em = Factory.createentitymanager ();
  42. Em.gettransaction (). Begin ();
  43. Person person = em.find (person.   class, 1);
  44. Em.clear (); //Change all entities in the entity manager to the off-pipe State
  45. Person.setname ("Hmk2");
  46. Em.merge (person); //Turn off state into managed state, merge can automatically select INSERT or update data
  47. Em.gettransaction (). commit ();
  48. Em.close ();
  49. Factory.close ();
  50. }
  51. @Test public void Remove () {
  52. Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
  53. Entitymanager em = Factory.createentitymanager ();
  54. Em.gettransaction (). Begin ();
  55. Person person = em.find (person.   class, 1);
  56. Em.remove (person); //Delete entity
  57. Em.gettransaction (). commit ();
  58. Em.close ();
  59. Factory.close ();
  60. }
  61. @Test public Void Find () {
  62. Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
  63. Entitymanager em = Factory.createentitymanager ();
  64. Person person = em.find (person.  Class, 2); //A Get method similar to hibernate, returns NULL when no data is found
  65. System.out.println (Person.getname ());
  66. Em.close ();
  67. Factory.close ();
  68. }
  69. @Test public void Find2 () {
  70. Entitymanagerfactory factory = persistence.createentitymanagerfactory ("MYSQLJPA");
  71. Entitymanager em = Factory.createentitymanager ();
  72. Person person = em.getreference (person.  Class, 2); //Similar to Hibernate's Load method, lazy loading. An exception occurs when there is no corresponding data
  73. System.out.println (Person.getname ()); //Real call to find data
  74. Em.close ();
  75. Factory.close ();
  76. }
  77. }

D), run the appropriate method in the JUnit test code.

JPA Instances-Reprint

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.