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