Public class Main {
Public static void main (String [] args) {
//1. Create the EntitymanagerFactory
String persistenceUnitName="jpa 1";
EntityManagerFactory EntityManagerFactory=
Persistence. CreateEntityManagerFactory (persistenceUnitName);
//2. Create the EntityManager
EntityManager EntityManager=entityManagerFactory. CreateEntityManager ();
//3.
account affairsEntityTransaction transaction=entityManager. GetTransaction ();
Transaction. The begin ();
//4. Persistence operation
The Customer the Customer=new Customer ();
Customer. SetAge (13);
Customer. SetEmail (" [email protected] ");
Customer. SetLastName (" Tom ");
Customer. SetCreatedTime (new Date ());
EntityManager. Persist (the customer);
//5. Commit the transaction
Transaction.com MIT ();
//6. Close the EntityManager
EntityManager. Close ();
//7. Close the EntityManagerFactory
EntityManagerFactory. Close ();
}
}
Entity classes:
@ Table (name="JPA_CUSTOMERS")
@ the Entity
Public class Customer {
private Integer id;
Private String lastName.
private String email;
Private int the age;
private Date createdTime;
@ GeneratedValue (strategy=GenerationType. AUTO)
@ Id
Public Integer getId () {
Return the id;
}
Public void setId (Integer id) {
This. Id=id;
}
@ the Column (name="LAST_NAME")
Public String getLastName () {
Return the lastName.
}
Public void setLastName (String lastName) {
Enclosing the lastName=lastName;
}
Public String getEmail () {
Return email;
}
Public void setEmail (String email) {
This. Email=email;
}
Public int getAge () {
Return the age;
}
Public void setAge (int age) {
Enclosing the age=age;
}
@ Temporal (TemporalType. DATE)
Public Date getCreatedTime () {
Return createdTime;
}
Public void setCreatedTime (Date createdTime) {
Enclosing createdTime=createdTime;
}
@ Override
Public String toString () {
Return the Customer {" + "
"Id=" + id +
"LastName='" + lastName +' \ ' '+
", email='" + email +' \ ' '+
"Age=" + age +
, "createdTime=+ createdTime +"
'} ';
}
}
Persistence. The XML configuration files:
<? The XML version="1.0" encoding="utf-8"?>
1. In fact configuration is javax.mail. Persistence. Spi. PersistenceProvider interface implementation class
2. If only a JPA implementation of JPA project products, also can not configure the node.
- & gt;
The runtime specific error:
The Exception in the thread "main" javax.mail. Persistence. PersistenceException: Unable to configure EntityManagerFactory
The at org. Hibernate. Ejb. Ejb3Configuration. Configure (Ejb3Configuration. Java: 387)
The at org. Hibernate. Ejb. HibernatePersistence. CreateEntityManagerFactory (58) HibernatePersistence. Java:
The at javax.mail. Persistence. Persistence. CreateEntityManagerFactory (persistence. Java: 63)
The at javax.mail. Persistence. Persistence. CreateEntityManagerFactory (persistence. Java: 47)
At com. Atguigu. Jpa. The helloworld. Main. The Main (16). The Main Java:
Under Caused by: Java. Lang. RuntimeException: Error while reading file:/F: AD e5/% % % a6 b5 e8 a0 b9 e4 % % % % % % e6 84% % 96% % 99 b0 e5/% % % 9 a e7 % a1 e5 b7Java b0 e8 % % % % 85% % 85% % a8 e5 e7 a5 e6%95% % 97% % 99% % % a8 8 b/4. 9 b0 e5 % % % a % % e7 a1 e5 b7 b0 e8 % % % % % 85% 85% a5 e5 a8 % % % % 97 Java e6, e7 % a8 % % 95% 99% 8 b - e9 JavaEE % % % % 98% b6 % e6 ae b5/6. % % e5 b0 e7 9 a % % % % 85% a1 a7 e8 b7JPA b0 e8 % % % % % 86% a7 e8 91/a2 e9 % % % % % % 86% e9 a2%91% % e6 ba a0 e7 % % % 90% 81/25/exer/out/production/jpa - 1 (this is compiled directory)
The at org. Hibernate. Ejb. Packaging. NativeScanner. GetClassesInJar (NativeScanner. Java: 131)
The at org. Hibernate. Ejb. Ejb3Configuration. AddScannedEntries (Ejb3Configuration. Java: 487)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull