further persistence work
This commit is contained in:
@@ -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> {
|
||||
//
|
||||
}
|
||||
@@ -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> {
|
||||
//
|
||||
}
|
||||
-65
@@ -1,65 +0,0 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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);
|
||||
|
||||
}
|
||||
@@ -1,24 +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.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class ChildDao extends AbstractHibernateDao<Child> implements IChildDao {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public ChildDao() {
|
||||
super();
|
||||
|
||||
setClazz(Child.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
@@ -1,24 +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.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class ParentDao extends AbstractHibernateDao<Parent> implements IParentDao {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public ParentDao() {
|
||||
super();
|
||||
|
||||
setClazz(Parent.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class Child implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToOne(mappedBy = "child")
|
||||
private Parent parent;
|
||||
|
||||
public Child() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(final Parent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Child [id=").append(id).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class Parent implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH })
|
||||
@JoinColumn(name = "child_fk")
|
||||
private Child child;
|
||||
|
||||
public Parent() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Parent(final Child child) {
|
||||
super();
|
||||
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Child getChild() {
|
||||
return child;
|
||||
}
|
||||
|
||||
public void setChild(final Child child) {
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Parent [id=").append(id).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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> {
|
||||
//
|
||||
}
|
||||
@@ -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> {
|
||||
//
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package org.baeldung.persistence.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalSessionFactoryBean sessionFactory() {
|
||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||
sessionFactory.setDataSource(restDataSource());
|
||||
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource restDataSource() {
|
||||
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 HibernateTransactionManager transactionManager() {
|
||||
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
||||
txManager.setSessionFactory(sessionFactory().getObject());
|
||||
|
||||
return txManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties hibernateProperties() {
|
||||
return new Properties() {
|
||||
{
|
||||
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
|
||||
// setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
// note: necessary in launchpad-storage, but causing problems here
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user