service work

This commit is contained in:
Eugen
2013-05-17 12:22:20 +03:00
parent bccc8fc690
commit 852c5b96de
5 changed files with 177 additions and 3 deletions

View File

@@ -1,14 +1,66 @@
package org.baeldung.spring.persistence.dao;
import java.util.List;
import org.baeldung.spring.persistence.model.Foo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.google.common.base.Preconditions;
@Repository
public class FooDao implements IFooDao {
@Autowired
private SessionFactory sessionFactory;
public FooDao() {
super();
}
// API
@Override
public Foo findOne(final Long id) {
Preconditions.checkArgument(id != null);
return (Foo) getCurrentSession().get(Foo.class, id);
}
@Override
@SuppressWarnings("unchecked")
public List<Foo> findAll() {
return getCurrentSession().createQuery("from " + Foo.class.getName()).list();
}
@Override
public void create(final Foo entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().persist(entity);
}
@Override
public void update(final Foo entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().merge(entity);
}
@Override
public void delete(final Foo entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().delete(entity);
}
@Override
public void deleteById(final Long entityId) {
final Foo entity = findOne(entityId);
Preconditions.checkState(entity != null);
delete(entity);
}
protected final Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}

View File

@@ -1,5 +1,21 @@
package org.baeldung.spring.persistence.dao;
import java.util.List;
import org.baeldung.spring.persistence.model.Foo;
public interface IFooDao {
//
Foo findOne(Long id);
List<Foo> findAll();
void create(Foo entity);
void update(Foo entity);
void delete(Foo entity);
void deleteById(Long entityId);
}

View File

@@ -1,6 +1,7 @@
package org.baeldung.spring.persistence.service;
import org.baeldung.spring.persistence.dao.IFooDao;
import org.baeldung.spring.persistence.model.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -10,7 +11,7 @@ import org.springframework.transaction.annotation.Transactional;
public class FooService {
@Autowired
private IFooDao fooDao;
private IFooDao dao;
public FooService() {
super();
@@ -18,4 +19,8 @@ public class FooService {
// API
public void create(final Foo entity) {
dao.create(entity);
}
}

View File

@@ -1,7 +1,10 @@
package org.baeldung.spring.persistence.service;
import org.baeldung.spring.persistence.config.PersistenceConfig;
import org.baeldung.spring.persistence.model.Foo;
import org.junit.Test;
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.test.context.support.AnnotationConfigContextLoader;
@@ -9,5 +12,20 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class FooServicePersistenceIntegrationTest {
//
@Autowired
private FooService service;
// tests
@Test
public final void whenContextIsBootstrapped_thenNoExceptions() {
//
}
@Test
public final void whenEntityisCreated_thenNoExceptions() {
service.create(new Foo());
}
}