In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of ORM for Spring database access, which is very detailed and has certain reference value. Friends who are interested must finish it!
Another major module in Spring database access is ORM,ORM, that is, object / relational mapping. Spring supports most ORM frameworks, such as Hibernate,JPA,JDO,TopLink and iBatis (Spring2 supports iBatis2, and now MyBatis3's Spring support is developed by the MyBatis community, not Spring).
First of all, let's start by using the ORM framework alone to introduce Spring's support for ORM, taking Hibernate as an example. Using the ORM framework, you need to provide persistence classes. With course management as the background, the course classes are designed as follows:
Java code
Public class Course {private Long id; private String title; private java.util.Date startDate; private java.util.Date endDate; private int fee; / / must provide no-parameter default construction methods public Course () {super ();} / / omitting other construction methods, getter and setter, etc.
As a mode of data access, we still apply the DAO pattern, and we are adept at writing DAO classes, which can be designed as follows:
Package org.ourpioneer.course.dao; import java.util.List; import org.ourpioneer.course.bean.Course; public interface CourseDAO {public void save (Course course); public void delete (Course course); public void update (Course course); public Course findById (Long courseId); public List findAll ();}
Very simple design, including CRUD operations, then the implementation of the class we use Hibernate to help us to access the database is also very simple:
Package org.ourpioneer.course.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.ourpioneer.course.bean.Course; public class CourseDAOImpl implements CourseDAO {private SessionFactory sessionFactory; public CourseDAOImpl () {Configuration cfg = new Configuration () .configure () SessionFactory = cfg.buildSessionFactory ();} public List findAll () {Session session = sessionFactory.openSession (); try {Query query = session.createQuery ("from Course"); return query.list ();} finally {session.close () }} public Course findById (Long courseId) {Session session = sessionFactory.openSession (); try {return (Course) session.get (Course.class, courseId);} finally {session.close () }} public void save (Course course) {Session session = sessionFactory.openSession (); Transaction tx = session.beginTransaction (); try {tx.begin (); session.saveOrUpdate (course); tx.commit () } catch (RuntimeException e) {tx.rollback (); throw e;} finally {session.close ();}
Only a few methods are shown here as representatives, and other methods can be written similarly, which is very simple. The first is the construction method, which creates the configuration object of Hibernate when initializing the implementation class. when new Configuration (). Configure (), Hibernate will automatically find and load the configuration file named hibernate.cfg.xml under the root path of the classpath, and then create the Session object of Hibernate, using the methods provided and derived by the Session object to operate the database. Next, let's take a look at the configuration file, which is more important, because through the configuration file, telling Hibernate both the database information and the entity Bean information can save us a lot of things about database design.
"- / / Hibernate/Hibernate Configuration DTD 3.0//EN"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> com.mysql.jdbc.Driverproperty > jdbc:mysql:///testproperty > rootproperty > 123property > org.hibernate.dialect.MySQLDialectproperty > trueproperty > updateproperty > session-factory > hibernate-configuration >
Here we tell Hibernate to use the mysql database, configure the database information, the dialect used, and print out the restored SQL statement on the console when the application is executed. Using hbm2ddl.auto, you can let Hibernate automatically build tables based on the configuration information of entity Bean, which is very convenient. The final mapping is the file that configures the mapping information of entity bean. Let's take a look at:
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> id > class > hibernate-mapping >
First of all, you configure the location of the entity bean, the class information, and the corresponding relationship of the table in the database. After that, the primary key information id is created, and the self-increasing primary key feature of MySQL is used, and the rest is the description of each field, which is easy to understand.
With this in place, you can write a test program to introduce Hibernate-related dependencies into the project. To explain here, the default repository of Maven is Apache, where the version of Hibernate is still in 3.3.2.GA (at the time of writing this article), and the current official version of Hibernate is 3.6.0.Final, we want to use the new version, what should we do? Quite simply, configure the location of the Maven repository so that it can discover the 3.6.0.Final version of Hibernate and download the dependencies. JBoss officially provides a Maven repository with the latest version of Hibernate, so let's configure this address in the POM of the project:
AlwaysupdatePolicy > releases > alwaysupdatePolicy > snapshots > Jbossid > Jboss Repositoryname > https://repository.jboss.org/nexus/content/groups/publicurl> repository > repositories >
After that, by introducing other necessary dependencies for the project and using Maven management, we no longer have to look for various dependencies ourselves, which is very simple to manage, as shown in the figure:
Let's take a look at the sample program:
Package org.ourpioneer.course; import java.util.GregorianCalendar; import java.util.List; import org.ourpioneer.course.bean.Course; import org.ourpioneer.course.dao.CourseDAO; import org.ourpioneer.course.dao.CourseDAOImpl; public class Demo {public static void main (String [] args) {CourseDAO courseDAO = new CourseDAOImpl (); Course course = new Course (); course.setTitle ("Spring ORM") Course.setStartDate (new GregorianCalendar (2011, 1, 1). GetTime (); course.setEndDate (new GregorianCalendar (2011, 2, 1). GetTime (); course.setFee (100); courseDAO.save (course); List courses = courseDAO.findAll (); Long courseId = courses.get (0). GetId (); course = courseDAO.findById (courseId) System.out.println (course); courseDAO.delete (course);}}
First you create the Course object, set its properties, persist it to the database using the save method, and then query all the records in the database through the findAll method, of course, there is only one. And get the Id, get it through the findById method, and then print the result. Finally delete the record. By executing the program, we can get the following output information:
We did not create a table in the database before, but Hibernate will automatically build the table for us before performing the insert, then perform the insert operation, query the operation twice, and print out the object information, and finally perform the delete operation. From the SQL statement, you can see what the final execution result of Hibernate is. At this point, when you go back to the database, you will find a built table.
This is the end of Hibernate's simple ORM mapping operation. Let's use JPA annotations and Hibernate's API to persist the object. First, modify the persistent class:
Package org.ourpioneer.course.bean; import java.sql.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table / * course information description bean * * @ author Nanlei * * / @ Entity @ Table (name = "course") public class Course {@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) @ Column (name = "ID") private Long id; @ Column (name = "TITLE", length = 100, nullable = false) private String title @ Column (name = "STARTDATE", nullable=false) private java.util.Date startDate; @ Column (name = "ENDDATE", nullable=false) private java.util.Date endDate; @ Column (name = "FEE", nullable=false) private int fee; / / the rest remains unchanged, omitted}
Using JPA's annotations, first annotate the class, using @ Entity, and associate database tables with @ Table. Here is the primary key for the field. Use @ Id for the identifier (primary key) field, and specify the generation policy and the corresponding column name. For the remaining fields, you only need to specify the column information. Now tell Hibernate that we use JPA annotations instead of mapping files, as follows:
Modify the construction method of the DAO implementation class, and use the annotation configuration method to create a SessionFactory, as follows:
Public CourseDAOImpl () {/ / Configuration cfg = new Configuration () .configure (); Configuration cfg = new AnnotationConfiguration () .configure (); sessionFactory = cfg.buildSessionFactory ();}
At this point, the test method is executed again, and there is no change in the feedback, but we use JPA annotations instead of Hibernate mapping information. Let's take a look at the persistence steps using Hibernate as the JPA engine. Configure dependencies first, introducing:
Org.hibernategroupId > hibernate-entitymanagerartifactId > 3.6.0.Finalversion > jartype > compilescope > dependency > jbossgroupId > jboss-archive-browsingartifactId > 5.0.0alpha-200607201-119version > jartype > compilescope > dependency >
If you run JPA in the Java EE container, you can configure JPA through the container. If you are running JPA in Java SE, you need to configure persistence.xml under the META-INF of the classpath to configure the persistence unit. In this example, we use Hibernate as the engine of JPA, which can be written as follows:
"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 ">" course "> "hibernate.ejb.cfgfile" value= "/ hibernate.cfg.xml" / >
Loading the configuration file still uses the configuration file of Hibernate, or you can write the property in persistence.xml, so because JPA can obtain the persistence unit here, you need to configure the mapping of persistence objects in the configuration of Hibernate, and remove the mapping information. With the configuration information and the need to implement the class, we rewrite a DAO implementation of JPA as follows:
Java code
Package org.ourpioneer.course.dao; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.Query; import org.ourpioneer.course.bean.Course; public class CourseDAOImplJPA implements CourseDAO {private EntityManagerFactory entityManagerFactory; public CourseDAOImplJPA () {entityManagerFactory = Persistence.createEntityManagerFactory ("course") } public void delete (Course course) {EntityManager manager = entityManagerFactory.createEntityManager (); EntityTransaction tx = manager.getTransaction (); try {tx.begin (); manager.remove (manager.merge (course)); tx.commit () } catch (RuntimeException e) {tx.rollback (); throw e;} finally {manager.close ();}} public List findAll () {EntityManager manager = entityManagerFactory.createEntityManager () Try {Query query = manager .createQuery ("select course from Course course"); return query.getResultList ();} finally {manager.close ();}} public Course findById (Long courseId) {EntityManager manager = entityManagerFactory.createEntityManager () Try {return manager.find (Course.class, courseId);} finally {manager.close ();}} public void save (Course course) {EntityManager manager = entityManagerFactory.createEntityManager (); EntityTransaction tx = manager.getTransaction (); try {tx.begin () Manager.persist (course); tx.commit ();} catch (RuntimeException e) {tx.rollback (); throw e;} finally {manager.close () }} public void update (Course course) {EntityManager manager = entityManagerFactory.createEntityManager (); EntityTransaction tx = manager.getTransaction (); try {tx.begin (); manager.merge (course); tx.commit () } catch (RuntimeException e) {tx.rollback (); throw e;} finally {manager.close ();}
It is important to note that the delete method calls the merge method first, otherwise the current object is out of control and cannot be associated with Session, so the object cannot be deleted. An exception will be thrown without the merge method, so you can test it because the underlying layer is still carried out by Hibernate, and the persistent object of Hibernate has three states, so you should pay attention to the changes in the state.
Now let's look at how to configure the ORM resource factory in Spring, that is, to use the ORM framework in Spring. Still take Hibernate as an example to illustrate that spring-orm and spring-context modules should be introduced for testing. First of all, we can modify the writing of the DAO implementation class, because with Spring, there is no need to explicitly new the object, so for the SessionFactory of Hibernate, use injection to configure and modify the CourseDAOImpl class, as follows:
Private SessionFactory sessionFactory; public void setSessionFactory (SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}
Remove the constructor and provide a get method for sessionFactory. Then it's time to configure Spring, which is very simple, to configure courseDao and sessionFactory:
Bean > property > bean >
Add the mapping file of the Hibernate configuration object, and then modify the test method to get the object from the Spring container:
ApplicationContext ctx = new ClassPathXmlApplicationContext ("classpath:applicationContext.xml"); CourseDAO courseDAO = (CourseDAO) ctx.getBean ("courseDao")
At this point, we still rely on the configuration file of Hibernate, so we can move the configuration information in Hibernate to Spring, because the ORM module of Spring fully supports Hibernate, which can be done as follows. We use C3P0 as the connection pool:
Bean >
After configuring the basic information of the database, the configuration of the data source is complete. Here is the configuration Hibernate:
Org.hibernate.dialect.MySQLDialectprop > trueprop > updateprop > props > property > bean >
Here we configure the entity mapping together, using the * wildcard character, and configure basic Hibernate properties, such as dialects, display sql statements, and automatic table creation. All that's left is the configuration of DAO, which doesn't need to be modified, just inject sessionFactory, and then run the test:
You can see the startup information and the SQL statement generated by Hibernate. In addition to using Hibernate's entity mapping file, we can also use annotations. We have previously added annotations to the Course persistence class, so let's configure the way to use annotations. It is very simple, just modify the Hibernate SessionFactory configuration in Spring, as follows:
Org.ourpioneer.course.bean.Coursevalue > list > property > org.hibernate.dialect.MySQLDialectprop > trueprop > updateprop > props > property > bean >
This allows you to use the annotation information in the persistent class without having to write a separate HBM mapping file, execute the test, and the result is the same. Of course, in Spring, you can also use JPA's EntityManager for data persistence operations, so how to do this? Similar to the previous introduction, first modify the configuration of EntityManager in the DAO implementation class of JPA, using injection:
Private EntityManagerFactory entityManagerFactory; public void setEntityManagerFactory (EntityManagerFactory entityManagerFactory) {this.entityManagerFactory = entityManagerFactory;}
Similarly, modify the configuration file of Spring and configure EntityManagerFactory as follows:
Bean >
Because persistenceUnitName is configured, don't forget the persistence.xml file in the META-INF directory, which reads:
Properties > persistence-unit >
Because hibernate.cfg.xml is also used, but to remove all the mapping information from mapping, the courseDaoJPA configuration is simple:
Bean >
The DAO implementation object of JPA can be obtained by replacing courseDaoJPA in the ctx.getBean method of the test program, so that the database can be operated, which is also very simple. If you do not use hibernate's configuration file, you need to configure JPA as follows:
Bean > property > bean >
Inject the data source, configure some database dialects, display sql and generate ddl information, and finally simplify the contents of the persistence.xml file:
You no longer need to use Hibernate's configuration file, so that's fine. Execute the test program again, and you'll get the following output:
In the JDBC module, the JDBC template of Spring simplifies the operation of SQL, which makes it very easy to use SQL without a lot of code to complete the database operation. Similarly, in the ORM module, the template technology of Spring is also applied. Here we mainly look at Hibernate template and JPA template technology. It is very simple to use HibernateTemplate, which can be injected into the implementation class of DAO. It is very easy to use the methods provided by the Hibernate template to perform ORM operations:
Package org.ourpioneer.course.dao; import java.util.List; import org.ourpioneer.course.bean.Course; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.transaction.annotation.Transactional; public class CourseDAOImplHibernate implements CourseDAO {private HibernateTemplate hibernateTemplate; public void setHibernateTemplate (HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate @ Transactional public void delete (Course course) {hibernateTemplate.delete (course);} @ Transactional (readOnly = true) public List findAll () {return hibernateTemplate.find ("from Course") @ Transactional (readOnly = true) public Course findById (Long courseId) {return (Course) hibernateTemplate.get (Course.class, courseId);} @ Transactional public void save (Course course) {hibernateTemplate.save (course);} @ Transactional public void update (Course course) {hibernateTemplate.update (course) }}
The annotations are used to describe the transaction, so you don't have to write them in the configuration file. Write the code to let the Spring container know what we are doing, and configure as follows:
Bean > bean > bean >
Using the tx prefix, you just need to add the appropriate namespace description, which is also very simple. Modify the main program:
Package org.ourpioneer.course; import java.util.GregorianCalendar; import java.util.List; import org.ourpioneer.course.bean.Course; import org.ourpioneer.course.dao.CourseDAO; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo {public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("classpath:applicationContext.xml") CourseDAO courseDAO = (CourseDAO) ctx.getBean ("courseDaoHibernate"); Course course = new Course (); course.setTitle ("Spring ORM"); course.setStartDate (new GregorianCalendar (2011, 1,1). GetTime (); course.setEndDate (new GregorianCalendar (2011, 2, 1). GetTime (); course.setFee (2011); courseDAO.save (course) List courses = courseDAO.findAll (); Long courseId = courses.get (0). GetId (); course = courseDAO.findById (courseId); System.out.println (course); courseDAO.delete (course);}}
Run the main program, and you can see the execution effect:
The same function can be achieved compared to HibernateTemplate,JpaTemplate. Let's take a look at the following code:
Package org.ourpioneer.course.dao; import java.util.List; import org.ourpioneer.course.bean.Course; import org.springframework.orm.jpa.JpaTemplate; import org.springframework.transaction.annotation.Transactional; public class CourseDAOImplJPA implements CourseDAO {private JpaTemplate jpaTemplate; public void setJpaTemplate (JpaTemplate jpaTemplate) {this.jpaTemplate = jpaTemplate @ Transactional public void delete (Course course) {jpaTemplate.remove (jpaTemplate.merge (course));} @ Transactional (readOnly = true) public List findAll () {return jpaTemplate.find ("from Course") } @ Transactional (readOnly = true) public Course findById (Long courseId) {return jpaTemplate.find (Course.class, courseId);} @ Transactional public void save (Course course) {jpaTemplate.merge (course);} @ Transactional public void update (Course course) {jpaTemplate.merge (course) }}
Don't forget that the delete method needs to change the unmanaged object into a persistent state before it can operate, otherwise an exception will occur. Similarly, for the configuration file, you need to adjust the response:
Bean > bean > bean >
In this way, we modify the objects obtained by getBean in the test program, and we can test the program, which is very simple. Here we extend to say that after using HibernateTemplate or jpaTemplate, if you want to get Session or EntityManager, you can do this: hibernateTemplate.getSessionFactory (). GetCurrentSession (), for JPA: jpaTemplate.getEntityManager (), in addition, we can also use their execute () method to perform the operation:
HibernateTemplate.execute (new HibernateCallback () {public Object doInHibernate (Session session) throws HibernateException,SQLException {}})
JPA's are:
JpaTemplate.execute (new JpaCallback () {public Object doInJpa (EntityManager em) throws PersistenceException {return null;}})
Above, we directly inject the template into the DAO implementation class to operate. Of course, we can also let the implementation class inherit its own DaoSupport class to obtain the template for operation. This is very simple. Let's see:
Package org.ourpioneer.course.dao; import java.util.List; import org.ourpioneer.course.bean.Course; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional; public class CourseDAOImplHibernate extends HibernateDaoSupport implements CourseDAO {@ Transactional public void delete (Course course) {getHibernateTemplate () .delete (course) } @ Transactional (readOnly = true) public List findAll () {return getHibernateTemplate () .find ("from Course");} @ Transactional (readOnly = true) public Course findById (Long courseId) {return (Course) getHibernateTemplate () .get (Course.class, courseId) } @ Transactional public void save (Course course) {getHibernateTemplate () .save (course);} @ Transactional public void update (Course course) {getHibernateTemplate () .update (course);}}
Modify the configuration file at the same time:
Bean >
Of course, you can also inject HibernateTemplate into this class. If you take a look at the source code of this class, you can refer to the corresponding instance for injection. This is very simple, and the modification of JPA is similar:
Package org.ourpioneer.course.dao; import java.util.List; import org.ourpioneer.course.bean.Course; import org.springframework.orm.jpa.support.JpaDaoSupport; import org.springframework.transaction.annotation.Transactional; public class CourseDAOImplJPA extends JpaDaoSupport implements CourseDAO {@ Transactional public void delete (Course course) {getJpaTemplate () .remove (getJpaTemplate () .merge (course)) } @ Transactional (readOnly = true) public List findAll () {return getJpaTemplate () .find ("from Course");} @ Transactional (readOnly = true) public Course findById (Long courseId) {return getJpaTemplate () .find (Course.class, courseId) } @ Transactional public void save (Course course) {getJpaTemplate () .merge (course);} @ Transactional public void update (Course course) {getJpaTemplate () .merge (course);}}
Also, modify the configuration file to:
Bean >
Previously, we used HibernateTemplate for object persistence, but in fact, we can also use Hibernate's context Session to persist objects in the DAO implementation class. That is, get the Session through the getCurrentSession () object of the SessionFactory object, and then manipulate it through Session.
Let's adjust the code:
Package org.ourpioneer.course.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.ourpioneer.course.bean.Course; import org.springframework.transaction.annotation.Transactional; public class CourseDAOImplHibernate implements CourseDAO {private SessionFactory sessionFactory; public void setSessionFactory (SessionFactory sessionFactory) {this.sessionFactory = sessionFactory } @ Transactional public void delete (Course course) {sessionFactory.getCurrentSession () .delete (course);} @ Transactional (readOnly = true) public List findAll () {Query query = sessionFactory.getCurrentSession () .createQuery ("from Course"); return query.list () } @ Transactional (readOnly = true) public Course findById (Long courseId) {return (Course) sessionFactory.getCurrentSession () .get (Course.class, courseId);} @ Transactional public void save (Course course) {sessionFactory.getCurrentSession () .saveOrUpdate (course) @ Transactional public void update (Course course) {sessionFactory.getCurrentSession () .update (course);}}
It is important to note here that all DAO methods must be transactional, which can be done by adding Transactional annotations, which is simple and has been introduced before. This ensures that all methods in DAO can be executed in the same Session and within the same transaction, achieving the effect of using transactions.
Once the code has been modified, it's time to modify the configuration file:
Bean > bean >
In this way, the persistence object based on the context Session is configured and executed in the sample program, and you will see the effect. Modify the test program as follows:
Package org.ourpioneer.course; import java.util.GregorianCalendar; import java.util.List; import org.ourpioneer.course.bean.Course; import org.ourpioneer.course.dao.CourseDAO; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo {public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("classpath:applicationContext.xml") CourseDAO courseDAO = (CourseDAO) ctx.getBean ("courseDaoHibernate"); Course course = new Course (); course.setTitle ("Spring ORM"); course.setStartDate (new GregorianCalendar (2011, 2, 1). GetTime (); course.setEndDate (new GregorianCalendar (2011, 3, 1). GetTime (); course.setFee (2011); courseDAO.save (course) List courses = courseDAO.findAll (); Long courseId = courses.get (0). GetId (); course = courseDAO.findById (courseId); System.out.println (course); course.setFee; courseDAO.update (course); System.out.println (course); courseDAO.delete (course);}}
On the console, we can see the following output:
The difference between this approach and using HibernateTemplate is their handling of exceptions. HibernateTemplate will translate the exception into an exception in Spring's data access exception system. When we use contextual Session, we will not throw an exception of Spring, but HibernateException. If we still want to see the exception system of Spring, we need to make some settings, which is also very simple.
Annotate the DAO implementation class with @ Respository and register an instance of PersistenceExceptionTranslationPostProcessor. In the configuration file of Spring, we add the following:
Bean >
This is the case of using Hibernate, so the case of using JPA is similar to this, let's modify the DAO implementation class of JPA:
Package org.ourpioneer.course.dao; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.ourpioneer.course.bean.Course; import org.springframework.transaction.annotation.Transactional; public class CourseDAOImplJPA implements CourseDAO {@ PersistenceContext private EntityManager entityManager; @ Transactional public void delete (Course course) {entityManager.remove (entityManager.merge (course)) } @ Transactional (readOnly = true) public List findAll () {Query query = entityManager.createQuery ("from Course"); return query.getResultList ();} @ Transactional (readOnly = true) public Course findById (Long courseId) {return entityManager.find (Course.class, courseId) @ Transactional public void save (Course course) {entityManager.merge (course);} @ Transactional public void update (Course course) {entityManager.merge (course);}}
Here we use annotations to declare EntityManager, so we just need to declare an instance of PersistenceAnnotationBeanPostProcessor in the configuration file. The configuration file is modified to:
Bean > bean >
After that, replace getBean () in the test program with courseDaoJPA.
Like HibernateTemplate, JpaTemplate translates exceptions into data access exceptions of Spring. If changed to entityManagerFactory, exceptions will become exceptions of Java SE, such as illegal parameters, illegal status and so on. To continue using Spring's exception architecture, annotate the DAO implementation class of JPA with @ Repository, and then register the PersistenceExceptionTranslationPostProcessor instance.
The above is all the contents of the article "sample Analysis of ORM for Spring Database access". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.