简体   繁体   中英

start using jpa in spring

Im really dont get it. I download STS Spring Tool Suite, and i want to use JPA in my MVC app. I have to add some JAR's? where can i find that JAR's, how do i know what JAR's are? what i have to add to my pom.xml file?

I already checked many sites but anyone tell exactly how to make it work. Its like the assume you know some steps that i really dont know what steps are.

Then, if i create more than one repository for differents objects, they do the transactions to the same database?

在pom.xml中使用它

<dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency>

I recommend visiting http://mvnrepository.com . I have used that in the past to find out what I need to add to the POM. Also, IDEs like IntelliJ and Eclipse make this really easy by providing arch-types, etc. For example, to get spring go here: http://mvnrepository.com/artifact/org.springframework/spring-context/4.1.0.RELEASE .

Here is something to get your started. Just add these dependencies and you'll get jpa along with spring. Removing the persistence this is what you would need to make a simple spring mvc web server. With the persistence, you'll have to set things up.

   <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>

There are lots of example on the net. I'm sure if you searched you could find something. Anyway I have a project here you can use to get started:

https://github.com/alanhay/spring-data-jpa-bootstrap

This is a minimal project that has everything you need including some unit tests that will run against an in-memory HSQLDB database.

You can download the ZIP and import to Eclipse/STS or check it out as a git project. For the ZIP:

  • download, unzip and then in Eclipse/STS select Import > Existing Maven Project > Select the unzipped folder.

  • Once imported right click on the project and run mvn > install (because the project uses QueryDSL requires a full Maven build rather than Eclipse incremental build but there is a plugin you can setup).

  • Open the test class UserRepositoryTest and run.

Also, further to the comments above, the persistence-api is just a specification. You also need to add dependency for an implementation eg Hibernate, OpenJPA, EclipseLink. My example project uses Hibernate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM