JAVA-143: Migrate spring-hibernate-3 to com.baeldung

This commit is contained in:
Krzysiek 2020-02-17 20:57:44 +01:00
parent 733402b709
commit 233891c107
26 changed files with 216 additions and 219 deletions

View File

@ -4,8 +4,8 @@ This module contains articles about Spring with Hibernate 3
### Relevant Articles:
- [Hibernate 3 with Spring](http://www.baeldung.com/hibernate3-spring)
- [HibernateException: No Hibernate Session Bound to Thread in Hibernate 3](http://www.baeldung.com/no-hibernate-session-bound-to-thread-exception)
- [Hibernate 3 with Spring](https://www.baeldung.com/hibernate3-spring)
- [HibernateException: No Hibernate Session Bound to Thread in Hibernate 3](https://www.baeldung.com/no-hibernate-session-bound-to-thread-exception)
### Quick Start

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.dao;
package com.baeldung.persistence.dao;
import java.io.Serializable;
import java.util.List;

View File

@ -1,18 +1,18 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.Event;
import org.springframework.stereotype.Repository;
@Repository
public class EventDao extends AbstractHibernateDao<Event> implements IEventDao {
public EventDao() {
super();
setClazz(Event.class);
}
// API
}
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Event;
import org.springframework.stereotype.Repository;
@Repository
public class EventDao extends AbstractHibernateDao<Event> implements IEventDao {
public EventDao() {
super();
setClazz(Event.class);
}
// API
}

View File

@ -1,6 +1,6 @@
package org.baeldung.persistence.dao;
package com.baeldung.persistence.dao;
import org.baeldung.persistence.model.Foo;
import com.baeldung.persistence.model.Foo;
import org.springframework.stereotype.Repository;
@Repository

View File

@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Event;
public interface IEventDao extends IOperations<Event> {
//
}

View File

@ -0,0 +1,7 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Foo;
public interface IFooDao extends IOperations<Foo> {
//
}

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.dao;
package com.baeldung.persistence.dao;
import java.io.Serializable;
import java.util.List;

View File

@ -1,45 +1,45 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EVENTS")
public class Event implements Serializable {
@Id
@GeneratedValue
private Long id;
private String description;
public Event() {
}
public Event(String description) {
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
package com.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EVENTS")
public class Event implements Serializable {
@Id
@GeneratedValue
private Long id;
private String description;
public Event() {
}
public Event(String description) {
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.model;
package com.baeldung.persistence.model;
import java.io.Serializable;

View File

@ -1,27 +1,27 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.IEventDao;
import org.baeldung.persistence.model.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class EventService {
@Autowired
private IEventDao dao;
public EventService() {
super();
}
// API
public void create(final Event entity) {
dao.create(entity);
}
}
package com.baeldung.persistence.service;
import com.baeldung.persistence.model.Event;
import com.baeldung.persistence.dao.IEventDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class EventService {
@Autowired
private IEventDao dao;
public EventService() {
super();
}
// API
public void create(final Event entity) {
dao.create(entity);
}
}

View File

@ -1,7 +1,7 @@
package org.baeldung.persistence.service;
package com.baeldung.persistence.service;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.model.Foo;
import com.baeldung.persistence.dao.IFooDao;
import com.baeldung.persistence.model.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

View File

@ -1,4 +1,4 @@
package org.baeldung.spring;
package com.baeldung.spring;
import java.util.Properties;
@ -22,7 +22,7 @@ import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" })
public class PersistenceConfig {
@Autowired
@ -36,7 +36,7 @@ public class PersistenceConfig {
public AnnotationSessionFactoryBean sessionFactory() {
final AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;

View File

@ -1,4 +1,4 @@
package org.baeldung.spring;
package com.baeldung.spring;
import java.util.Properties;
@ -24,7 +24,7 @@ import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" })
public class PersistenceConfigHibernate3 {
@Autowired

View File

@ -1,4 +1,4 @@
package org.baeldung.spring;
package com.baeldung.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@ -6,7 +6,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
// @Configuration
@EnableTransactionManagement
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" })
@ImportResource({ "classpath:persistenceConfig.xml" })
public class PersistenceXmlConfig {

View File

@ -1,9 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.Event;
public interface IEventDao extends IOperations<Event> {
//
}

View File

@ -1,7 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.Foo;
public interface IFooDao extends IOperations<Foo> {
//
}

View File

@ -4,6 +4,6 @@
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="org.baeldung.persistence.model.Event" />
<mapping class="com.baeldung.persistence.model.Event" />
</session-factory>
</hibernate-configuration>

View File

@ -16,7 +16,7 @@
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan base-package="org.baeldung.persistence" />
<context:component-scan base-package="com.baeldung.persistence" />
<context:property-placeholder location="classpath:persistence-h2.properties"/>

View File

@ -9,7 +9,7 @@
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="org.baeldung.persistence.model"/>
<property name="packagesToScan" value="com.baeldung.persistence.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>

View File

@ -11,7 +11,7 @@
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.baeldung.spring</param-value>
<param-value>com.baeldung.spring</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

View File

@ -1,6 +1,6 @@
package org.baeldung;
package com.baeldung;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.spring.PersistenceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;

View File

@ -1,10 +1,9 @@
package org.baeldung.persistence.service;
package com.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.FooService;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Foo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,7 +1,7 @@
package org.baeldung.persistence.service;
package com.baeldung.persistence.service;
import org.baeldung.persistence.model.Event;
import org.baeldung.spring.PersistenceConfigHibernate3;
import com.baeldung.persistence.model.Event;
import com.baeldung.spring.PersistenceConfigHibernate3;
import org.hamcrest.core.IsInstanceOf;
import org.hibernate.HibernateException;
import org.junit.Ignore;

View File

@ -1,7 +1,7 @@
package org.baeldung.persistence.service;
package com.baeldung.persistence.service;
import org.baeldung.persistence.model.Event;
import org.baeldung.spring.PersistenceConfig;
import com.baeldung.persistence.model.Event;
import com.baeldung.spring.PersistenceConfig;
import org.hamcrest.core.IsInstanceOf;
import org.hibernate.HibernateException;
import org.junit.Ignore;

View File

@ -1,42 +1,42 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.model.Event;
import org.baeldung.spring.PersistenceXmlConfig;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateSystemException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest {
@Autowired
EventService service;
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Test
public final void whenEntityIsCreated_thenNoExceptions() {
service.create(new Event("from Annotation Session Bean Factory"));
}
@Test
@Ignore
public final void whenNoTransBoundToSession_thenException() {
expectedEx.expect(HibernateSystemException.class);
expectedEx.expectMessage("No Hibernate Session bound to thread, "
+ "and configuration does not allow creation of "
+ "non-transactional one here");
service.create(new Event("from Annotation Session Bean Factory"));
}
}
package com.baeldung.persistence.service;
import com.baeldung.persistence.model.Event;
import com.baeldung.spring.PersistenceXmlConfig;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateSystemException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest {
@Autowired
EventService service;
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Test
public final void whenEntityIsCreated_thenNoExceptions() {
service.create(new Event("from Annotation Session Bean Factory"));
}
@Test
@Ignore
public final void whenNoTransBoundToSession_thenException() {
expectedEx.expect(HibernateSystemException.class);
expectedEx.expectMessage("No Hibernate Session bound to thread, "
+ "and configuration does not allow creation of "
+ "non-transactional one here");
service.create(new Event("from Annotation Session Bean Factory"));
}
}

View File

@ -1,39 +1,38 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.model.Event;
import org.hibernate.HibernateException;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateSystemException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" })
public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest {
@Autowired
EventService service;
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Test
public final void whenEntityIsCreated_thenNoExceptions() {
service.create(new Event("from local session bean factory"));
}
@Test
@Ignore
public final void whenNoTransBoundToSession_thenException() {
expectedEx.expect(HibernateException.class);
expectedEx.expectMessage("No Hibernate Session bound to thread, "
+ "and configuration does not allow creation "
+ "of non-transactional one here");
service.create(new Event("from local session bean factory"));
}
}
package com.baeldung.persistence.service;
import com.baeldung.persistence.model.Event;
import org.hibernate.HibernateException;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" })
public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest {
@Autowired
EventService service;
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Test
public final void whenEntityIsCreated_thenNoExceptions() {
service.create(new Event("from local session bean factory"));
}
@Test
@Ignore
public final void whenNoTransBoundToSession_thenException() {
expectedEx.expect(HibernateException.class);
expectedEx.expectMessage("No Hibernate Session bound to thread, "
+ "and configuration does not allow creation "
+ "of non-transactional one here");
service.create(new Event("from local session bean factory"));
}
}