Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC with Spring Data JPA
    text
    copied!<p>The following tutorial (<a href="https://spring.io/guides/gs/accessing-data-jpa/" rel="nofollow">Spring Data JPA Tutorial</a>) explains how to get started with an application that uses Spring Data JPA to store and retrieve data in a relational database.</p> <p>I'm currently having some trouble getting this to work together with SpringMVC. I have a very simple MVC app and I'm trying to integrate Spring Data JPA into it.</p> <p>The above tutorial is unfortunately not very helpful in my case since it doesn't explain how to use Spring Data together with MVC.</p> <p>The tutorial explains how to set up an <code>Application</code> class where all the necessary beans are configured and then it has a simple main method that uses one of the example repositories.</p> <p>My question is: How do I configure my MVC app in the same way as in the tutorial so that I can start using Spring Data in MVC?</p> <p>Here's the code of the <code>Application</code> class just for reference:</p> <pre><code>@Configuration @EnableJpaRepositories public class Application { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder().setType(H2).build(); } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setDataSource(dataSource); lef.setJpaVendorAdapter(jpaVendorAdapter); lef.setPackagesToScan("hello"); return lef; } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setShowSql(false); hibernateJpaVendorAdapter.setGenerateDdl(true); hibernateJpaVendorAdapter.setDatabase(Database.H2); return hibernateJpaVendorAdapter; } @Bean public PlatformTransactionManager transactionManager() { return new JpaTransactionManager(); } public static void main(String[] args) { AbstractApplicationContext context = new AnnotationConfigApplicationContext(Application.class); CustomerRepository repository = context.getBean(CustomerRepository.class); // save a couple of customers repository.save(new Customer("Jack", "Bauer")); repository.save(new Customer("Chloe", "O'Brian")); repository.save(new Customer("Kim", "Bauer")); repository.save(new Customer("David", "Palmer")); repository.save(new Customer("Michelle", "Dessler")); // fetch all customers Iterable&lt;Customer&gt; customers = repository.findAll(); System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : customers) { System.out.println(customer); } System.out.println(); // fetch an individual customer by ID Customer customer = repository.findOne(1L); System.out.println("Customer found with findOne(1L):"); System.out.println("--------------------------------"); System.out.println(customer); System.out.println(); // fetch customers by last name List&lt;Customer&gt; bauers = repository.findByLastName("Bauer"); System.out.println("Customer found with findByLastName('Bauer'):"); System.out.println("--------------------------------------------"); for (Customer bauer : bauers) { System.out.println(bauer); } context.close(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload