Rename package to conform to standards

This commit is contained in:
David Morley 2016-01-20 21:04:35 -06:00
parent 825940ba7c
commit 226e23b603
75 changed files with 1096 additions and 1097 deletions

View File

@ -1,6 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<groupId>com.baeldung</groupId>
<artifactId>spring-hibernate4</artifactId>
<version>0.1-SNAPSHOT</version>

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.dao.common.IAuditOperations;
import com.baeldung.persistence.model.Bar;
public interface IBarAuditableDao extends IBarDao, IAuditOperations<Bar> {
//
}

View File

@ -1,10 +1,10 @@
package org.baeldung.persistence.dao;
import java.io.Serializable;
import org.baeldung.persistence.model.Bar;
import org.springframework.data.repository.CrudRepository;
public interface IBarCrudRepository extends CrudRepository<Bar, Serializable> {
//
}
package com.baeldung.persistence.dao;
import java.io.Serializable;
import com.baeldung.persistence.model.Bar;
import org.springframework.data.repository.CrudRepository;
public interface IBarCrudRepository extends CrudRepository<Bar, Serializable> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.model.Bar;
public interface IBarDao extends IOperations<Bar> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Child;
import com.baeldung.persistence.dao.common.IOperations;
public interface IChildDao extends IOperations<Child> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.dao.common.IAuditOperations;
import com.baeldung.persistence.model.Foo;
public interface IFooAuditableDao extends IFooDao, IAuditOperations<Foo> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.dao.common.IOperations;
public interface IFooDao extends IOperations<Foo> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Parent;
import com.baeldung.persistence.dao.common.IOperations;
public interface IParentDao extends IOperations<Parent> {
//
}

View File

@ -1,14 +1,14 @@
package org.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);
}
}
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);
}
}

View File

@ -1,37 +1,37 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.query.AuditQuery;
@SuppressWarnings("unchecked")
public class AbstractHibernateAuditableDao<T extends Serializable> extends AbstractHibernateDao<T> implements IAuditOperations<T> {
@Override
public List<T> getEntitiesAtRevision(final Number revision) {
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
final AuditQuery query = auditReader.createQuery().forEntitiesAtRevision(clazz, revision);
final List<T> resultList = query.getResultList();
return resultList;
}
@Override
public List<T> getEntitiesModifiedAtRevision(final Number revision) {
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
final AuditQuery query = auditReader.createQuery().forEntitiesModifiedAtRevision(clazz, revision);
final List<T> resultList = query.getResultList();
return resultList;
}
@Override
public List<T> getRevisions() {
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
final AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(clazz, true, true);
final List<T> resultList = query.getResultList();
return resultList;
}
}
package com.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.query.AuditQuery;
@SuppressWarnings("unchecked")
public class AbstractHibernateAuditableDao<T extends Serializable> extends AbstractHibernateDao<T> implements IAuditOperations<T> {
@Override
public List<T> getEntitiesAtRevision(final Number revision) {
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
final AuditQuery query = auditReader.createQuery().forEntitiesAtRevision(clazz, revision);
final List<T> resultList = query.getResultList();
return resultList;
}
@Override
public List<T> getEntitiesModifiedAtRevision(final Number revision) {
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
final AuditQuery query = auditReader.createQuery().forEntitiesModifiedAtRevision(clazz, revision);
final List<T> resultList = query.getResultList();
return resultList;
}
@Override
public List<T> getRevisions() {
final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession());
final AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(clazz, true, true);
final List<T> resultList = query.getResultList();
return resultList;
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.dao.common;
package com.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;

View File

@ -1,56 +1,56 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
public class AbstractJpaDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> {
@PersistenceContext
private EntityManager em;
// API
@Override
public T findOne(final long id) {
return em.find(clazz, Long.valueOf(id).intValue());
}
@Override
public List<T> findAll() {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<T> cq = cb.createQuery(clazz);
final Root<T> rootEntry = cq.from(clazz);
final CriteriaQuery<T> all = cq.select(rootEntry);
final TypedQuery<T> allQuery = em.createQuery(all);
return allQuery.getResultList();
}
@Override
public void create(final T entity) {
em.persist(entity);
}
@Override
public T update(final T entity) {
em.merge(entity);
return entity;
}
@Override
public void delete(final T entity) {
em.remove(entity);
}
@Override
public void deleteById(final long entityId) {
delete(findOne(entityId));
}
}
package com.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
public class AbstractJpaDao<T extends Serializable> extends AbstractDao<T> implements IOperations<T> {
@PersistenceContext
private EntityManager em;
// API
@Override
public T findOne(final long id) {
return em.find(clazz, Long.valueOf(id).intValue());
}
@Override
public List<T> findAll() {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<T> cq = cb.createQuery(clazz);
final Root<T> rootEntry = cq.from(clazz);
final CriteriaQuery<T> all = cq.select(rootEntry);
final TypedQuery<T> allQuery = em.createQuery(all);
return allQuery.getResultList();
}
@Override
public void create(final T entity) {
em.persist(entity);
}
@Override
public T update(final T entity) {
em.merge(entity);
return entity;
}
@Override
public void delete(final T entity) {
em.remove(entity);
}
@Override
public void deleteById(final long entityId) {
delete(findOne(entityId));
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.dao.common;
package com.baeldung.persistence.dao.common;
import java.io.Serializable;

View File

@ -1,14 +1,14 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;
public interface IAuditOperations<T extends Serializable> {
List<T> getEntitiesAtRevision(Number revision);
List<T> getEntitiesModifiedAtRevision(Number revision);
List<T> getRevisions();
}
package com.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;
public interface IAuditOperations<T extends Serializable> {
List<T> getEntitiesAtRevision(Number revision);
List<T> getEntitiesModifiedAtRevision(Number revision);
List<T> getRevisions();
}

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.dao.common;
package com.baeldung.persistence.dao.common;
import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.dao.common;
package com.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;

View File

@ -1,28 +1,28 @@
package org.baeldung.persistence.dao.impl;
import java.util.List;
import org.baeldung.persistence.dao.IBarAuditableDao;
import org.baeldung.persistence.dao.common.AbstractHibernateAuditableDao;
import org.baeldung.persistence.model.Bar;
public class BarAuditableDao extends AbstractHibernateAuditableDao<Bar> implements IBarAuditableDao {
public BarAuditableDao() {
super();
setClazz(Bar.class);
}
// API
@Override
public List<Bar> getRevisions() {
final List<Bar> resultList = super.getRevisions();
for (final Bar bar : resultList) {
bar.getFooSet().size(); // force FooSet initialization
}
return resultList;
}
package com.baeldung.persistence.dao.impl;
import java.util.List;
import com.baeldung.persistence.dao.IBarAuditableDao;
import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao;
import com.baeldung.persistence.model.Bar;
public class BarAuditableDao extends AbstractHibernateAuditableDao<Bar> implements IBarAuditableDao {
public BarAuditableDao() {
super();
setClazz(Bar.class);
}
// API
@Override
public List<Bar> getRevisions() {
final List<Bar> resultList = super.getRevisions();
for (final Bar bar : resultList) {
bar.getFooSet().size(); // force FooSet initialization
}
return resultList;
}
}

View File

@ -1,19 +1,19 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.dao.IBarDao;
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
import org.baeldung.persistence.model.Bar;
import org.springframework.stereotype.Repository;
@Repository
public class BarDao extends AbstractHibernateDao<Bar> implements IBarDao {
public BarDao() {
super();
setClazz(Bar.class);
}
// API
}
package com.baeldung.persistence.dao.impl;
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.model.Bar;
import org.springframework.stereotype.Repository;
@Repository
public class BarDao extends AbstractHibernateDao<Bar> implements IBarDao {
public BarDao() {
super();
setClazz(Bar.class);
}
// API
}

View File

@ -1,19 +1,19 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.dao.IBarDao;
import org.baeldung.persistence.dao.common.AbstractJpaDao;
import org.baeldung.persistence.model.Bar;
import org.springframework.stereotype.Repository;
@Repository
public class BarJpaDao extends AbstractJpaDao<Bar> implements IBarDao {
public BarJpaDao() {
super();
setClazz(Bar.class);
}
// API
}
package com.baeldung.persistence.dao.impl;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.common.AbstractJpaDao;
import com.baeldung.persistence.model.Bar;
import org.springframework.stereotype.Repository;
@Repository
public class BarJpaDao extends AbstractJpaDao<Bar> implements IBarDao {
public BarJpaDao() {
super();
setClazz(Bar.class);
}
// API
}

View File

@ -0,0 +1,19 @@
package com.baeldung.persistence.dao.impl;
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
import com.baeldung.persistence.model.Child;
import com.baeldung.persistence.dao.IChildDao;
import org.springframework.stereotype.Repository;
@Repository
public class ChildDao extends AbstractHibernateDao<Child> implements IChildDao {
public ChildDao() {
super();
setClazz(Child.class);
}
// API
}

View File

@ -0,0 +1,17 @@
package com.baeldung.persistence.dao.impl;
import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.dao.IFooAuditableDao;
public class FooAuditableDao extends AbstractHibernateAuditableDao<Foo> implements IFooAuditableDao {
public FooAuditableDao() {
super();
setClazz(Foo.class);
}
// API
}

View File

@ -1,8 +1,8 @@
package org.baeldung.persistence.dao.impl;
package com.baeldung.persistence.dao.impl;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
import org.baeldung.persistence.model.Foo;
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
import com.baeldung.persistence.dao.IFooDao;
import com.baeldung.persistence.model.Foo;
import org.springframework.stereotype.Repository;
@Repository

View File

@ -0,0 +1,19 @@
package com.baeldung.persistence.dao.impl;
import com.baeldung.persistence.dao.IParentDao;
import com.baeldung.persistence.dao.common.AbstractHibernateDao;
import com.baeldung.persistence.model.Parent;
import org.springframework.stereotype.Repository;
@Repository
public class ParentDao extends AbstractHibernateDao<Parent> implements IParentDao {
public ParentDao() {
super();
setClazz(Parent.class);
}
// API
}

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.model;
package com.baeldung.persistence.model;
import java.io.Serializable;
import java.util.Date;

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.model;
package com.baeldung.persistence.model;
import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.model;
package com.baeldung.persistence.model;
import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.model;
package com.baeldung.persistence.model;
import java.io.Serializable;

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.dao.common.IAuditOperations;
import com.baeldung.persistence.model.Bar;
public interface IBarAuditableService extends IBarService, IAuditOperations<Bar> {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.model.Bar;
public interface IBarService extends IOperations<Bar> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.model.Child;
import com.baeldung.persistence.dao.common.IOperations;
public interface IChildService extends IOperations<Child> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.dao.common.IAuditOperations;
import com.baeldung.persistence.model.Foo;
public interface IFooAuditableService extends IFooService, IAuditOperations<Foo> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.dao.common.IOperations;
public interface IFooService extends IOperations<Foo> {
//
}

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.model.Parent;
import com.baeldung.persistence.dao.common.IOperations;
public interface IParentService extends IOperations<Parent> {
//
}

View File

@ -1,30 +1,30 @@
package org.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import org.baeldung.persistence.dao.common.IAuditOperations;
import org.baeldung.persistence.dao.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional(value = "hibernateTransactionManager")
public abstract class AbstractHibernateAuditableService<T extends Serializable> extends AbstractHibernateService<T> implements IOperations<T>, IAuditOperations<T> {
@Override
public List<T> getEntitiesAtRevision(final Number revision) {
return getAuditableDao().getEntitiesAtRevision(revision);
}
@Override
public List<T> getEntitiesModifiedAtRevision(final Number revision) {
return getAuditableDao().getEntitiesModifiedAtRevision(revision);
}
@Override
public List<T> getRevisions() {
return getAuditableDao().getRevisions();
}
abstract protected IAuditOperations<T> getAuditableDao();
}
package com.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import com.baeldung.persistence.dao.common.IAuditOperations;
import com.baeldung.persistence.dao.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional(value = "hibernateTransactionManager")
public abstract class AbstractHibernateAuditableService<T extends Serializable> extends AbstractHibernateService<T> implements IOperations<T>, IAuditOperations<T> {
@Override
public List<T> getEntitiesAtRevision(final Number revision) {
return getAuditableDao().getEntitiesAtRevision(revision);
}
@Override
public List<T> getEntitiesModifiedAtRevision(final Number revision) {
return getAuditableDao().getEntitiesModifiedAtRevision(revision);
}
@Override
public List<T> getRevisions() {
return getAuditableDao().getRevisions();
}
abstract protected IAuditOperations<T> getAuditableDao();
}

View File

@ -1,42 +1,42 @@
package org.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import org.baeldung.persistence.dao.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional(value = "hibernateTransactionManager")
public abstract class AbstractHibernateService<T extends Serializable> extends AbstractService<T> implements IOperations<T> {
@Override
public T findOne(final long id) {
return super.findOne(id);
}
@Override
public List<T> findAll() {
return super.findAll();
}
@Override
public void create(final T entity) {
super.create(entity);
}
@Override
public T update(final T entity) {
return super.update(entity);
}
@Override
public void delete(final T entity) {
super.delete(entity);
}
@Override
public void deleteById(final long entityId) {
super.deleteById(entityId);
}
}
package com.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import com.baeldung.persistence.dao.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional(value = "hibernateTransactionManager")
public abstract class AbstractHibernateService<T extends Serializable> extends AbstractService<T> implements IOperations<T> {
@Override
public T findOne(final long id) {
return super.findOne(id);
}
@Override
public List<T> findAll() {
return super.findAll();
}
@Override
public void create(final T entity) {
super.create(entity);
}
@Override
public T update(final T entity) {
return super.update(entity);
}
@Override
public void delete(final T entity) {
super.delete(entity);
}
@Override
public void deleteById(final long entityId) {
super.deleteById(entityId);
}
}

View File

@ -1,42 +1,42 @@
package org.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import org.baeldung.persistence.dao.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional(value = "jpaTransactionManager")
public abstract class AbstractJpaService<T extends Serializable> extends AbstractService<T> implements IOperations<T> {
@Override
public T findOne(final long id) {
return super.findOne(id);
}
@Override
public List<T> findAll() {
return super.findAll();
}
@Override
public void create(final T entity) {
super.create(entity);
}
@Override
public T update(final T entity) {
return super.update(entity);
}
@Override
public void delete(final T entity) {
super.delete(entity);
}
@Override
public void deleteById(final long entityId) {
super.deleteById(entityId);
}
}
package com.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import com.baeldung.persistence.dao.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional(value = "jpaTransactionManager")
public abstract class AbstractJpaService<T extends Serializable> extends AbstractService<T> implements IOperations<T> {
@Override
public T findOne(final long id) {
return super.findOne(id);
}
@Override
public List<T> findAll() {
return super.findAll();
}
@Override
public void create(final T entity) {
super.create(entity);
}
@Override
public T update(final T entity) {
return super.update(entity);
}
@Override
public void delete(final T entity) {
super.delete(entity);
}
@Override
public void deleteById(final long entityId) {
super.deleteById(entityId);
}
}

View File

@ -1,9 +1,9 @@
package org.baeldung.persistence.service.common;
package com.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import org.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.dao.common.IOperations;
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {

View File

@ -1,46 +1,46 @@
package org.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import org.baeldung.persistence.dao.common.IOperations;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
@Transactional(value = "jpaTransactionManager")
public abstract class AbstractSpringDataJpaService<T extends Serializable> implements IOperations<T> {
@Override
public T findOne(final long id) {
return getDao().findOne(Long.valueOf(id));
}
@Override
public List<T> findAll() {
return Lists.newArrayList(getDao().findAll());
}
@Override
public void create(final T entity) {
getDao().save(entity);
}
@Override
public T update(final T entity) {
return getDao().save(entity);
}
@Override
public void delete(final T entity) {
getDao().delete(entity);
}
@Override
public void deleteById(final long entityId) {
getDao().delete(Long.valueOf(entityId));
}
protected abstract CrudRepository<T, Serializable> getDao();
}
package com.baeldung.persistence.service.common;
import java.io.Serializable;
import java.util.List;
import com.baeldung.persistence.dao.common.IOperations;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
@Transactional(value = "jpaTransactionManager")
public abstract class AbstractSpringDataJpaService<T extends Serializable> implements IOperations<T> {
@Override
public T findOne(final long id) {
return getDao().findOne(Long.valueOf(id));
}
@Override
public List<T> findAll() {
return Lists.newArrayList(getDao().findAll());
}
@Override
public void create(final T entity) {
getDao().save(entity);
}
@Override
public T update(final T entity) {
return getDao().save(entity);
}
@Override
public void delete(final T entity) {
getDao().delete(entity);
}
@Override
public void deleteById(final long entityId) {
getDao().delete(Long.valueOf(entityId));
}
protected abstract CrudRepository<T, Serializable> getDao();
}

View File

@ -1,41 +1,41 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IBarAuditableDao;
import org.baeldung.persistence.dao.IBarDao;
import org.baeldung.persistence.dao.common.IAuditOperations;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.service.IBarAuditableService;
import org.baeldung.persistence.service.common.AbstractHibernateAuditableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class BarAuditableService extends AbstractHibernateAuditableService<Bar> implements IBarAuditableService {
@Autowired
@Qualifier("barHibernateDao")
private IBarDao dao;
@Autowired
@Qualifier("barHibernateAuditableDao")
private IBarAuditableDao auditDao;
public BarAuditableService() {
super();
}
// API
@Override
protected IOperations<Bar> getDao() {
return dao;
}
@Override
protected IAuditOperations<Bar> getAuditableDao() {
return auditDao;
}
}
package com.baeldung.persistence.service.impl;
import com.baeldung.persistence.dao.common.IAuditOperations;
import com.baeldung.persistence.service.common.AbstractHibernateAuditableService;
import com.baeldung.persistence.dao.IBarAuditableDao;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.service.IBarAuditableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class BarAuditableService extends AbstractHibernateAuditableService<Bar> implements IBarAuditableService {
@Autowired
@Qualifier("barHibernateDao")
private IBarDao dao;
@Autowired
@Qualifier("barHibernateAuditableDao")
private IBarAuditableDao auditDao;
public BarAuditableService() {
super();
}
// API
@Override
protected IOperations<Bar> getDao() {
return dao;
}
@Override
protected IAuditOperations<Bar> getAuditableDao() {
return auditDao;
}
}

View File

@ -1,30 +1,30 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IBarDao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.service.IBarService;
import org.baeldung.persistence.service.common.AbstractJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class BarJpaService extends AbstractJpaService<Bar> implements IBarService {
@Autowired
@Qualifier("barJpaDao")
private IBarDao dao;
public BarJpaService() {
super();
}
// API
@Override
protected IOperations<Bar> getDao() {
return dao;
}
package com.baeldung.persistence.service.impl;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.persistence.service.common.AbstractJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class BarJpaService extends AbstractJpaService<Bar> implements IBarService {
@Autowired
@Qualifier("barJpaDao")
private IBarDao dao;
public BarJpaService() {
super();
}
// API
@Override
protected IOperations<Bar> getDao() {
return dao;
}
}

View File

@ -1,30 +1,30 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IBarDao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.service.IBarService;
import org.baeldung.persistence.service.common.AbstractHibernateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class BarService extends AbstractHibernateService<Bar> implements IBarService {
@Autowired
@Qualifier("barHibernateDao")
private IBarDao dao;
public BarService() {
super();
}
// API
@Override
protected IOperations<Bar> getDao() {
return dao;
}
}
package com.baeldung.persistence.service.impl;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.persistence.service.common.AbstractHibernateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class BarService extends AbstractHibernateService<Bar> implements IBarService {
@Autowired
@Qualifier("barHibernateDao")
private IBarDao dao;
public BarService() {
super();
}
// API
@Override
protected IOperations<Bar> getDao() {
return dao;
}
}

View File

@ -1,26 +1,26 @@
package org.baeldung.persistence.service.impl;
import java.io.Serializable;
import org.baeldung.persistence.dao.IBarCrudRepository;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.service.IBarService;
import org.baeldung.persistence.service.common.AbstractSpringDataJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
public class BarSpringDataJpaService extends AbstractSpringDataJpaService<Bar> implements IBarService {
@Autowired
private IBarCrudRepository dao;
public BarSpringDataJpaService() {
super();
}
@Override
protected CrudRepository<Bar, Serializable> getDao() {
return dao;
}
}
package com.baeldung.persistence.service.impl;
import java.io.Serializable;
import com.baeldung.persistence.service.common.AbstractSpringDataJpaService;
import com.baeldung.persistence.dao.IBarCrudRepository;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.service.IBarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
public class BarSpringDataJpaService extends AbstractSpringDataJpaService<Bar> implements IBarService {
@Autowired
private IBarCrudRepository dao;
public BarSpringDataJpaService() {
super();
}
@Override
protected CrudRepository<Bar, Serializable> getDao() {
return dao;
}
}

View File

@ -1,10 +1,10 @@
package org.baeldung.persistence.service.impl;
package com.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IChildDao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Child;
import org.baeldung.persistence.service.IChildService;
import org.baeldung.persistence.service.common.AbstractHibernateService;
import com.baeldung.persistence.model.Child;
import com.baeldung.persistence.service.IChildService;
import com.baeldung.persistence.dao.IChildDao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.service.common.AbstractHibernateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@ -1,41 +1,41 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IFooAuditableDao;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.dao.common.IAuditOperations;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooAuditableService;
import org.baeldung.persistence.service.common.AbstractHibernateAuditableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class FooAuditableService extends AbstractHibernateAuditableService<Foo> implements IFooAuditableService {
@Autowired
@Qualifier("fooHibernateDao")
private IFooDao dao;
@Autowired
@Qualifier("fooHibernateAuditableDao")
private IFooAuditableDao auditDao;
public FooAuditableService() {
super();
}
// API
@Override
protected IOperations<Foo> getDao() {
return dao;
}
@Override
protected IAuditOperations<Foo> getAuditableDao() {
return auditDao;
}
}
package com.baeldung.persistence.service.impl;
import com.baeldung.persistence.dao.common.IAuditOperations;
import com.baeldung.persistence.service.IFooAuditableService;
import com.baeldung.persistence.service.common.AbstractHibernateAuditableService;
import com.baeldung.persistence.dao.IFooAuditableDao;
import com.baeldung.persistence.dao.IFooDao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.model.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class FooAuditableService extends AbstractHibernateAuditableService<Foo> implements IFooAuditableService {
@Autowired
@Qualifier("fooHibernateDao")
private IFooDao dao;
@Autowired
@Qualifier("fooHibernateAuditableDao")
private IFooAuditableDao auditDao;
public FooAuditableService() {
super();
}
// API
@Override
protected IOperations<Foo> getDao() {
return dao;
}
@Override
protected IAuditOperations<Foo> getAuditableDao() {
return auditDao;
}
}

View File

@ -1,10 +1,10 @@
package org.baeldung.persistence.service.impl;
package com.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.baeldung.persistence.service.common.AbstractHibernateService;
import com.baeldung.persistence.dao.IFooDao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IFooService;
import com.baeldung.persistence.service.common.AbstractHibernateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

View File

@ -1,10 +1,10 @@
package org.baeldung.persistence.service.impl;
package com.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IParentDao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Parent;
import org.baeldung.persistence.service.IParentService;
import org.baeldung.persistence.service.common.AbstractHibernateService;
import com.baeldung.persistence.model.Parent;
import com.baeldung.persistence.service.IParentService;
import com.baeldung.persistence.dao.IParentDao;
import com.baeldung.persistence.dao.common.IOperations;
import com.baeldung.persistence.service.common.AbstractHibernateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@ -1,28 +1,28 @@
package org.baeldung.spring;
package com.baeldung.spring;
import java.util.Properties;
import javax.sql.DataSource;
import com.baeldung.persistence.dao.impl.BarDao;
import com.baeldung.persistence.dao.impl.FooDao;
import com.baeldung.persistence.service.IBarAuditableService;
import com.baeldung.persistence.service.IFooAuditableService;
import com.baeldung.persistence.service.IFooService;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.baeldung.persistence.dao.IBarAuditableDao;
import org.baeldung.persistence.dao.IBarDao;
import org.baeldung.persistence.dao.IFooAuditableDao;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.dao.impl.BarAuditableDao;
import org.baeldung.persistence.dao.impl.BarDao;
import org.baeldung.persistence.dao.impl.BarJpaDao;
import org.baeldung.persistence.dao.impl.FooAuditableDao;
import org.baeldung.persistence.dao.impl.FooDao;
import org.baeldung.persistence.service.IBarAuditableService;
import org.baeldung.persistence.service.IBarService;
import org.baeldung.persistence.service.IFooAuditableService;
import org.baeldung.persistence.service.IFooService;
import org.baeldung.persistence.service.impl.BarAuditableService;
import org.baeldung.persistence.service.impl.BarJpaService;
import org.baeldung.persistence.service.impl.BarSpringDataJpaService;
import org.baeldung.persistence.service.impl.FooAuditableService;
import org.baeldung.persistence.service.impl.FooService;
import com.baeldung.persistence.dao.IBarAuditableDao;
import com.baeldung.persistence.dao.IBarDao;
import com.baeldung.persistence.dao.IFooAuditableDao;
import com.baeldung.persistence.dao.IFooDao;
import com.baeldung.persistence.dao.impl.BarAuditableDao;
import com.baeldung.persistence.dao.impl.BarJpaDao;
import com.baeldung.persistence.dao.impl.FooAuditableDao;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.persistence.service.impl.BarAuditableService;
import com.baeldung.persistence.service.impl.BarJpaService;
import com.baeldung.persistence.service.impl.BarSpringDataJpaService;
import com.baeldung.persistence.service.impl.FooAuditableService;
import com.baeldung.persistence.service.impl.FooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -45,10 +45,10 @@ import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "org.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaAuditing
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.persistence" })
@ComponentScan({ "com.baeldung.persistence" })
public class PersistenceConfig {
@Autowired
@ -62,7 +62,7 @@ public class PersistenceConfig {
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
@ -72,7 +72,7 @@ public class PersistenceConfig {
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(restDataSource());
emf.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
emf.setJpaVendorAdapter(vendorAdapter);

View File

@ -1,4 +1,4 @@
package org.baeldung.spring;
package com.baeldung.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ -7,7 +7,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" })
@ImportResource({ "classpath:hibernate4Config.xml" })
public class PersistenceXmlConfig {

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.dao.common.IAuditOperations;
import org.baeldung.persistence.model.Bar;
public interface IBarAuditableDao extends IBarDao, IAuditOperations<Bar> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Bar;
public interface IBarDao extends IOperations<Bar> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Child;
public interface IChildDao extends IOperations<Child> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.dao.common.IAuditOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooAuditableDao extends IFooDao, IAuditOperations<Foo> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooDao extends IOperations<Foo> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Parent;
public interface IParentDao extends IOperations<Parent> {
//
}

View File

@ -1,19 +0,0 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.dao.IChildDao;
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
import org.baeldung.persistence.model.Child;
import org.springframework.stereotype.Repository;
@Repository
public class ChildDao extends AbstractHibernateDao<Child>implements IChildDao {
public ChildDao() {
super();
setClazz(Child.class);
}
// API
}

View File

@ -1,17 +0,0 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.dao.IFooAuditableDao;
import org.baeldung.persistence.dao.common.AbstractHibernateAuditableDao;
import org.baeldung.persistence.model.Foo;
public class FooAuditableDao extends AbstractHibernateAuditableDao<Foo> implements IFooAuditableDao {
public FooAuditableDao() {
super();
setClazz(Foo.class);
}
// API
}

View File

@ -1,19 +0,0 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.dao.IParentDao;
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
import org.baeldung.persistence.model.Parent;
import org.springframework.stereotype.Repository;
@Repository
public class ParentDao extends AbstractHibernateDao<Parent>implements IParentDao {
public ParentDao() {
super();
setClazz(Parent.class);
}
// API
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.common.IAuditOperations;
import org.baeldung.persistence.model.Bar;
public interface IBarAuditableService extends IBarService, IAuditOperations<Bar> {
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Bar;
public interface IBarService extends IOperations<Bar> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Child;
public interface IChildService extends IOperations<Child> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.common.IAuditOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooAuditableService extends IFooService, IAuditOperations<Foo> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooService extends IOperations<Foo> {
//
}

View File

@ -1,8 +0,0 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Parent;
public interface IParentService extends IOperations<Parent> {
//
}

View File

@ -9,7 +9,7 @@
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="org.baeldung.persistence.model"/>
<property name="packagesToScan" value="com.baeldung.persistence.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>

View File

@ -1,10 +1,10 @@
package org.baeldung.persistence;
package com.baeldung.persistence;
import org.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest;
import org.baeldung.persistence.hibernate.FooSortingPersistenceServiceTest;
import org.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest;
import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest;
import org.baeldung.persistence.service.ParentServicePersistenceIntegrationTest;
import com.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest;
import com.baeldung.persistence.hibernate.FooSortingPersistenceServiceTest;
import com.baeldung.persistence.service.FooServicePersistenceIntegrationTest;
import com.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest;
import com.baeldung.persistence.service.ParentServicePersistenceIntegrationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

View File

@ -1,14 +1,14 @@
package org.baeldung.persistence.audit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ // @formatter:off
EnversFooBarAuditTest.class,
JPABarAuditTest.class,
SpringDataJPABarAuditTest.class
}) // @formatter:on
public class AuditTestSuite {
//
package com.baeldung.persistence.audit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ // @formatter:off
EnversFooBarAuditTest.class,
JPABarAuditTest.class,
SpringDataJPABarAuditTest.class
}) // @formatter:on
public class AuditTestSuite {
//
}

View File

@ -1,142 +1,142 @@
package org.baeldung.persistence.audit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IBarAuditableService;
import org.baeldung.persistence.service.IFooAuditableService;
import org.baeldung.spring.PersistenceConfig;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class EnversFooBarAuditTest {
private static Logger logger = LoggerFactory.getLogger(EnversFooBarAuditTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.info("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
logger.info("tearDownAfterClass()");
}
@Autowired
@Qualifier("fooHibernateAuditableService")
private IFooAuditableService fooService;
@Autowired
@Qualifier("barHibernateAuditableService")
private IBarAuditableService barService;
@Autowired
private SessionFactory sessionFactory;
private Session session;
@Before
public void setUp() throws Exception {
logger.info("setUp()");
makeRevisions();
session = sessionFactory.openSession();
}
@After
public void tearDown() throws Exception {
logger.info("tearDown()");
session.close();
}
private void makeRevisions() {
final Bar bar = rev1();
rev2(bar);
rev3(bar);
rev4(bar);
}
// REV #1: insert BAR & FOO1
private Bar rev1() {
final Bar bar = new Bar("BAR");
final Foo foo1 = new Foo("FOO1");
foo1.setBar(bar);
fooService.create(foo1);
return bar;
}
// REV #2: insert FOO2 & update BAR
private void rev2(final Bar bar) {
final Foo foo2 = new Foo("FOO2");
foo2.setBar(bar);
fooService.create(foo2);
}
// REV #3: update BAR
private void rev3(final Bar bar) {
bar.setName("BAR1");
barService.update(bar);
}
// REV #4: insert FOO3 & update BAR
private void rev4(final Bar bar) {
final Foo foo3 = new Foo("FOO3");
foo3.setBar(bar);
fooService.create(foo3);
}
@Test
public final void whenFooBarsModified_thenFooBarsAudited() {
List<Bar> barRevisionList;
List<Foo> fooRevisionList;
// test Bar revisions
barRevisionList = barService.getRevisions();
assertNotNull(barRevisionList);
assertEquals(4, barRevisionList.size());
assertEquals("BAR", barRevisionList.get(0).getName());
assertEquals("BAR", barRevisionList.get(1).getName());
assertEquals("BAR1", barRevisionList.get(2).getName());
assertEquals("BAR1", barRevisionList.get(3).getName());
assertEquals(1, barRevisionList.get(0).getFooSet().size());
assertEquals(2, barRevisionList.get(1).getFooSet().size());
assertEquals(2, barRevisionList.get(2).getFooSet().size());
assertEquals(3, barRevisionList.get(3).getFooSet().size());
// test Foo revisions
fooRevisionList = fooService.getRevisions();
assertNotNull(fooRevisionList);
assertEquals(3, fooRevisionList.size());
assertEquals("FOO1", fooRevisionList.get(0).getName());
assertEquals("FOO2", fooRevisionList.get(1).getName());
assertEquals("FOO3", fooRevisionList.get(2).getName());
}
}
package com.baeldung.persistence.audit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IBarAuditableService;
import com.baeldung.persistence.service.IFooAuditableService;
import com.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Bar;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class EnversFooBarAuditTest {
private static Logger logger = LoggerFactory.getLogger(EnversFooBarAuditTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.info("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
logger.info("tearDownAfterClass()");
}
@Autowired
@Qualifier("fooHibernateAuditableService")
private IFooAuditableService fooService;
@Autowired
@Qualifier("barHibernateAuditableService")
private IBarAuditableService barService;
@Autowired
private SessionFactory sessionFactory;
private Session session;
@Before
public void setUp() throws Exception {
logger.info("setUp()");
makeRevisions();
session = sessionFactory.openSession();
}
@After
public void tearDown() throws Exception {
logger.info("tearDown()");
session.close();
}
private void makeRevisions() {
final Bar bar = rev1();
rev2(bar);
rev3(bar);
rev4(bar);
}
// REV #1: insert BAR & FOO1
private Bar rev1() {
final Bar bar = new Bar("BAR");
final Foo foo1 = new Foo("FOO1");
foo1.setBar(bar);
fooService.create(foo1);
return bar;
}
// REV #2: insert FOO2 & update BAR
private void rev2(final Bar bar) {
final Foo foo2 = new Foo("FOO2");
foo2.setBar(bar);
fooService.create(foo2);
}
// REV #3: update BAR
private void rev3(final Bar bar) {
bar.setName("BAR1");
barService.update(bar);
}
// REV #4: insert FOO3 & update BAR
private void rev4(final Bar bar) {
final Foo foo3 = new Foo("FOO3");
foo3.setBar(bar);
fooService.create(foo3);
}
@Test
public final void whenFooBarsModified_thenFooBarsAudited() {
List<Bar> barRevisionList;
List<Foo> fooRevisionList;
// test Bar revisions
barRevisionList = barService.getRevisions();
assertNotNull(barRevisionList);
assertEquals(4, barRevisionList.size());
assertEquals("BAR", barRevisionList.get(0).getName());
assertEquals("BAR", barRevisionList.get(1).getName());
assertEquals("BAR1", barRevisionList.get(2).getName());
assertEquals("BAR1", barRevisionList.get(3).getName());
assertEquals(1, barRevisionList.get(0).getFooSet().size());
assertEquals(2, barRevisionList.get(1).getFooSet().size());
assertEquals(2, barRevisionList.get(2).getFooSet().size());
assertEquals(3, barRevisionList.get(3).getFooSet().size());
// test Foo revisions
fooRevisionList = fooService.getRevisions();
assertNotNull(fooRevisionList);
assertEquals(3, fooRevisionList.size());
assertEquals("FOO1", fooRevisionList.get(0).getName());
assertEquals("FOO2", fooRevisionList.get(1).getName());
assertEquals("FOO3", fooRevisionList.get(2).getName());
}
}

View File

@ -1,106 +1,106 @@
package org.baeldung.persistence.audit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Bar.OPERATION;
import org.baeldung.persistence.service.IBarService;
import org.baeldung.spring.PersistenceConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class JPABarAuditTest {
private static Logger logger = LoggerFactory.getLogger(JPABarAuditTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.info("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
logger.info("tearDownAfterClass()");
}
@Autowired
@Qualifier("barJpaService")
private IBarService barService;
@Autowired
private EntityManagerFactory entityManagerFactory;
private EntityManager em;
@Before
public void setUp() throws Exception {
logger.info("setUp()");
em = entityManagerFactory.createEntityManager();
}
@After
public void tearDown() throws Exception {
logger.info("tearDown()");
em.close();
}
@Test
public final void whenBarsModified_thenBarsAudited() {
// insert BAR1
Bar bar1 = new Bar("BAR1");
barService.create(bar1);
// update BAR1
bar1.setName("BAR1a");
barService.update(bar1);
// insert BAR2
Bar bar2 = new Bar("BAR2");
barService.create(bar2);
// update BAR1
bar1.setName("BAR1b");
barService.update(bar1);
// get BAR1 and BAR2 from the DB and check the audit values
// detach instances from persistence context to make sure we fire db
em.detach(bar1);
em.detach(bar2);
bar1 = barService.findOne(bar1.getId());
bar2 = barService.findOne(bar2.getId());
assertNotNull(bar1);
assertNotNull(bar2);
assertEquals(OPERATION.UPDATE, bar1.getOperation());
assertEquals(OPERATION.INSERT, bar2.getOperation());
assertTrue(bar1.getTimestamp() > bar2.getTimestamp());
barService.deleteById(bar1.getId());
barService.deleteById(bar2.getId());
}
package com.baeldung.persistence.audit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.model.Bar.OPERATION;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.spring.PersistenceConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class JPABarAuditTest {
private static Logger logger = LoggerFactory.getLogger(JPABarAuditTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.info("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
logger.info("tearDownAfterClass()");
}
@Autowired
@Qualifier("barJpaService")
private IBarService barService;
@Autowired
private EntityManagerFactory entityManagerFactory;
private EntityManager em;
@Before
public void setUp() throws Exception {
logger.info("setUp()");
em = entityManagerFactory.createEntityManager();
}
@After
public void tearDown() throws Exception {
logger.info("tearDown()");
em.close();
}
@Test
public final void whenBarsModified_thenBarsAudited() {
// insert BAR1
Bar bar1 = new Bar("BAR1");
barService.create(bar1);
// update BAR1
bar1.setName("BAR1a");
barService.update(bar1);
// insert BAR2
Bar bar2 = new Bar("BAR2");
barService.create(bar2);
// update BAR1
bar1.setName("BAR1b");
barService.update(bar1);
// get BAR1 and BAR2 from the DB and check the audit values
// detach instances from persistence context to make sure we fire db
em.detach(bar1);
em.detach(bar2);
bar1 = barService.findOne(bar1.getId());
bar2 = barService.findOne(bar2.getId());
assertNotNull(bar1);
assertNotNull(bar2);
assertEquals(OPERATION.UPDATE, bar1.getOperation());
assertEquals(OPERATION.INSERT, bar2.getOperation());
assertTrue(bar1.getTimestamp() > bar2.getTimestamp());
barService.deleteById(bar1.getId());
barService.deleteById(bar2.getId());
}
}

View File

@ -1,76 +1,76 @@
package org.baeldung.persistence.audit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.service.IBarService;
import org.baeldung.spring.PersistenceConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class SpringDataJPABarAuditTest {
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.info("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
logger.info("tearDownAfterClass()");
}
@Autowired
@Qualifier("barSpringDataJpaService")
private IBarService barService;
@Autowired
private EntityManagerFactory entityManagerFactory;
private EntityManager em;
@Before
public void setUp() throws Exception {
logger.info("setUp()");
em = entityManagerFactory.createEntityManager();
}
@After
public void tearDown() throws Exception {
logger.info("tearDown()");
em.close();
}
@Test
@WithMockUser(username = "tutorialuser")
public final void whenBarsModified_thenBarsAudited() {
Bar bar = new Bar("BAR1");
barService.create(bar);
assertEquals(bar.getCreatedDate(), bar.getModifiedDate());
assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy());
bar.setName("BAR2");
bar = barService.update(bar);
assertTrue(bar.getCreatedDate() < bar.getModifiedDate());
assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy());
}
}
package com.baeldung.persistence.audit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.service.IBarService;
import com.baeldung.spring.PersistenceConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class SpringDataJPABarAuditTest {
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.info("setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
logger.info("tearDownAfterClass()");
}
@Autowired
@Qualifier("barSpringDataJpaService")
private IBarService barService;
@Autowired
private EntityManagerFactory entityManagerFactory;
private EntityManager em;
@Before
public void setUp() throws Exception {
logger.info("setUp()");
em = entityManagerFactory.createEntityManager();
}
@After
public void tearDown() throws Exception {
logger.info("tearDown()");
em.close();
}
@Test
@WithMockUser(username = "tutorialuser")
public final void whenBarsModified_thenBarsAudited() {
Bar bar = new Bar("BAR1");
barService.create(bar);
assertEquals(bar.getCreatedDate(), bar.getModifiedDate());
assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy());
bar.setName("BAR2");
bar = barService.update(bar);
assertTrue(bar.getCreatedDate() < bar.getModifiedDate());
assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy());
}
}

View File

@ -1,9 +1,9 @@
package org.baeldung.persistence.hibernate;
package com.baeldung.persistence.hibernate;
import java.util.List;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.model.Bar;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.hibernate;
package com.baeldung.persistence.hibernate;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.hamcrest.Matchers.hasSize;
@ -7,9 +7,9 @@ import static org.junit.Assert.assertThat;
import java.util.List;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IFooService;
import com.baeldung.spring.PersistenceConfig;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.ScrollMode;

View File

@ -1,13 +1,13 @@
package org.baeldung.persistence.hibernate;
package com.baeldung.persistence.hibernate;
import static org.junit.Assert.assertNull;
import java.util.List;
import java.util.Set;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Bar;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig;
import org.hibernate.Criteria;
import org.hibernate.NullPrecedence;
import org.hibernate.Query;

View File

@ -1,10 +1,9 @@
package org.baeldung.persistence.service;
package com.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;

View File

@ -1,9 +1,9 @@
package org.baeldung.persistence.service;
package com.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.persistence.model.Foo;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Foo;
import com.baeldung.spring.PersistenceConfig;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -1,8 +1,8 @@
package org.baeldung.persistence.service;
package com.baeldung.persistence.service;
import org.baeldung.persistence.model.Child;
import org.baeldung.persistence.model.Parent;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Child;
import com.baeldung.persistence.model.Parent;
import com.baeldung.spring.PersistenceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;