Last active
December 17, 2015 00:10
-
-
Save mokies/5519040 to your computer and use it in GitHub Desktop.
hibernate repository
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
package es.moki.app.repository.domain; | |
import javax.persistence.Id; | |
import javax.persistence.MappedSuperclass; | |
import java.io.Serializable; | |
@MappedSuperclass | |
public abstract class BaseEntity<ID extends Serializable> implements IdentifiableEntity<ID> { | |
@Id | |
protected ID id; | |
public ID getId() { | |
return id; | |
} | |
public void setId(ID id) { | |
this.id = id; | |
} | |
} |
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
package es.moki.app.repository.hibernate; | |
import au.com.coles.boston.app.repository.Repository; | |
import au.com.coles.boston.app.repository.domain.IdentifiableEntity; | |
import org.hibernate.Criteria; | |
import org.hibernate.Session; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.criterion.Projections; | |
import org.springframework.transaction.annotation.Transactional; | |
import java.io.Serializable; | |
import java.util.List; | |
public abstract class HibernateRepository<T extends IdentifiableEntity, ID extends Serializable> implements Repository<T, ID> { | |
protected final SessionFactory sessionFactory; | |
private final Class<T> persistentClass; | |
protected HibernateRepository(Class<T> clazz, SessionFactory sessionFactory) { | |
this.persistentClass = clazz; | |
this.sessionFactory = sessionFactory; | |
} | |
Session getSession() { | |
return sessionFactory.getCurrentSession(); | |
} | |
Class<T> getPersistentClass() { | |
return persistentClass; | |
} | |
@SuppressWarnings("unchecked") | |
@Transactional | |
public T findById(final ID id) { | |
return (T) getSession().load(getPersistentClass(), id); | |
} | |
@SuppressWarnings("unchecked") | |
@Transactional | |
public List<T> findAll() { | |
return createCriteria().list(); | |
} | |
@Transactional | |
public long countAll() { | |
return (Long) createCriteria() | |
.setProjection(Projections.rowCount()) | |
.uniqueResult(); | |
} | |
@Transactional | |
public T save(T object) { | |
getSession().save(object); | |
return object; | |
} | |
@Transactional | |
public int deleteAll() { | |
return getSession().createQuery("delete " + getPersistentClass().getName()).executeUpdate(); | |
} | |
protected Criteria createCriteria() { | |
return getSession().createCriteria(getPersistentClass()); | |
} | |
} |
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
package es.moki.app.repository.hibernate; | |
import es.moki.app.repository.Repository; | |
import es.moki.app.repository.domain.IdentifiableEntity; | |
import org.junit.Test; | |
import java.util.List; | |
import static org.hamcrest.CoreMatchers.equalTo; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
public abstract class HibernateRepositoryTestCase<T extends IdentifiableEntity, R extends Repository> extends HibernateTestCase { | |
abstract R getRepository(); | |
abstract T createEntity(); | |
@Test | |
public void shouldFindAll() { | |
int allEntitiesBeforeCount = (int) getRepository().countAll(); | |
T entity = givenAnExistingDomainReferenceInTheDatabase(); | |
beforeHibernateAssert(); | |
List<T> allEntities = getRepository().findAll(); | |
assertThat(allEntitiesBeforeCount + 1, equalTo(allEntities.size())); | |
} | |
private T givenAnExistingDomainReferenceInTheDatabase() { | |
T entity = createEntity(); | |
R repository = getRepository(); | |
repository.save(entity); | |
return entity; | |
} | |
} |
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
package es.moki.app.repository.hibernate; | |
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.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.transaction.annotation.Transactional; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = {"classpath:integration-applicationContext-repository.xml" }) | |
@Transactional | |
public abstract class HibernateTestCase { | |
@Autowired | |
protected SessionFactory sessionFactory; | |
@BeforeClass | |
public static void setupOnce() { | |
} | |
@AfterClass | |
public static void tearDownOnce() { | |
} | |
@Before | |
public void setUp() { | |
} | |
@After | |
public void tearDown() { | |
} | |
protected void beforeHibernateAssert() { | |
Session session = sessionFactory.getCurrentSession(); | |
session.flush(); | |
session.clear(); | |
} | |
} |
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
package es.moki.app.repository.domain; | |
import java.io.Serializable; | |
public interface IdentifiableEntity<ID extends Serializable> { | |
ID getId(); | |
} |
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
package es.moki.app.repository; | |
import au.com.coles.boston.app.repository.domain.IdentifiableEntity; | |
import java.util.List; | |
public interface Repository<T extends IdentifiableEntity, ID> { | |
T findById(ID id); | |
T save(T entity); | |
List<T> findAll(); | |
long countAll(); | |
int deleteAll(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment