Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Spring Data JPA based repositories. #422

Closed
wants to merge 1 commit into from

3 participants

@ffbit
Collaborator

Hello!

Could you consider introduction of Spring Data - JPA based CRUD repositories to the spring-txn example project? It can help avoid writing some redundant code.

Thanks.
Have a great day.

@aslakhellesoy

Thanks a lot for your contribution! You clearly know more Spring than I do :-)

While I have you here - would you be interested in helping porting the spring-txn example to be a direct port of the AJAX example in the Cucumber book? (If so, I'll get you a copy if you don't have it).

I have been experimenting with this on a branch called spring-ajax, and the main problem I'm facing is to have a shared database transaction between the Cucumber Step Definitions and the Spring app. If I can get this to work it would be easier to clean the database with a rollback in an After. I thought I'd ask someone who's more intimate with Spring to help me.

/cc @paoloambrosio who wrote the original code my spring-ajax branch is based on.

@ffbit
Collaborator

Hello!

I'm glad to hear that.
Please send me a copy (or just a piece with the example) of that famous book and I'll do my best.
My email can be gotten from my commit.

Thanks in advance.
Have a nice day.

@paoloambrosio
Collaborator

I totally missed the spring-ajax branch!

I have done a few changes since I gave you the code (use mysql for production and hsql for test, introduced page objects, switchable js/non-js browser). Feel free to take a look: https://bitbucket.org/paoloambrosio/web-java/src (it was a private repository, so no licensing info, references to the book, etc.)

@ffbit
Collaborator

@paoloambrosio, I forked your repository on bitbucket. I'll take a look in a couple of days.

Thanks.

@ffbit
Collaborator

Hello,

There will be a local Ukrainian Hackathon [RU] in Kiyv.
And I've bought tickets on that event, but I haven't got a idea for the project.

So I think, why do not port the spring-txn example to a direct port of the AJAX example from the Cucumber book?
What do you think of that?

Thanks in advance.
Have a nice day.

@ffbit
Collaborator

I've made a research on MVC testing support in Spring 3.1
According to obtained information, there is no way to test Spring shared transactions and AJAX with testing tools, like Selenium, because Servlet Container in this situation is a black box for us.

So I can implement required functionality without shared transactions.
Is this OK?

@ffbit
Collaborator

I made the Chapter 14 of the Cucumber Book pull request.
Could you take a look at it, please

@aslakhellesoy

Isn't this PR obsoleted by #438 now?

@ffbit
Collaborator
@aslakhellesoy

I'm confused. You are saying this PR is obsoleted and you want me to accept it? (I assume you mean merge it in). That sounds contradictory to me.

@ffbit
Collaborator
@aslakhellesoy aslakhellesoy closed this pull request from a commit
@aslakhellesoy aslakhellesoy Closes #422. 2fe7fba
@aslakhellesoy

Ok, closed it, but since the two PRs were diverging, #438 no longer applies cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 11, 2012
  1. @ffbit
This page is out of date. Refresh to see the latest.
View
6 examples/spring-txn/pom.xml
@@ -15,6 +15,7 @@
<properties>
<hibernate.version>4.1.4.Final</hibernate.version>
<hsqldb.version>2.2.8</hsqldb.version>
+ <spring.data.version>1.1.0.RELEASE</spring.data.version>
</properties>
<dependencies>
@@ -37,6 +38,11 @@
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.springframework.data</groupId>
+ <artifactId>spring-data-jpa</artifactId>
+ <version>${spring.data.version}</version>
+ </dependency>
<dependency>
<groupId>org.hibernate</groupId>
View
20 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/JpaMessageRepository.java
@@ -1,20 +0,0 @@
-package cucumber.examples.spring.txn;
-
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import java.util.List;
-
-@Repository
-public class JpaMessageRepository implements MessageRepository {
- @PersistenceContext
- private EntityManager entityManager;
-
- @Transactional
- @Override
- public void save(Message message) {
- entityManager.persist(message);
- }
-}
View
25 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/JpaUserRepository.java
@@ -1,25 +0,0 @@
-package cucumber.examples.spring.txn;
-
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import java.util.List;
-
-@Repository
-public class JpaUserRepository implements UserRepository {
- @PersistenceContext
- private EntityManager entityManager;
-
- @Transactional
- @Override
- public void save(User user) {
- entityManager.persist(user);
- }
-
- @Override
- public List<User> findAll() {
- return entityManager.createQuery("SELECT b FROM User b", User.class).getResultList();
- }
-}
View
6 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/MessageRepository.java
@@ -1,5 +1,7 @@
package cucumber.examples.spring.txn;
-public interface MessageRepository {
- void save(Message message);
+import org.springframework.data.repository.CrudRepository;
+
+public interface MessageRepository extends CrudRepository<Message, Long> {
+
}
View
6 examples/spring-txn/src/main/java/cucumber/examples/spring/txn/UserRepository.java
@@ -1,9 +1,7 @@
package cucumber.examples.spring.txn;
-import java.util.List;
+import org.springframework.data.repository.CrudRepository;
-public interface UserRepository {
- void save(User user);
+public interface UserRepository extends CrudRepository<User, Long> {
- List<User> findAll();
}
View
2  examples/spring-txn/src/main/resources/applicationContext.xml
@@ -5,8 +5,6 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:component-scan base-package="cucumber.examples.spring.txn"/>
-
<import resource="persistenceContext.xml"/>
</beans>
View
7 examples/spring-txn/src/main/resources/persistenceContext.xml
@@ -3,13 +3,16 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
+ xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
+ http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
+ http://www.springframework.org/schema/data/jpa
+ http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
@@ -43,4 +46,6 @@
<tx:annotation-driven/>
+ <jpa:repositories base-package="cucumber.examples.spring.txn"/>
+
</beans>
View
1  examples/spring-txn/src/test/resources/cucumber.xml
@@ -4,7 +4,6 @@
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:component-scan base-package="cucumber.examples.spring.txn"/>
<context:annotation-config/>
<import resource="classpath*:/applicationContext.xml"/>
Something went wrong with that request. Please try again.