Spring uses memory database 2

Source: Internet
Author: User

In the previous article Spring memory database, we used Hibernate as the orm framework and directly called the Hibernate API for related persistence operations. However, in actual projects, it may be required to use the JPA interface for persistence for various reasons, and the JPA implementation can be flexibly configured to avoid excessive dependency on third-party JPA implementation, this may sacrifice some additional optimizations and features brought about by the third-party JPA implementation, but does avoid dependency on it. This article will change the implementation of the Demo of Spring memory database, use the JPA standard interface for persistent operations, and the JPA implementation can be configured to support Hibernate or EclipseLink.


Step 1: Change the HibernateConfiguration class to the JpaConfiguration class.

@ Configurationpublic class JpaConfiguration {@ Autowired private DataSource dataSource; @ Bean public Map <String, Object> jpaProperties () {Map <String, Object> props = new HashMap <String, object> (); // Hibernate JPA properties/* props. put ("hibernate. dialect ", H2Dialect. class. getName (); props. put ("hibernate. cache. provider_class ", HashtableCacheProvider. class. getName (); * // invoke selink JPA properties props. put ("Your selink. weaving "," false "); return props;} @ Bean public JpaVendorAdapter jpaVendorAdapter () {// Hibernate JPA Vendor Adapter/* AbstractJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter (); * // export selink JPA Vendor Adapter AbstractJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter (); // Common vendor settings jpaVendorAdapter. setShowSql (false); jpaVendorAdapter. setGenerateDdl (true); jpaVendorAdapter. setDatabase (Database. h2); // specify the database as H2 return jpaVendorAdapter;} @ Bean public PlatformTransactionManager transactionManager () {return new JpaTransactionManager (localContainerEntityManagerFactoryBean (). getObject ();} @ Bean public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean () {LocalContainerEntityManagerFactoryBean lef = new principal (); lef. setLoadTimeWeaver (new InstrumentationLoadTimeWeaver (); lef. setDataSource (this. dataSource); lef. setJpaPropertyMap (this. jpaProperties (); lef. setJpaVendorAdapter (this. jpaVendorAdapter (); return lef;}/*** set the data source. You can specify the data type as Derby or HSQLDB * @ return */@ Bean public DataSource dataSource () {EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder (); builder. setType (EmbeddedDatabaseType. h2); return builder. build ();}}

Step 2: Modify the OrderPersistenceTests class

Modify the API operation of the org. hibernate. Session class to the API of the javax. persistence. EntityManager class.

Sample Code of the org. hibernate. Session API:

@Autowired    private SessionFactory sessionFactory;    @Test    @Transactional    public void testSaveOrderWithItems() throws Exception {        Session session = sessionFactory.getCurrentSession();        Order order = new Order();        order.getItems().add(new Item());        session.save(order);        session.flush();        assertNotNull(order.getId());    }

Javax. persistence. EntityManager class API code example:

@PersistenceContext    private EntityManager entityManager;    @Test    @Transactional    public void testSaveOrderWithItems() throws Exception {        Order order = new Order();        order.getItems().add(new Item());        entityManager.persist(order);        entityManager.flush();        assertNotNull(order.getId());    }

Step 3: Modify the Maven dependency. Refer to the attachment.


After the modification is completed, you can start to run the test case for debugging! I always feel that EclipseLink is a semi-finished product, which is not very good. Oracle still wants Everybody to use TopLink!

This article is from "the power comes from the sincere love !" Blog, please be sure to keep this source http://stevex.blog.51cto.com/4300375/1343969

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.