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.
- Loading branch information
Showing
66 changed files
with
1,034 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
========= | ||
|
||
## Spring Data JPA Example Project | ||
|
||
### Relevant Articles: | ||
- [Spring Data JPA – Derived Delete Methods](https://www.baeldung.com/spring-data-jpa-deleteby) | ||
- [JPA Join Types](https://www.baeldung.com/jpa-join-types) | ||
- [Case Insensitive Queries with Spring Data Repository](https://www.baeldung.com/spring-data-case-insensitive-queries) | ||
- [The Exists Query in Spring Data](https://www.baeldung.com/spring-data-exists-query) | ||
- [Spring Data JPA Repository Populators](https://www.baeldung.com/spring-data-jpa-repository-populators) | ||
- [Spring Data JPA and Null Parameters](https://www.baeldung.com/spring-data-jpa-null-parameters) | ||
- [Spring Data JPA Projections](https://www.baeldung.com/spring-data-jpa-projections) | ||
- [JPA @Embedded And @Embeddable](https://www.baeldung.com/jpa-embedded-embeddable) | ||
- [Spring Data JPA Delete and Relationships](https://www.baeldung.com/spring-data-jpa-delete) | ||
|
||
========= | ||
|
||
## Spring Data JPA Example Project | ||
|
||
### Relevant Articles: | ||
- [Spring Data JPA – Derived Delete Methods](https://www.baeldung.com/spring-data-jpa-deleteby) | ||
- [JPA Join Types](https://www.baeldung.com/jpa-join-types) | ||
- [Case Insensitive Queries with Spring Data Repository](https://www.baeldung.com/spring-data-case-insensitive-queries) | ||
- [The Exists Query in Spring Data](https://www.baeldung.com/spring-data-exists-query) | ||
- [Spring Data JPA Repository Populators](https://www.baeldung.com/spring-data-jpa-repository-populators) | ||
- [Spring Data JPA and Null Parameters](https://www.baeldung.com/spring-data-jpa-null-parameters) | ||
- [Spring Data JPA Projections](https://www.baeldung.com/spring-data-jpa-projections) | ||
- [JPA @Embedded And @Embeddable](https://www.baeldung.com/jpa-embedded-embeddable) | ||
- [Spring Data JPA Delete and Relationships](https://www.baeldung.com/spring-data-jpa-delete) |
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
39 changes: 39 additions & 0 deletions
39
...ng-persistence-simple/src/main/java/com/baeldung/hibernate/bootstrap/BarHibernateDAO.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,39 @@ | ||
package com.baeldung.hibernate.bootstrap; | ||
|
||
import com.baeldung.hibernate.bootstrap.model.TestEntity; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public abstract class BarHibernateDAO { | ||
|
||
@Autowired | ||
private SessionFactory sessionFactory; | ||
|
||
public TestEntity findEntity(int id) { | ||
|
||
return getCurrentSession().find(TestEntity.class, 1); | ||
} | ||
|
||
public void createEntity(TestEntity entity) { | ||
|
||
getCurrentSession().save(entity); | ||
} | ||
|
||
public void createEntity(int id, String newDescription) { | ||
|
||
TestEntity entity = findEntity(id); | ||
entity.setDescription(newDescription); | ||
getCurrentSession().save(entity); | ||
} | ||
|
||
public void deleteEntity(int id) { | ||
|
||
TestEntity entity = findEntity(id); | ||
getCurrentSession().delete(entity); | ||
} | ||
|
||
protected Session getCurrentSession() { | ||
return sessionFactory.getCurrentSession(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...ring-persistence-simple/src/main/java/com/baeldung/hibernate/bootstrap/HibernateConf.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,61 @@ | ||
package com.baeldung.hibernate.bootstrap; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.orm.hibernate5.HibernateTransactionManager; | ||
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Properties; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@PropertySource({ "classpath:persistence-h2.properties" }) | ||
public class HibernateConf { | ||
|
||
@Autowired | ||
private Environment env; | ||
|
||
@Bean | ||
public LocalSessionFactoryBean sessionFactory() { | ||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); | ||
sessionFactory.setDataSource(dataSource()); | ||
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.hibernate.bootstrap.model" }); | ||
sessionFactory.setHibernateProperties(hibernateProperties()); | ||
|
||
return sessionFactory; | ||
} | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
final BasicDataSource dataSource = new BasicDataSource(); | ||
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 hibernateTransactionManager() { | ||
final HibernateTransactionManager transactionManager = new HibernateTransactionManager(); | ||
transactionManager.setSessionFactory(sessionFactory().getObject()); | ||
return transactionManager; | ||
} | ||
|
||
private final Properties hibernateProperties() { | ||
final Properties hibernateProperties = new Properties(); | ||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); | ||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); | ||
|
||
return hibernateProperties; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...g-persistence-simple/src/main/java/com/baeldung/hibernate/bootstrap/HibernateXMLConf.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,24 @@ | ||
package com.baeldung.hibernate.bootstrap; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.ImportResource; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.orm.hibernate5.HibernateTransactionManager; | ||
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Properties; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@ImportResource({ "classpath:hibernate5Configuration.xml" }) | ||
public class HibernateXMLConf { | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...g-persistence-simple/src/main/java/com/baeldung/hibernate/bootstrap/model/TestEntity.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,29 @@ | ||
package com.baeldung.hibernate.bootstrap.model; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class TestEntity { | ||
|
||
private int id; | ||
|
||
private String description; | ||
|
||
@Id | ||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ing-persistence-simple/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.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,14 @@ | ||
package com.baeldung.persistence.dao.common; | ||
|
||
import java.io.Serializable; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
public abstract class AbstractDao<T extends Serializable> implements IOperations<T> { | ||
|
||
protected Class<T> clazz; | ||
|
||
protected final void setClazz(final Class<T> clazzToSet) { | ||
clazz = Preconditions.checkNotNull(clazzToSet); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...stence-simple/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.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,59 @@ | ||
package com.baeldung.persistence.dao.common; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
@SuppressWarnings("unchecked") | ||
public abstract class AbstractHibernateDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> { | ||
|
||
@Autowired | ||
protected SessionFactory sessionFactory; | ||
|
||
// API | ||
|
||
@Override | ||
public T findOne(final long id) { | ||
return (T) getCurrentSession().get(clazz, id); | ||
} | ||
|
||
@Override | ||
public List<T> findAll() { | ||
return getCurrentSession().createQuery("from " + clazz.getName()).list(); | ||
} | ||
|
||
@Override | ||
public void create(final T entity) { | ||
Preconditions.checkNotNull(entity); | ||
getCurrentSession().saveOrUpdate(entity); | ||
} | ||
|
||
@Override | ||
public T update(final T entity) { | ||
Preconditions.checkNotNull(entity); | ||
return (T) getCurrentSession().merge(entity); | ||
} | ||
|
||
@Override | ||
public void delete(final T entity) { | ||
Preconditions.checkNotNull(entity); | ||
getCurrentSession().delete(entity); | ||
} | ||
|
||
@Override | ||
public void deleteById(final long entityId) { | ||
final T entity = findOne(entityId); | ||
Preconditions.checkState(entity != null); | ||
delete(entity); | ||
} | ||
|
||
protected Session getCurrentSession() { | ||
return sessionFactory.getCurrentSession(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...ing-persistence-simple/src/main/java/com/baeldung/persistence/dao/common/IOperations.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,20 @@ | ||
package com.baeldung.persistence.dao.common; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
public interface IOperations<T extends Serializable> { | ||
|
||
T findOne(final long id); | ||
|
||
List<T> findAll(); | ||
|
||
void create(final T entity); | ||
|
||
T update(final T entity); | ||
|
||
void delete(final T entity); | ||
|
||
void deleteById(final long entityId); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...les/spring-persistence-simple/src/main/java/com/baeldung/persistence/dao/impl/FooDao.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,20 @@ | ||
package com.baeldung.persistence.dao.impl; | ||
|
||
import org.baeldung.persistence.dao.IFooDao; | ||
import org.baeldung.persistence.model.Foo; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.baeldung.persistence.dao.common.AbstractHibernateDao; | ||
|
||
@Repository | ||
public class FooDao extends AbstractHibernateDao<Foo> implements IFooDao { | ||
|
||
public FooDao() { | ||
super(); | ||
|
||
setClazz(Foo.class); | ||
} | ||
|
||
// API | ||
|
||
} |
Oops, something went wrong.