The JPA (Java Persistence API), the Java Storage Data API, provides a more convenient interface for storing data, and, of course, requires the use of Java Persistence Query language for complex and query operations. This is basically the same as SQL, just need to follow some other grammatical rules, you can refer to http://download.oracle.com/javaee/5/tutorial/doc/bnbuf.html.
When using JPA, for example, you have a lesson table defined in MySQL, and now you want to add a record to the lesson table, you need to write two code:
1, write a Lesson.java, is a pojo, a simple Java object, which includes all the fields of the lesson table in the database, here see the property name in Lesson.java with the name of the database to save the same OK. Then user.java the class definition before adding @entity this annotation, which is quite annotated that the Lesson.java class will be treated as an entity at the time of storage.
2, write a storage interface, For example, Lessonserviceimpl.java, this class needs to have a Javax.persistence.EntityManager class attribute, such as EM, so that the corresponding data operations can be implemented through this EM, such as to add a day record, only need to call the em.pe Rsist. Em.merge when updated, em.remove when deleted. JPA takes into account these common applications and therefore designs these simple interfaces for ease of operation. The following is part of the code for the class
@Transactional
public class Lessonserviceimpl implements Lessonservice {
Private Entitymanager em;
@PersistenceContext
public void Setentitymanager (Entitymanager em)
{
This.em = em;
}
public void remove (int id) {
TODO auto-generated Method Stub
Lesson Lesson = Em.find (lesson.class, id);
if (lesson!= null)
{
Em.remove (lesson);
}
}
.............
It is of course necessary to note that the storage interface Lessonserviceimpl.java is usually used as a singleton type in the design, so it is usually in the future of the class factory. And usually the Entitymanager object in the Lessonserviceimpl.java is set directly in the class factory by relying on injection. For example, the following code in spring Factory
................
<bean id= "Lessonservice" class= "Quickstart.service.LessonServiceImpl"/>
<bean id= "Entitymanagerfactory" class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
.......................
</bean>
<bean id= "TransactionManager"
class= "Org.springframework.orm.jpa.JpaTransactionManager" >
<property name= "Entitymanagerfactory" ref= "Entitymanagerfactory"/>
</bean>
<tx:annotation-driven transaction-manager= "TransactionManager"/>
...................
Instead of using pure JPA, we use JPA in Hibernate-jpa-2.0-api-1.0.0.final.jar. Add @transactional before the definition of Lessonserviceimpl.java this class, which means that the class needs to be transacted, and the transaction used is specified through <tx:annotation-driven/>. That is, Lessonserviceimpl to use the TransactionManager defined in this class factory for data operations, you can refer to the http://static.springsource.org/spring/ Docs/2.0.x/reference/transaction.html#transaction-declarative.
Define a set function This function passes into a Entitymanager class parameter, through this interface class factory can lessonserviceimpl need Entitymanager by the class factory to inject, The set function then needs to add the @persistentcontext this annotation, which means that the injection storage context object's interface, can refer to http://download.oracle.com/javaee/5/ Tutorial/doc/bnbqw.html.
And, of course, I think the easiest way to express JPA is to use the method of direct injection:
<bean id= "Lessonservice" class= "Quickstart.service.LessonServiceImpl" >
<property name= "Entitymanager" ref= "Entitymanager"/>
</bean>
But such a problem is that all interfaces that access data are injected once. So here we do dependency injection through the AOP approach and the combined annotation approach (commonly used in XML). For transaction processing through @transactional, so if the removal of @transactional is equivalent to remove transaction management, will result in data can only query operations cannot be deleted or updated And for management entity through @persistentcontext, where the class factory will call the name Entitymanagerfactory (default name) Bean is passed to the bean in all factories with an interface for adding @persistentcontext (here is Lessonservice)