Intellij 13.x JPA Persistence Sample

Source: Internet
Author: User

Similar to the previous article, there are some basic things.

This is JPA + Spring MVC 3.0.

1. Create a Project

2. Add JPA Support

3. We use Hibernate as an example to set the JPA Provider to Hibernate.

4. Add persistence. xml, where the standard location should be src/main/resources/META-INF, so we need to create a folder for the resource and META-INF

5. Go back to project structure> modules> JPA and add the created persistence. xml file to JPA.

6. Modify POM. XML to add two jars, one being hibernate-entitymanager and the other being mysql connector.

7. modify our persistence. xml (here you can also do not add persistence. xml, just build the META-INF, and then generate it from step 1)

            org.hibernate.ejb.HibernatePersistence                                                                                             

 

7. View-> Tools windows-> DataBase creates a DataSource

8. View-> Tools Windows-> Persistence-> Generate Persistence Mapping-> By Database Schema

Selected Location

9. At this time, we have the generated Entity.

10. Note that at this time, if you use Console> to perform hql queries, the system will display an error. Now you can ignore this issue and wait for the build to complete.

11. Check the persistence. xml file at this time. Check whether the class has been added to the xml file. The complete persistence. xml should be like this.

            org.hibernate.ejb.HibernatePersistence        com.springapp.modlels.OfficeEntity                                                                                             

12. add. jsp and modify the controller.

"/" ="message", "Hello world!" "hello"= RequestMethod.GET, value="add""message", "Hello world!"= Persistence.createEntityManagerFactory("personDB"== "test" "add"

Run it.

At this time, if we return persistence and console hql, there will be no problem.

Note: The No suitable Driver problem may occur in JBoss, starting with getTransaction (). I don't know why this problem occurs and it runs normally in Tomcat.

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.