Merge branch 'master' into JAVA-4
This commit is contained in:
@@ -5,7 +5,7 @@ This module contains articles about the Java Persistence API (JPA) in Java.
|
||||
### Relevant Articles
|
||||
|
||||
- [JPA Query Parameters Usage](https://www.baeldung.com/jpa-query-parameters)
|
||||
- [Mapping Entitiy Class Names to SQL Table Names with JPA](https://www.baeldung.com/jpa-entity-table-names)
|
||||
- [Mapping Entity Class Names to SQL Table Names with JPA](https://www.baeldung.com/jpa-entity-table-names)
|
||||
- [Default Column Values in JPA](https://www.baeldung.com/jpa-default-column-values)
|
||||
- [Types of JPA Queries](https://www.baeldung.com/jpa-queries)
|
||||
- [JPA/Hibernate Projections](https://www.baeldung.com/jpa-hibernate-projections)
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ server.port=8082
|
||||
|
||||
#spring boot mongodb
|
||||
spring.data.mongodb.host=localhost
|
||||
spring.data.mongodb.port=27017
|
||||
spring.data.mongodb.port=0
|
||||
spring.data.mongodb.database=springboot-mongo
|
||||
|
||||
spring.thymeleaf.cache=false
|
||||
|
||||
@@ -23,6 +23,16 @@
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.storedprocedure;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class StoredProcedureApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(StoredProcedureApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.storedprocedure.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.storedprocedure.entity.Car;
|
||||
import com.baeldung.storedprocedure.service.CarService;
|
||||
|
||||
@RestController
|
||||
public class CarController {
|
||||
@Autowired
|
||||
private CarService carService;
|
||||
|
||||
@GetMapping(path = "/modelcount")
|
||||
public long getTotalCarsByModel(@RequestParam("model") String model) {
|
||||
return carService.getTotalCarsByModel(model);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/modelcountP")
|
||||
public long getTotalCarsByModelProcedureName(@RequestParam("model") String model) {
|
||||
return carService.getTotalCarsByModelProcedureName(model);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/modelcountV")
|
||||
public long getTotalCarsByModelVaue(@RequestParam("model") String model) {
|
||||
return carService.getTotalCarsByModelValue(model);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/modelcountEx")
|
||||
public long getTotalCarsByModelExplicit(@RequestParam("model") String model) {
|
||||
return carService.getTotalCarsByModelExplicit(model);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/modelcountEn")
|
||||
public long getTotalCarsByModelEntity(@RequestParam("model") String model) {
|
||||
return carService.getTotalCarsByModelEntity(model);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/carsafteryear")
|
||||
public List<Car> findCarsAfterYear(@RequestParam("year") Integer year) {
|
||||
return carService.findCarsAfterYear(year);
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.storedprocedure.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedStoredProcedureQuery;
|
||||
import javax.persistence.StoredProcedureParameter;
|
||||
import javax.persistence.ParameterMode;
|
||||
|
||||
@Entity
|
||||
@NamedStoredProcedureQuery(name = "Car.getTotalCardsbyModelEntity", procedureName = "GET_TOTAL_CARS_BY_MODEL", parameters = {
|
||||
@StoredProcedureParameter(mode = ParameterMode.IN, name = "model_in", type = String.class),
|
||||
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "count_out", type = Integer.class) })
|
||||
|
||||
public class Car {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column
|
||||
private long id;
|
||||
|
||||
@Column
|
||||
private String model;
|
||||
|
||||
@Column
|
||||
private Integer year;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public Integer getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.storedprocedure.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.jpa.repository.query.Procedure;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.storedprocedure.entity.Car;
|
||||
|
||||
@Repository
|
||||
public interface CarRepository extends JpaRepository<Car, Integer> {
|
||||
|
||||
@Procedure
|
||||
int GET_TOTAL_CARS_BY_MODEL(String model);
|
||||
|
||||
@Procedure("GET_TOTAL_CARS_BY_MODEL")
|
||||
int getTotalCarsByModel(String model);
|
||||
|
||||
@Procedure(procedureName = "GET_TOTAL_CARS_BY_MODEL")
|
||||
int getTotalCarsByModelProcedureName(String model);
|
||||
|
||||
@Procedure(value = "GET_TOTAL_CARS_BY_MODEL")
|
||||
int getTotalCarsByModelValue(String model);
|
||||
|
||||
@Procedure(name = "Car.getTotalCardsbyModelEntity")
|
||||
int getTotalCarsByModelEntiy(@Param("model_in") String model);
|
||||
|
||||
@Query(value = "CALL FIND_CARS_AFTER_YEAR(:year_in);", nativeQuery = true)
|
||||
List<Car> findCarsAfterYear(@Param("year_in") Integer year_in);
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.storedprocedure.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.storedprocedure.entity.Car;
|
||||
import com.baeldung.storedprocedure.repository.CarRepository;
|
||||
|
||||
@Service
|
||||
public class CarService {
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
public int getTotalCarsByModel(String model) {
|
||||
return carRepository.getTotalCarsByModel(model);
|
||||
}
|
||||
|
||||
public int getTotalCarsByModelProcedureName(String model) {
|
||||
return carRepository.getTotalCarsByModelProcedureName(model);
|
||||
}
|
||||
|
||||
public int getTotalCarsByModelValue(String model) {
|
||||
return carRepository.getTotalCarsByModelValue(model);
|
||||
}
|
||||
|
||||
public int getTotalCarsByModelExplicit(String model) {
|
||||
return carRepository.GET_TOTAL_CARS_BY_MODEL(model);
|
||||
}
|
||||
|
||||
public int getTotalCarsByModelEntity(String model) {
|
||||
return carRepository.getTotalCarsByModelEntiy(model);
|
||||
}
|
||||
|
||||
public List<Car> findCarsAfterYear(Integer year) {
|
||||
return carRepository.findCarsAfterYear(year);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,5 @@
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.show-sql=true
|
||||
#MySql
|
||||
#spring.datasource.url=jdbc:mysql://localhost:3306/baeldung
|
||||
#spring.datasource.username=baeldung
|
||||
#spring.datasource.password=baeldung
|
||||
@@ -0,0 +1,27 @@
|
||||
DROP TABLE IF EXISTS car;
|
||||
|
||||
CREATE TABLE car (id int(10) NOT NULL AUTO_INCREMENT,
|
||||
model varchar(50) NOT NULL,
|
||||
year int(4) NOT NULL,
|
||||
PRIMARY KEY (id));
|
||||
|
||||
INSERT INTO car (model, year) VALUES ('BMW', 2000);
|
||||
INSERT INTO car (model, year) VALUES ('BENZ', 2010);
|
||||
INSERT INTO car (model, year) VALUES ('PORCHE', 2005);
|
||||
INSERT INTO car (model, year) VALUES ('PORCHE', 2004);
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
DROP PROCEDURE IF EXISTS FIND_CARS_AFTER_YEAR$$
|
||||
CREATE PROCEDURE FIND_CARS_AFTER_YEAR(IN year_in INT)
|
||||
BEGIN
|
||||
SELECT * FROM car WHERE year >= year_in ORDER BY year;
|
||||
END$$
|
||||
|
||||
DROP PROCEDURE IF EXISTS GET_TOTAL_CARS_BY_MODEL$$
|
||||
CREATE PROCEDURE GET_TOTAL_CARS_BY_MODEL(IN model_in VARCHAR(50), OUT count_out INT)
|
||||
BEGIN
|
||||
SELECT COUNT(*) into count_out from car WHERE model = model_in;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
+17
-3
@@ -1,13 +1,11 @@
|
||||
package com.baeldung.spring.data.redis.config;
|
||||
|
||||
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
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.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
@@ -15,6 +13,10 @@ import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
|
||||
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
||||
|
||||
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.spring.data.redis")
|
||||
@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo")
|
||||
@@ -33,6 +35,18 @@ public class RedisConfig {
|
||||
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LettuceConnectionFactory lettuceConnectionFactory() {
|
||||
return new LettuceConnectionFactory();
|
||||
}
|
||||
|
||||
@Bean(name = "flushRedisTemplate")
|
||||
public RedisTemplate<String, String> flushRedisTemplate() {
|
||||
RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(lettuceConnectionFactory());
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
MessageListenerAdapter messageListener() {
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.spring.data.redis.delete;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { RedisConfig.class })
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class RedisFlushDatabaseIntegrationTest {
|
||||
|
||||
private RedisServer redisServer;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("flushRedisTemplate")
|
||||
private RedisTemplate<String, String> flushRedisTemplate;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
redisServer = new RedisServer(6390);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFlushDB_thenAllKeysInDatabaseAreCleared() {
|
||||
|
||||
ValueOperations<String, String> simpleValues = flushRedisTemplate.opsForValue();
|
||||
String key = "key";
|
||||
String value = "value";
|
||||
simpleValues.set(key, value);
|
||||
assertThat(simpleValues.get(key)).isEqualTo(value);
|
||||
|
||||
flushRedisTemplate.execute(new RedisCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
connection.flushDb();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(simpleValues.get(key)).isNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFlushAll_thenAllKeysInDatabasesAreCleared() {
|
||||
|
||||
ValueOperations<String, String> simpleValues = flushRedisTemplate.opsForValue();
|
||||
String key = "key";
|
||||
String value = "value";
|
||||
simpleValues.set(key, value);
|
||||
assertThat(simpleValues.get(key)).isEqualTo(value);
|
||||
|
||||
flushRedisTemplate.execute(new RedisCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
connection.flushAll();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(simpleValues.get(key)).isNull();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
+18
-18
@@ -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
|
||||
|
||||
}
|
||||
+2
-2
@@ -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
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Event;
|
||||
|
||||
|
||||
public interface IEventDao extends IOperations<Event> {
|
||||
//
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooDao extends IOperations<Foo> {
|
||||
//
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
+44
-44
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
+27
-27
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+3
-3
@@ -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;
|
||||
+3
-3
@@ -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;
|
||||
+2
-2
@@ -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
|
||||
+2
-2
@@ -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 {
|
||||
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
|
||||
|
||||
|
||||
public interface IEventDao extends IOperations<Event> {
|
||||
//
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooDao extends IOperations<Foo> {
|
||||
//
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
+1
-1
@@ -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"/>
|
||||
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
+2
-2
@@ -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;
|
||||
+3
-4
@@ -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;
|
||||
+3
-3
@@ -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;
|
||||
+3
-3
@@ -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;
|
||||
+42
-42
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
+38
-39
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,12 @@ This module contains articles about Hibernate 5 with Spring.
|
||||
|
||||
### Relevant articles
|
||||
|
||||
- [Hibernate Many to Many Annotation Tutorial](http://www.baeldung.com/hibernate-many-to-many)
|
||||
- [Programmatic Transactions in the Spring TestContext Framework](http://www.baeldung.com/spring-test-programmatic-transactions)
|
||||
- [JPA Criteria Queries](http://www.baeldung.com/hibernate-criteria-queries)
|
||||
- [Introduction to Hibernate Search](http://www.baeldung.com/hibernate-search)
|
||||
- [Hibernate Many to Many Annotation Tutorial](https://www.baeldung.com/hibernate-many-to-many)
|
||||
- [Programmatic Transactions in the Spring TestContext Framework](https://www.baeldung.com/spring-test-programmatic-transactions)
|
||||
- [JPA Criteria Queries](https://www.baeldung.com/hibernate-criteria-queries)
|
||||
- [Introduction to Hibernate Search](https://www.baeldung.com/hibernate-search)
|
||||
- [@DynamicUpdate with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-dynamicupdate)
|
||||
- [Hibernate Second-Level Cache](http://www.baeldung.com/hibernate-second-level-cache)
|
||||
- [Deleting Objects with Hibernate](http://www.baeldung.com/delete-with-hibernate)
|
||||
- [Spring, Hibernate and a JNDI Datasource](http://www.baeldung.com/spring-persistence-jpa-jndi-datasource)
|
||||
- [@Immutable in Hibernate](http://www.baeldung.com/hibernate-immutable)
|
||||
- [@Immutable in Hibernate](http://www.baeldung.com/hibernate-immutable)
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -3,13 +3,13 @@
|
||||
This module contains articles about Spring with Hibernate 4
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to Hibernate 4 with Spring](http://www.baeldung.com/hibernate-4-spring)
|
||||
- [Hibernate Pagination](http://www.baeldung.com/hibernate-pagination)
|
||||
- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort)
|
||||
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||
- [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
|
||||
- [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading)
|
||||
- [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||
- [Guide to Hibernate 4 with Spring](https://www.baeldung.com/hibernate-4-spring)
|
||||
- [Hibernate Pagination](https://www.baeldung.com/hibernate-pagination)
|
||||
- [Sorting with Hibernate](https://www.baeldung.com/hibernate-sort)
|
||||
- [Stored Procedures with Hibernate](https://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||
- [Hibernate: save, persist, update, merge, saveOrUpdate](https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
|
||||
- [Eager/Lazy Loading In Hibernate](https://www.baeldung.com/hibernate-lazy-eager-loading)
|
||||
- [The DAO with Spring and Hibernate](https://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||
- [Auditing with JPA, Hibernate, and Spring Data JPA](https://www.baeldung.com/database-auditing-jpa)
|
||||
|
||||
### Quick Start
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -4,13 +4,13 @@
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
- [The DAO with JPA and Spring](http://www.baeldung.com/spring-dao-jpa)
|
||||
- [JPA Pagination](http://www.baeldung.com/jpa-pagination)
|
||||
- [Sorting with JPA](http://www.baeldung.com/jpa-sort)
|
||||
- [Self-Contained Testing Using an In-Memory Database](http://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](http://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [Obtaining Auto-generated Keys in Spring JDBC](http://www.baeldung.com/spring-jdbc-autogenerated-keys)
|
||||
- [Transactions with Spring 4 and JPA](http://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [The DAO with JPA and Spring](https://www.baeldung.com/spring-dao-jpa)
|
||||
- [JPA Pagination](https://www.baeldung.com/jpa-pagination)
|
||||
- [Sorting with JPA](https://www.baeldung.com/jpa-sort)
|
||||
- [Self-Contained Testing Using an In-Memory Database](https://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](https://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [Obtaining Auto-generated Keys in Spring JDBC](https://www.baeldung.com/spring-jdbc-autogenerated-keys)
|
||||
- [Transactions with Spring 4 and JPA](https://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
|
||||
- [Many-To-Many Relationship in JPA](https://www.baeldung.com/jpa-many-to-many)
|
||||
- [Spring Persistence (Hibernate and JPA) with a JNDI datasource](https://www.baeldung.com/spring-persistence-hibernate-and-jpa-with-a-jndi-datasource/)
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.annotations;
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.annotations;
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@@ -8,7 +8,7 @@ import javax.persistence.NamedStoredProcedureQuery;
|
||||
import javax.persistence.ParameterMode;
|
||||
import javax.persistence.StoredProcedureParameter;
|
||||
|
||||
import org.baeldung.persistence.multiple.model.user.User;
|
||||
import com.baeldung.persistence.multiple.model.user.User;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.Id;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.annotations;
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import javax.persistence.LockModeType;
|
||||
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -25,8 +25,8 @@ import com.google.common.base.Preconditions;
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
||||
@ComponentScan({ "com.baeldung.persistence" })
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.persistence.dao")
|
||||
public class PersistenceJPAConfig {
|
||||
|
||||
@Autowired
|
||||
@@ -42,7 +42,7 @@ public class PersistenceJPAConfig {
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
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" })
|
||||
@ComponentScan({ "com.baeldung.persistence" })
|
||||
@ImportResource({ "classpath:jpaConfig.xml" })
|
||||
public class PersistenceJPAConfigXml {
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@@ -10,7 +10,7 @@ import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@ComponentScan({ "org.baeldung.web" })
|
||||
@ComponentScan({ "com.baeldung.web" })
|
||||
public class SpringWebConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Bean
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -18,7 +18,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.inmemory.persistence.dao")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.inmemory.persistence.dao")
|
||||
@PropertySource("persistence-student.properties")
|
||||
@EnableTransactionManagement
|
||||
public class StudentJpaConfig {
|
||||
@@ -41,7 +41,7 @@ public class StudentJpaConfig {
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" });
|
||||
em.setPackagesToScan(new String[] { "com.baeldung.inmemory.persistence.model" });
|
||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
em.setJpaProperties(additionalProperties());
|
||||
return em;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
public enum ClientDatabase {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
/**
|
||||
* Service layer code for datasource routing example. Here, the service methods are responsible
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.inmemory.persistence.dao;
|
||||
package com.baeldung.inmemory.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import com.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.inmemory.persistence.dao;
|
||||
package com.baeldung.inmemory.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import com.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ManyTagRepository extends JpaRepository<ManyTag, Long> {
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.inmemory.persistence.dao;
|
||||
package com.baeldung.inmemory.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.inmemory.persistence.model.Student;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
|
||||
public interface BookRepositoryCustom {
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -10,7 +10,7 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
+4
-4
@@ -1,12 +1,12 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import static org.baeldung.persistence.dao.BookSpecifications.hasAuthor;
|
||||
import static org.baeldung.persistence.dao.BookSpecifications.titleContains;
|
||||
import static com.baeldung.persistence.dao.BookSpecifications.hasAuthor;
|
||||
import static com.baeldung.persistence.dao.BookSpecifications.titleContains;
|
||||
import static org.springframework.data.jpa.domain.Specifications.where;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
public class BookSpecifications {
|
||||
+2
-2
@@ -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
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooDao {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.multiple.model.user;
|
||||
package com.baeldung.persistence.multiple.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.multiple.model.user;
|
||||
package com.baeldung.persistence.multiple.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sqlfiles;
|
||||
package com.baeldung.sqlfiles;
|
||||
|
||||
import static javax.persistence.GenerationType.IDENTITY;
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.web;
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.baeldung.persistence.service.FooService;
|
||||
import com.baeldung.persistence.service.FooService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="packagesToScan" value="org.baeldung.persistence.model"/>
|
||||
<property name="packagesToScan" value="com.baeldung.persistence.model"/>
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
|
||||
<!-- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> <property name="generateDdl" value="${jpa.generateDdl}" /> <property name="databasePlatform"
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
|
||||
<persistence-unit name="punit">
|
||||
<class>org.baeldung.persistence.model.Foo</class>
|
||||
<class>org.baeldung.persistence.model.Bar</class>
|
||||
<class>com.baeldung.persistence.model.Foo</class>
|
||||
<class>com.baeldung.persistence.model.Bar</class>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
+10
-10
@@ -1,14 +1,14 @@
|
||||
package org.baeldung.persistence.repository;
|
||||
package com.baeldung.persistence.repository;
|
||||
|
||||
import org.baeldung.config.StudentJpaConfig;
|
||||
import org.baeldung.inmemory.persistence.dao.ManyStudentRepository;
|
||||
import org.baeldung.inmemory.persistence.dao.ManyTagRepository;
|
||||
import org.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import org.baeldung.inmemory.persistence.model.KVTag;
|
||||
import org.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import org.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import org.baeldung.inmemory.persistence.model.SkillTag;
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.config.StudentJpaConfig;
|
||||
import com.baeldung.inmemory.persistence.dao.ManyStudentRepository;
|
||||
import com.baeldung.inmemory.persistence.dao.ManyTagRepository;
|
||||
import com.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import com.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import com.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import com.baeldung.inmemory.persistence.model.SkillTag;
|
||||
import com.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.inmemory.persistence.model.KVTag;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
package org.baeldung.persistence.repository;
|
||||
package com.baeldung.persistence.repository;
|
||||
|
||||
import org.baeldung.config.StudentJpaConfig;
|
||||
import org.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.config.StudentJpaConfig;
|
||||
import com.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import com.baeldung.inmemory.persistence.model.Student;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
@@ -15,8 +15,8 @@ import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -10,9 +10,9 @@ import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.junit.Assert.assertNull;
|
||||
@@ -9,8 +9,8 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
@@ -6,7 +6,7 @@
|
||||
### Relevant Articles:
|
||||
- [A Guide to JPA with Spring](https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa)
|
||||
- [Bootstrapping Hibernate 5 with Spring](http://www.baeldung.com/hibernate-5-spring)
|
||||
- [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||
- [The DAO with Spring and Hibernate](https://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||
- [Simplify the DAO with Spring and Java Generics](https://www.baeldung.com/simplifying-the-data-access-layer-with-spring-and-java-generics)
|
||||
- [Transactions with Spring and JPA](https://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [Introduction to Spring Data JPA](http://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
|
||||
|
||||
Reference in New Issue
Block a user