rest work

This commit is contained in:
Eugen 2013-06-03 18:22:54 +03:00
parent 3201a925ca
commit 6f8e29db09
12 changed files with 280 additions and 36 deletions

View File

@ -0,0 +1,8 @@
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

@ -0,0 +1,65 @@
package org.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> implements IOperations<T> {
private Class<T> clazz;
@Autowired
private SessionFactory sessionFactory;
// API
protected final void setClazz(final Class<T> clazzToSet) {
clazz = Preconditions.checkNotNull(clazzToSet);
}
@Override
public final T findOne(final long id) {
return (T) getCurrentSession().get(clazz, id);
}
@Override
public final List<T> findAll() {
return getCurrentSession().createQuery("from " + clazz.getName()).list();
}
@Override
public final void create(final T entity) {
Preconditions.checkNotNull(entity);
// getCurrentSession().persist(entity);
getCurrentSession().saveOrUpdate(entity);
}
@Override
public final T update(final T entity) {
Preconditions.checkNotNull(entity);
return (T) getCurrentSession().merge(entity);
}
@Override
public final void delete(final T entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().delete(entity);
}
@Override
public final void deleteById(final long entityId) {
final T entity = findOne(entityId);
Preconditions.checkState(entity != null);
delete(entity);
}
protected final Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}

View File

@ -0,0 +1,13 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
@Repository
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class GenericHibernateDao<T extends Serializable> extends AbstractHibernateDao<T> implements IGenericDao<T> {
//
}

View File

@ -0,0 +1,7 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
public interface IGenericDao<T extends Serializable> extends IOperations<T> {
//
}

View File

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

View File

@ -0,0 +1,83 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
public Foo() {
super();
}
public Foo(final String name) {
super();
this.name = name;
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
//
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
}

View File

@ -1,23 +0,0 @@
package org.baeldung.persistence.service;
import org.baeldung.web.dto.Foo;
import org.springframework.stereotype.Service;
@Service
public class FooService {
public FooService() {
super();
}
// API
public Foo getById(final Long id) {
return null;
}
public Long create(final Foo resource) {
return null;
}
}

View File

@ -0,0 +1,8 @@
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

@ -0,0 +1,44 @@
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
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
@Override
public T findOne(final long id) {
return getDao().findOne(id);
}
@Override
public List<T> findAll() {
return getDao().findAll();
}
@Override
public void create(final T entity) {
getDao().create(entity);
}
@Override
public T update(final T entity) {
return getDao().update(entity);
}
@Override
public void delete(final T entity) {
getDao().delete(entity);
}
@Override
public void deleteById(final long entityId) {
getDao().deleteById(entityId);
}
protected abstract IOperations<T> getDao();
}

View File

@ -0,0 +1,30 @@
package org.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.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class FooService extends AbstractService<Foo> implements IFooService {
@Autowired
private IFooDao dao;
public FooService() {
super();
}
// API
@Override
protected IOperations<Foo> getDao() {
return dao;
}
}

View File

@ -5,8 +5,8 @@ import java.net.URI;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.baeldung.persistence.service.FooService;
import org.baeldung.web.dto.Foo;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.impl.FooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;

View File

@ -1,11 +0,0 @@
package org.baeldung.web.dto;
import java.io.Serializable;
public class Foo implements Serializable {
public Foo() {
super();
}
}