jpa work
This commit is contained in:
parent
676bc5d160
commit
dbd5616f5a
@ -34,6 +34,11 @@
|
|||||||
<artifactId>hibernate-core</artifactId>
|
<artifactId>hibernate-core</artifactId>
|
||||||
<version>4.2.1.Final</version>
|
<version>4.2.1.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-entitymanager</artifactId>
|
||||||
|
<version>4.2.1.Final</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.javassist</groupId>
|
<groupId>org.javassist</groupId>
|
||||||
<artifactId>javassist</artifactId>
|
<artifactId>javassist</artifactId>
|
||||||
|
@ -12,8 +12,11 @@ import org.springframework.context.annotation.PropertySource;
|
|||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
@ -32,13 +35,20 @@ public class PersistenceConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LocalSessionFactoryBean sessionFactory() {
|
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
|
||||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
|
||||||
sessionFactory.setDataSource(restDataSource());
|
factoryBean.setDataSource(restDataSource());
|
||||||
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.spring.persistence.model" });
|
factoryBean.setPackagesToScan(new String[] { "org.baeldung.spring.persistence.model" });
|
||||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
|
||||||
|
|
||||||
return sessionFactory;
|
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter() {
|
||||||
|
{
|
||||||
|
// JPA properties ...
|
||||||
|
}
|
||||||
|
};
|
||||||
|
factoryBean.setJpaVendorAdapter(vendorAdapter);
|
||||||
|
factoryBean.setJpaProperties(additionalProperties());
|
||||||
|
|
||||||
|
return factoryBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -53,11 +63,11 @@ public class PersistenceConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public HibernateTransactionManager transactionManager() {
|
public PlatformTransactionManager transactionManager() {
|
||||||
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
txManager.setSessionFactory(sessionFactory().getObject());
|
transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
|
||||||
|
|
||||||
return txManager;
|
return transactionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -65,7 +75,7 @@ public class PersistenceConfig {
|
|||||||
return new PersistenceExceptionTranslationPostProcessor();
|
return new PersistenceExceptionTranslationPostProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
final Properties hibernateProperties() {
|
final Properties additionalProperties() {
|
||||||
return new Properties() {
|
return new Properties() {
|
||||||
{
|
{
|
||||||
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
@ -76,4 +86,5 @@ public class PersistenceConfig {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -2,10 +2,10 @@ package org.baeldung.spring.persistence.dao;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
import org.baeldung.spring.persistence.model.Foo;
|
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 org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
@ -13,8 +13,8 @@ import com.google.common.base.Preconditions;
|
|||||||
@Repository
|
@Repository
|
||||||
public class FooDao implements IFooDao {
|
public class FooDao implements IFooDao {
|
||||||
|
|
||||||
@Autowired
|
@PersistenceContext
|
||||||
private SessionFactory sessionFactory;
|
private EntityManager entityManager;
|
||||||
|
|
||||||
public FooDao() {
|
public FooDao() {
|
||||||
super();
|
super();
|
||||||
@ -23,44 +23,38 @@ public class FooDao implements IFooDao {
|
|||||||
// API
|
// API
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Foo findOne(final Long id) {
|
public Foo findOne(final long id) {
|
||||||
Preconditions.checkArgument(id != null);
|
return entityManager.find(Foo.class, id);
|
||||||
return (Foo) getCurrentSession().get(Foo.class, id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Foo> findAll() {
|
public List<Foo> findAll() {
|
||||||
return getCurrentSession().createQuery("from " + Foo.class.getName()).list();
|
return entityManager.createQuery("from " + Foo.class.getName()).getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create(final Foo entity) {
|
public void create(final Foo entity) {
|
||||||
Preconditions.checkNotNull(entity);
|
Preconditions.checkNotNull(entity);
|
||||||
getCurrentSession().persist(entity);
|
entityManager.persist(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(final Foo entity) {
|
public void update(final Foo entity) {
|
||||||
Preconditions.checkNotNull(entity);
|
Preconditions.checkNotNull(entity);
|
||||||
getCurrentSession().merge(entity);
|
entityManager.merge(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(final Foo entity) {
|
public void delete(final Foo entity) {
|
||||||
Preconditions.checkNotNull(entity);
|
Preconditions.checkNotNull(entity);
|
||||||
getCurrentSession().delete(entity);
|
entityManager.remove(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteById(final Long entityId) {
|
public void deleteById(final long entityId) {
|
||||||
final Foo entity = findOne(entityId);
|
final Foo entity = findOne(entityId);
|
||||||
Preconditions.checkState(entity != null);
|
|
||||||
delete(entity);
|
delete(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final Session getCurrentSession() {
|
|
||||||
return sessionFactory.getCurrentSession();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import org.baeldung.spring.persistence.model.Foo;
|
|||||||
|
|
||||||
public interface IFooDao {
|
public interface IFooDao {
|
||||||
|
|
||||||
Foo findOne(Long id);
|
Foo findOne(long id);
|
||||||
|
|
||||||
List<Foo> findAll();
|
List<Foo> findAll();
|
||||||
|
|
||||||
@ -16,6 +16,6 @@ public interface IFooDao {
|
|||||||
|
|
||||||
void delete(Foo entity);
|
void delete(Foo entity);
|
||||||
|
|
||||||
void deleteById(Long entityId);
|
void deleteById(long entityId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import javax.persistence.Entity;
|
|||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Foo {
|
public class Foo {
|
||||||
@ -15,7 +14,6 @@ public class Foo {
|
|||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@NotNull
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public Foo() {
|
public Foo() {
|
||||||
|
@ -4,6 +4,7 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
|||||||
|
|
||||||
import org.baeldung.spring.persistence.config.PersistenceConfig;
|
import org.baeldung.spring.persistence.config.PersistenceConfig;
|
||||||
import org.baeldung.spring.persistence.model.Foo;
|
import org.baeldung.spring.persistence.model.Foo;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -50,6 +51,7 @@ public class FooServicePersistenceIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Ignore
|
||||||
public final void temp_whenInvalidEntityIsCreated_thenDataException() {
|
public final void temp_whenInvalidEntityIsCreated_thenDataException() {
|
||||||
service.create(new Foo(randomAlphabetic(2048)));
|
service.create(new Foo(randomAlphabetic(2048)));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user