[BAEL-14774] - Fixed conflicts and replaced hsql with h2

This commit is contained in:
amit2103
2019-07-08 00:21:11 +05:30
109 changed files with 1269 additions and 172 deletions
@@ -0,0 +1,3 @@
### Relevant Articles
- [Jest Elasticsearch Java Client](https://www.baeldung.com/elasticsearch-jest)
+6 -2
View File
@@ -20,12 +20,16 @@
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.1</version>
<version>${jest.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<properties>
<jest.version>6.3.1</jest.version>
</properties>
</project>
@@ -3,4 +3,5 @@
- [Persisting Maps with Hibernate](https://www.baeldung.com/hibernate-persisting-maps)
- [Difference Between @Size, @Length, and @Column(length=value)](https://www.baeldung.com/jpa-size-length-column-differences)
- [Hibernate Validator Specific Constraints](https://www.baeldung.com/hibernate-validator-constraints)
- [Hibernate One to Many Annotation Tutorial](http://www.baeldung.com/hibernate-one-to-many)
@@ -31,12 +31,6 @@
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.hibernate</groupId>
@@ -51,12 +45,12 @@
<dependency>
<groupId>javax.money</groupId>
<artifactId>money-api</artifactId>
<version>1.0.3</version>
<version>${money-api.version}</version>
</dependency>
<dependency>
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>1.3</version>
<version>${moneta.version}</version>
<type>pom</type>
</dependency>
</dependencies>
@@ -76,7 +70,8 @@
<assertj-core.version>3.8.0</assertj-core.version>
<hibernate-validator.version>6.0.16.Final</hibernate-validator.version>
<org.glassfish.javax.el.version>3.0.1-b11</org.glassfish.javax.el.version>
<hsqldb.version>2.3.4</hsqldb.version>
<money-api.version>1.0.3</money-api.version>
<moneta.version>1.3</moneta.version>
</properties>
</project>
@@ -10,7 +10,7 @@ import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.AfterClass;
@@ -18,6 +18,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.baeldung.hibernate.oneToMany.model.Cart;
import com.baeldung.hibernate.oneToMany.model.Items;
@@ -33,9 +34,9 @@ public class HibernateOneToManyAnnotationMainIntegrationTest {
@BeforeClass
public static void beforeTests() {
Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class)
.setProperty("hibernate.dialect", HSQLDialect.class.getName())
.setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
.setProperty("hibernate.dialect", H2Dialect.class.getName())
.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
.setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
+11 -5
View File
@@ -19,31 +19,31 @@
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>5.4.0.Final</version>
<version>${hibernate.version}</version>
</dependency>
<!-- Neo4j -->
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-neo4j</artifactId>
<version>5.4.0.Final</version>
<version>${hibernate.version}</version>
</dependency>
<!-- Narayana JTA -->
<dependency>
<groupId>org.jboss.narayana.jta</groupId>
<artifactId>narayana-jta</artifactId>
<version>5.5.23.Final</version>
<version>${narayana-jta.version}</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<version>1.4</version>
<version>${fest-assert.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -57,4 +57,10 @@
</resource>
</resources>
</build>
<properties>
<hibernate.version>5.4.0.Final</hibernate.version>
<fest-assert.version>1.4</fest-assert.version>
<narayana-jta.version>5.5.23.Final</narayana-jta.version>
</properties>
</project>
+1
View File
@@ -33,3 +33,4 @@
- [Hibernate Aggregate Functions](https://www.baeldung.com/hibernate-aggregate-functions)
- [Hibernate Query Plan Cache](https://www.baeldung.com/hibernate-query-plan-cache)
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)
+5 -3
View File
@@ -60,7 +60,7 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-testing</artifactId>
<version>5.2.2.Final</version>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
@@ -70,7 +70,7 @@
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.9.5</version>
<version>${byte-buddy.version}</version>
</dependency>
<dependency>
@@ -91,7 +91,7 @@
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
<version>${jaxb-api.version}</version>
</dependency>
</dependencies>
@@ -120,6 +120,8 @@
<assertj-core.version>3.8.0</assertj-core.version>
<openjdk-jmh.version>1.21</openjdk-jmh.version>
<geodb.version>0.9</geodb.version>
<byte-buddy.version>1.9.5</byte-buddy.version>
<jaxb-api.version>2.3.0</jaxb-api.version>
</properties>
</project>
+2 -1
View File
@@ -34,7 +34,7 @@
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
<version>${javax.persistence-api.version}</version>
</dependency>
<!--Runtime JPA implementation-->
@@ -108,6 +108,7 @@
<hibernate.version>5.4.0.Final</hibernate.version>
<eclipselink.version>2.7.4-RC1</eclipselink.version>
<postgres.version>42.2.5</postgres.version>
<javax.persistence-api.version>2.2</javax.persistence-api.version>
</properties>
</project>
@@ -20,6 +20,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -0,0 +1,9 @@
package com.baeldung.mongodb.daos;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.baeldung.mongodb.models.Photo;
public interface PhotoRepository extends MongoRepository<Photo, String> {
}
@@ -0,0 +1,51 @@
package com.baeldung.mongodb.models;
import org.bson.types.Binary;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "photos")
public class Photo {
@Id
private String id;
private String title;
private Binary image;
public Photo(String title) {
super();
this.title = title;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Binary getImage() {
return image;
}
public void setImage(Binary image) {
this.image = image;
}
@Override
public String toString() {
return "Photo [id=" + id + ", title=" + title + ", image=" + image + "]";
}
}
@@ -0,0 +1,39 @@
package com.baeldung.mongodb.models;
import java.io.InputStream;
public class Video {
private String title;
private InputStream stream;
public Video() {
super();
}
public Video(String title) {
super();
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public InputStream getStream() {
return stream;
}
public void setStream(InputStream stream) {
this.stream = stream;
}
@Override
public String toString() {
return "Video [title=" + title + "]";
}
}
@@ -0,0 +1,38 @@
package com.baeldung.mongodb.services;
import java.io.IOException;
import java.util.Optional;
import org.bson.BsonBinarySubType;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.daos.PhotoRepository;
import com.baeldung.mongodb.models.Photo;
@Service
public class PhotoService {
@Autowired
private PhotoRepository photoRepo;
public Photo getPhoto(String id) {
Optional<Photo> result = photoRepo.findById(id);
return result.isPresent() ? result.get() : null;
}
public String addPhoto(String title, MultipartFile file) {
String id = null;
try {
Photo photo = new Photo(title);
photo.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes()));
photo = photoRepo.insert(photo);
id = photo.getId();
} catch (IOException e) {
return null;
}
return id;
}
}
@@ -0,0 +1,56 @@
package com.baeldung.mongodb.services;
import java.io.IOException;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.models.Video;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.model.GridFSFile;
@Service
public class VideoService {
@Autowired
private GridFsTemplate gridFsTemplate;
@Autowired
private GridFsOperations operations;
public Video getVideo(String id) {
Video video = null;
GridFSFile file = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
if (file != null) {
video = new Video();
video.setTitle(file.getMetadata().get("title").toString());
try {
video.setStream(operations.getResource(file).getInputStream());
} catch (IOException e) {
return null;
}
}
return video;
}
public String addVideo(String title, MultipartFile file) {
DBObject metaData = new BasicDBObject();
metaData.put("type", "video");
metaData.put("title", title);
ObjectId id;
try {
id = gridFsTemplate.store(file.getInputStream(), file.getName(), file.getContentType(), metaData);
} catch (IOException e) {
return null;
}
return id.toString();
}
}
@@ -0,0 +1,50 @@
package com.baeldung.mongodb.web;
import java.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.models.Photo;
import com.baeldung.mongodb.services.PhotoService;
@Controller
public class PhotoController {
@Autowired
private PhotoService photoService;
@GetMapping("/photos/{id}")
public String getPhoto(@PathVariable String id, Model model) {
Photo photo = photoService.getPhoto(id);
if (photo != null) {
model.addAttribute("title", photo.getTitle());
model.addAttribute("image", Base64.getEncoder().encodeToString(photo.getImage().getData()));
return "photos";
}
model.addAttribute("message", "Photo not found");
return "index";
}
@GetMapping("/photos/upload")
public String uploadPhoto(Model model) {
model.addAttribute("message", "hello");
return "uploadPhoto";
}
@PostMapping("/photos/add")
public String addPhoto(@RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model) {
String id = photoService.addPhoto(title, image);
if (id == null) {
model.addAttribute("message", "Error Occurred");
return "index";
}
return "redirect:/photos/" + id;
}
}
@@ -0,0 +1,67 @@
package com.baeldung.mongodb.web;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.baeldung.mongodb.models.Video;
import com.baeldung.mongodb.services.VideoService;
@Controller
public class VideoController {
@Autowired
private VideoService videoService;
@GetMapping("/videos/{id}")
public String getVideo(@PathVariable String id, Model model) {
Video video = videoService.getVideo(id);
if (video != null) {
model.addAttribute("title", video.getTitle());
model.addAttribute("url", "/videos/stream/" + id);
return "videos";
}
model.addAttribute("message", "Video not found");
return "index";
}
@GetMapping("/videos/stream/{id}")
public void streamVideo(@PathVariable String id, HttpServletResponse response) {
Video video = videoService.getVideo(id);
if (video != null) {
try {
FileCopyUtils.copy(video.getStream(), response.getOutputStream());
} catch (IOException e) {
response.setStatus(500);
}
} else {
response.setStatus(404);
}
}
@GetMapping("/videos/upload")
public String uploadVideo(Model model) {
model.addAttribute("message", "hello");
return "uploadVideo";
}
@PostMapping("/videos/add")
public String addVideo(@RequestParam("title") String title, @RequestParam("file") MultipartFile file, Model model) {
String id = videoService.addVideo(title, file);
if (id == null) {
model.addAttribute("message", "Error Occurred");
return "index";
}
return "redirect:/videos/" + id;
}
}
@@ -1,8 +1,13 @@
spring.application.name=spring-boot-persistence
server.port=${PORT:0}
server.port=8082
#spring boot mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=springboot-mongo
spring.thymeleaf.cache=false
spring.servlet.multipart.max-file-size=256MB
spring.servlet.multipart.max-request-size=256MB
spring.servlet.multipart.enabled=true
@@ -0,0 +1,14 @@
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Files MongoDB</title>
</head>
<body>
<h1>Home Page</h1>
<div th:if="${message != null}" th:text="${message}">Message</div>
<br/>
<a href="/photos/upload">Upload new Photo</a>
<br/><br/>
<a href="/videos/upload">Upload new Video</a>
</body>
</html>
@@ -0,0 +1,10 @@
<html xmlns:th="https://www.thymeleaf.org">
<body>
<h1>View Photo</h1>
Title: <span th:text="${title}">name</span>
<br/>
<img alt="sample" th:src="*{'data:image/png;base64,'+image}" width="200"/>
<br/> <br/>
<a href="/">Back to home page</a>
</body>
</html>
@@ -0,0 +1,15 @@
<html xmlns:th="https://www.thymeleaf.org">
<body>
<h1>Upload new Photo</h1>
<form method="POST" action="/photos/add" enctype="multipart/form-data">
Title:<input type="text" name="title" />
<br/>
Image:<input type="file" name="image" accept="image/*" />
<br/>
<br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
@@ -0,0 +1,15 @@
<html xmlns:th="https://www.thymeleaf.org">
<body>
<h1>Upload new Video</h1>
<form method="POST" action="/videos/add" enctype="multipart/form-data">
Title:<input type="text" name="title" />
<br/>
Video:<input type="file" name="file" accept="video/*" />
<br/>
<br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
@@ -0,0 +1,15 @@
<html xmlns:th="https://www.thymeleaf.org">
<body>
<h1>View Video</h1>
Title: <span th:text="${title}">title</span>
<br/>
<br/>
<video width="400" controls>
<source th:src="${url}" />
</video>
<br/> <br/>
<a href="/">Back to home page</a>
</body>
</html>
@@ -29,7 +29,7 @@
<dependency>
<groupId>net.ttddyy</groupId>
<artifactId>datasource-proxy</artifactId>
<version>1.4.1</version>
<version>${datasource-proxy.version}</version>
</dependency>
<dependency>
@@ -42,4 +42,8 @@
<artifactId>spring-oxm</artifactId>
</dependency>
</dependencies>
<properties>
<datasource-proxy.version>1.4.1</datasource-proxy.version>
</properties>
</project>
+6 -3
View File
@@ -37,14 +37,14 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.10.6</version>
<version>${testcontainers.postgresql.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
<version>${postgresql.version}</version>
</dependency>
<!-- Test containers only dependencies -->
@@ -66,7 +66,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
<version>${guava.version}</version>
</dependency>
<!-- JUnit Jupiter dependencies -->
@@ -92,6 +92,9 @@
<properties>
<start-class>com.baeldung.boot.Application</start-class>
<testcontainers.postgresql.version>1.10.6</testcontainers.postgresql.version>
<postgresql.version>42.2.5</postgresql.version>
<guava.version>21.0</guava.version>
</properties>
</project>