Merge branch 'master' into JAVA-4

This commit is contained in:
Sampada
2020-03-15 12:58:45 +05:30
committed by GitHub
853 changed files with 10678 additions and 30025 deletions
+1 -1
View File
@@ -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)
@@ -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>
@@ -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);
}
}
@@ -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);
}
}
@@ -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;
}
}
@@ -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);
}
@@ -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 ;
@@ -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() {
@@ -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,4 +1,4 @@
package org.baeldung.persistence.dao;
package com.baeldung.persistence.dao;
import java.io.Serializable;
import java.util.List;
@@ -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
}
@@ -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
@@ -0,0 +1,8 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Event;
public interface IEventDao extends IOperations<Event> {
//
}
@@ -0,0 +1,7 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Foo;
public interface IFooDao extends IOperations<Foo> {
//
}
@@ -1,4 +1,4 @@
package org.baeldung.persistence.dao;
package com.baeldung.persistence.dao;
import java.io.Serializable;
import java.util.List;
@@ -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,4 +1,4 @@
package org.baeldung.persistence.model;
package com.baeldung.persistence.model;
import java.io.Serializable;
@@ -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);
}
}
@@ -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;
@@ -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;
@@ -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
@@ -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 {
@@ -1,9 +0,0 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.Event;
public interface IEventDao extends IOperations<Event> {
//
}
@@ -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>
@@ -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"/>

Some files were not shown because too many files have changed in this diff Show More