forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#6943 from amit2103/BAEL-14320
[BAEL-14320] - Moved code for Intro to Spring Data JPA Article
- Loading branch information
Showing
16 changed files
with
669 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...e-simple/src/main/java/org/baeldung/spring/data/persistence/config/PersistenceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.baeldung.spring.data.persistence.config; | ||
|
||
import java.util.Properties; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.jdbc.datasource.DriverManagerDataSource; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" }) | ||
@ComponentScan({ "org.baeldung.spring.data.persistence" }) | ||
// @ImportResource("classpath*:springDataPersistenceConfig.xml") | ||
@EnableJpaRepositories(basePackages = "org.baeldung.spring.data.persistence.dao") | ||
public class PersistenceConfig { | ||
|
||
@Autowired | ||
private Environment env; | ||
|
||
public PersistenceConfig() { | ||
super(); | ||
} | ||
|
||
@Bean | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { | ||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); | ||
em.setDataSource(dataSource()); | ||
em.setPackagesToScan(new String[] { "org.baeldung.spring.data.persistence.model" }); | ||
|
||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); | ||
// vendorAdapter.set | ||
em.setJpaVendorAdapter(vendorAdapter); | ||
em.setJpaProperties(additionalProperties()); | ||
|
||
return em; | ||
} | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
final DriverManagerDataSource dataSource = new DriverManagerDataSource(); | ||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); | ||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); | ||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); | ||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); | ||
|
||
return dataSource; | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager transactionManager() { | ||
final JpaTransactionManager transactionManager = new JpaTransactionManager(); | ||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); | ||
|
||
return transactionManager; | ||
} | ||
|
||
@Bean | ||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { | ||
return new PersistenceExceptionTranslationPostProcessor(); | ||
} | ||
|
||
final Properties additionalProperties() { | ||
final Properties hibernateProperties = new Properties(); | ||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); | ||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); | ||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); | ||
return hibernateProperties; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...ng-persistence-simple/src/main/java/org/baeldung/spring/data/persistence/dao/IFooDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.baeldung.spring.data.persistence.dao; | ||
|
||
import org.baeldung.spring.data.persistence.model.Foo; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
public interface IFooDao extends JpaRepository<Foo, Long> { | ||
|
||
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)") | ||
Foo retrieveByName(@Param("name") String name); | ||
} |
83 changes: 83 additions & 0 deletions
83
...ring-persistence-simple/src/main/java/org/baeldung/spring/data/persistence/model/Foo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.baeldung.spring.data.persistence.model; | ||
|
||
import java.io.Serializable; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class Foo implements Serializable { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private long id; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
public Foo() { | ||
super(); | ||
} | ||
|
||
public Foo(final String name) { | ||
super(); | ||
|
||
this.name = name; | ||
} | ||
|
||
// API | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(final long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(final String name) { | ||
this.name = name; | ||
} | ||
|
||
// | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((name == null) ? 0 : name.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
final Foo other = (Foo) obj; | ||
if (name == null) { | ||
if (other.name != null) | ||
return false; | ||
} else if (!name.equals(other.name)) | ||
return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder builder = new StringBuilder(); | ||
builder.append("Foo [name=").append(name).append("]"); | ||
return builder.toString(); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...stence-simple/src/main/java/org/baeldung/spring/data/persistence/service/IFooService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.baeldung.spring.data.persistence.service; | ||
|
||
import org.baeldung.spring.data.persistence.model.Foo; | ||
|
||
import com.baeldung.persistence.dao.common.IOperations; | ||
|
||
public interface IFooService extends IOperations<Foo> { | ||
|
||
Foo retrieveByName(String name); | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...le/src/main/java/org/baeldung/spring/data/persistence/service/common/AbstractService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.baeldung.spring.data.persistence.service.common; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
import org.springframework.data.repository.PagingAndSortingRepository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.baeldung.persistence.dao.common.IOperations; | ||
import com.google.common.collect.Lists; | ||
|
||
@Transactional | ||
public abstract class AbstractService<T extends Serializable> implements IOperations<T> { | ||
|
||
// read - one | ||
|
||
@Override | ||
@Transactional(readOnly = true) | ||
public T findOne(final long id) { | ||
return getDao().findById(id).orElse(null); | ||
} | ||
|
||
// read - all | ||
|
||
@Override | ||
@Transactional(readOnly = true) | ||
public List<T> findAll() { | ||
return Lists.newArrayList(getDao().findAll()); | ||
} | ||
|
||
// write | ||
|
||
@Override | ||
public T create(final T entity) { | ||
return getDao().save(entity); | ||
} | ||
|
||
@Override | ||
public T update(final T entity) { | ||
return getDao().save(entity); | ||
} | ||
|
||
@Override | ||
public void delete(T entity) { | ||
getDao().delete(entity); | ||
} | ||
|
||
@Override | ||
public void deleteById(long entityId) { | ||
T entity = findOne(entityId); | ||
delete(entity); | ||
} | ||
|
||
protected abstract PagingAndSortingRepository<T, Long> getDao(); | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...ce-simple/src/main/java/org/baeldung/spring/data/persistence/service/impl/FooService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.baeldung.spring.data.persistence.service.impl; | ||
|
||
|
||
import org.baeldung.spring.data.persistence.model.Foo; | ||
import org.baeldung.spring.data.persistence.dao.IFooDao; | ||
import org.baeldung.spring.data.persistence.service.IFooService; | ||
import org.baeldung.spring.data.persistence.service.common.AbstractService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.repository.PagingAndSortingRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
public class FooService extends AbstractService<Foo> implements IFooService { | ||
|
||
@Autowired | ||
private IFooDao dao; | ||
|
||
public FooService() { | ||
super(); | ||
} | ||
|
||
// API | ||
|
||
@Override | ||
protected PagingAndSortingRepository<Foo, Long> getDao() { | ||
return dao; | ||
} | ||
|
||
// custom methods | ||
|
||
@Override | ||
public Foo retrieveByName(final String name) { | ||
return dao.retrieveByName(name); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
persistence-modules/spring-persistence-simple/src/main/java/org/baeldung/util/IDUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.baeldung.util; | ||
|
||
import java.util.Random; | ||
|
||
public final class IDUtil { | ||
|
||
private IDUtil() { | ||
throw new AssertionError(); | ||
} | ||
|
||
// API | ||
|
||
public static String randomPositiveLongAsString() { | ||
return Long.toString(randomPositiveLong()); | ||
} | ||
|
||
public static String randomNegativeLongAsString() { | ||
return Long.toString(randomNegativeLong()); | ||
} | ||
|
||
public static long randomPositiveLong() { | ||
long id = new Random().nextLong() * 10000; | ||
id = (id < 0) ? (-1 * id) : id; | ||
return id; | ||
} | ||
|
||
private static long randomNegativeLong() { | ||
long id = new Random().nextLong() * 10000; | ||
id = (id > 0) ? (-1 * id) : id; | ||
return id; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...ence-modules/spring-persistence-simple/src/main/resources/springDataPersistenceConfig.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd | ||
http://www.springframework.org/schema/data/jpa | ||
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" | ||
> | ||
|
||
<jpa:repositories base-package="org.baeldung.persistence.dao"/> | ||
|
||
</beans> |
Oops, something went wrong.