Skip to content

Commit

Permalink
#8 - Expand examples to show JavaConfig more prominently.
Browse files Browse the repository at this point in the history
Added additional examples for java config. Restructured the packages a bit according to the particular use case. Updated spring-data-jpa dependency to 1.4.0.RC1.

Original pull request: #9.
  • Loading branch information
Thomas Darimont authored and odrotbohm committed Sep 3, 2013
1 parent 0347d19 commit e8b0399
Show file tree
Hide file tree
Showing 40 changed files with 640 additions and 109 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<aspectj.version>1.6.12</aspectj.version>
<jodatime.version>2.2</jodatime.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.data.jpa.version>1.4.0.M1</spring.data.jpa.version>
<spring.data.jpa.version>1.4.0.RC1</spring.data.jpa.version>
</properties>

<build>
Expand Down Expand Up @@ -153,7 +153,7 @@
<version>2.2.8</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.auditing;

import javax.persistence.Entity;
Expand All @@ -10,6 +25,7 @@
* {@link Auditable} or extend {@link AbstractAuditable}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@Entity
public class AuditableUser extends AbstractAuditable<AuditableUser, Long> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.auditing;

import org.springframework.data.domain.AuditorAware;
Expand All @@ -8,6 +23,7 @@
* current user.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class AuditorAwareImpl implements AuditorAware<AuditableUser> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Package showing auditing support with Spring Data repositories.
*/
package org.springframework.data.jpa.example.auditing;
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.domain;

import javax.persistence.Column;
Expand All @@ -10,15 +25,15 @@
* Sample user class.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@Entity
@NamedQuery(name = "User.findByTheUsersName", query = "from User u where u.username = ?1")
public class User extends AbstractPersistable<Long> {

private static final long serialVersionUID = -2952735933715107252L;

@Column(unique = true)
private String username;
@Column(unique = true) private String username;

private String firstname;
private String lastname;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Java config to use Spring Data JPA alongside the Spring caching support.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@Configuration
@EnableJpaRepositories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* User repository using Spring's caching abstraction.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public interface CachingUserRepository extends CrudRepository<User, Long> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* Sample for the integration of the Spring Caching abstraction with Spring Data repositories.
*/
package org.springframework.data.jpa.example.repository.caching;
package org.springframework.data.jpa.example.repository.caching;

Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package org.springframework.data.jpa.example.repository;
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.repository.custom;

import java.util.List;

Expand All @@ -12,6 +27,7 @@
* {@link JpaRepository}. Includes custom implemented functionality by extending {@link UserRepositoryCustom}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.repository.custom;

import java.util.List;

import org.springframework.data.jpa.example.domain.User;

/**
* Interface for repository functionality that ought to be implemented manually.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
interface UserRepositoryCustom {

/**
* Custom repository operation.
*
* @return
*/
List<User> myCustomBatchOperation();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package org.springframework.data.jpa.example.repository;
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.repository.custom;

import java.util.List;

Expand Down Expand Up @@ -29,14 +44,14 @@
* &lt;jpa:repositories base-package=&quot;com.acme.repository&quot; /&gt;
* </pre>
*
* If you need to manually configure the custom instance see {@link UserRepositoryJdbcImpl} for an example.
* If you need to manually configure the custom instance see {@link UserRepositoryImplJdbc} for an example.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
class UserRepositoryImpl implements UserRepositoryCustom {

@PersistenceContext
private EntityManager em;
@PersistenceContext private EntityManager em;

/**
* Configure the entity manager to be used.
Expand All @@ -54,6 +69,7 @@ public void setEntityManager(EntityManager em) {
public List<User> myCustomBatchOperation() {

CriteriaQuery<User> criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
criteriaQuery.select(criteriaQuery.from(User.class));
return em.createQuery(criteriaQuery).getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
package org.springframework.data.jpa.example.repository;
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.repository.custom;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.example.domain.User;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;

/**
* Class with the implementation of the custom repository code. Uses JDBC in this case. For basic programatic setup see
Expand All @@ -27,11 +47,19 @@
* logic as the default postfix for custom DAO instances is {@code Impl}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
class UserRepositoryJdbcImpl extends JdbcDaoSupport implements UserRepositoryCustom {
@Profile("jdbc")
@Component("userRepositoryImpl")
class UserRepositoryImplJdbc extends JdbcDaoSupport implements UserRepositoryCustom {

private static final String COMPLICATED_SQL = "SELECT * FROM User";

@Autowired
public UserRepositoryImplJdbc(DataSource dataSource) {
setDataSource(dataSource);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jpa.example.repository.UserRepositoryCustom#myCustomBatchOperation()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Package showing a repository interface to use basic finder method execution functionality as well as <em>customized</em> repository functionality.
*/
package org.springframework.data.jpa.example.repository.custom;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.example.repository.simple;

import java.util.List;
Expand All @@ -12,6 +27,7 @@
* methods to retrieve single entities or collections of them.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public interface SimpleUserRepository extends CrudRepository<User, Long> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Package showing a simple repository interface to use basic finder method execution functionality.
*/
package org.springframework.data.jpa.example.repository.simple;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="jpa.sample" />
<persistence-unit name="jpa.sample"/>
<persistence-unit name="jpa.sample.plain">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
Expand Down
Loading

0 comments on commit e8b0399

Please sign in to comment.