Merge branch 'master' of https://github.com/Maiklins/tutorials into Java-1524-split-move-spring-boot-persistence
Conflicts: persistence-modules/spring-boot-persistence-2/README.md
This commit is contained in:
@@ -14,3 +14,4 @@ This module contains articles specific to use of Hibernate as a JPA implementati
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
||||
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
|
||||
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
|
||||
- [Hibernate Error “No Persistence Provider for EntityManager”](https://www.baeldung.com/hibernate-no-persistence-provider)
|
||||
|
||||
@@ -11,3 +11,4 @@ This module contains articles about MongoDB in Java.
|
||||
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
|
||||
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
|
||||
- [MongoDB BSON to JSON](https://www.baeldung.com/bson-to-json)
|
||||
- [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json)
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.baeldung.bsontojson;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import dev.morphia.annotations.Embedded;
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Field;
|
||||
import dev.morphia.annotations.Id;
|
||||
import dev.morphia.annotations.Index;
|
||||
import dev.morphia.annotations.IndexOptions;
|
||||
import dev.morphia.annotations.Indexes;
|
||||
import dev.morphia.annotations.Property;
|
||||
import dev.morphia.annotations.Reference;
|
||||
import dev.morphia.annotations.Validation;
|
||||
|
||||
@Entity("Books")
|
||||
@Indexes({ @Index(fields = @Field("title"), options = @IndexOptions(name = "book_title")) })
|
||||
@Validation("{ price : { $gt : 0 } }")
|
||||
public class Book {
|
||||
@Id
|
||||
private String isbn;
|
||||
@Property
|
||||
private String title;
|
||||
private String author;
|
||||
@Embedded
|
||||
private Publisher publisher;
|
||||
@Property("price")
|
||||
private double cost;
|
||||
@Reference
|
||||
private Set<Book> companionBooks;
|
||||
@Property
|
||||
private LocalDateTime publishDate;
|
||||
|
||||
public Book() {
|
||||
|
||||
}
|
||||
|
||||
public Book(String isbn, String title, String author, double cost, Publisher publisher) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.cost = cost;
|
||||
this.publisher = publisher;
|
||||
this.companionBooks = new HashSet<>();
|
||||
}
|
||||
|
||||
// Getters and setters ...
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public Book setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public Book setTitle(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public Book setAuthor(String author) {
|
||||
this.author = author;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Publisher getPublisher() {
|
||||
return publisher;
|
||||
}
|
||||
|
||||
public Book setPublisher(Publisher publisher) {
|
||||
this.publisher = publisher;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public Book setCost(double cost) {
|
||||
this.cost = cost;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LocalDateTime getPublishDate() {
|
||||
return publishDate;
|
||||
}
|
||||
|
||||
public Book setPublishDate(LocalDateTime publishDate) {
|
||||
this.publishDate = publishDate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Set<Book> getCompanionBooks() {
|
||||
return companionBooks;
|
||||
}
|
||||
|
||||
public Book addCompanionBooks(Book book) {
|
||||
if (companionBooks != null)
|
||||
this.companionBooks.add(book);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [isbn=" + isbn + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", cost=" + cost + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((author == null) ? 0 : author.hashCode());
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(cost);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
|
||||
result = prime * result + ((publisher == null) ? 0 : publisher.hashCode());
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Book other = (Book) obj;
|
||||
if (author == null) {
|
||||
if (other.author != null)
|
||||
return false;
|
||||
} else if (!author.equals(other.author))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(cost) != Double.doubleToLongBits(other.cost))
|
||||
return false;
|
||||
if (isbn == null) {
|
||||
if (other.isbn != null)
|
||||
return false;
|
||||
} else if (!isbn.equals(other.isbn))
|
||||
return false;
|
||||
if (publisher == null) {
|
||||
if (other.publisher != null)
|
||||
return false;
|
||||
} else if (!publisher.equals(other.publisher))
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.bsontojson;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
|
||||
@Entity
|
||||
public class Publisher {
|
||||
|
||||
@Id
|
||||
private ObjectId id;
|
||||
private String name;
|
||||
|
||||
public Publisher() {
|
||||
|
||||
}
|
||||
|
||||
public Publisher(ObjectId id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ObjectId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(ObjectId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Catalog [id=" + id + ", name=" + name + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Publisher other = (Publisher) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+4
-18
@@ -2,32 +2,18 @@ package com.baeldung.bsontojson;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.json.Converter;
|
||||
import org.bson.json.JsonMode;
|
||||
import org.bson.json.JsonWriterSettings;
|
||||
import org.bson.json.StrictJsonWriter;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.baeldung.morphia.domain.Book;
|
||||
import com.baeldung.morphia.domain.Publisher;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
|
||||
@@ -42,7 +28,7 @@ public class BsonToJsonLiveTest {
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
Morphia morphia = new Morphia();
|
||||
morphia.mapPackage("com.baeldung.morphia");
|
||||
morphia.mapPackage("com.baeldung.bsontojson");
|
||||
datastore = morphia.createDatastore(new MongoClient(), DB_NAME);
|
||||
datastore.ensureIndexes();
|
||||
|
||||
@@ -72,7 +58,7 @@ public class BsonToJsonLiveTest {
|
||||
}
|
||||
|
||||
String expectedJson = "{\"_id\": \"isbn\", " +
|
||||
"\"className\": \"com.baeldung.morphia.domain.Book\", " +
|
||||
"\"className\": \"com.baeldung.bsontojson.Book\", " +
|
||||
"\"title\": \"title\", " +
|
||||
"\"author\": \"author\", " +
|
||||
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
|
||||
@@ -100,7 +86,7 @@ public class BsonToJsonLiveTest {
|
||||
}
|
||||
|
||||
String expectedJson = "{\"_id\": \"isbn\", " +
|
||||
"\"className\": \"com.baeldung.morphia.domain.Book\", " +
|
||||
"\"className\": \"com.baeldung.bsontojson.Book\", " +
|
||||
"\"title\": \"title\", " +
|
||||
"\"author\": \"author\", " +
|
||||
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
|
||||
@@ -127,7 +113,7 @@ public class BsonToJsonLiveTest {
|
||||
}
|
||||
|
||||
String expectedJson = "{\"_id\": \"isbn\", " +
|
||||
"\"className\": \"com.baeldung.morphia.domain.Book\", " +
|
||||
"\"className\": \"com.baeldung.bsontojson.Book\", " +
|
||||
"\"title\": \"title\", " +
|
||||
"\"author\": \"author\", " +
|
||||
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
|
||||
|
||||
@@ -4,4 +4,5 @@
|
||||
- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool)
|
||||
- [Integrating Spring Boot with HSQLDB](https://www.baeldung.com/spring-boot-hsqldb)
|
||||
- [List of In-Memory Databases](http://www.baeldung.com/java-in-memory-databases)
|
||||
- More articles: [[<-- prev]](../spring-boot-persistence)
|
||||
- [Oracle Connection Pooling With Spring](https://www.baeldung.com/spring-oracle-connection-pooling)
|
||||
- More articles: [[<-- prev]](../spring-boot-persistence)
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.spring.transactional;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
class TransactionalCompareApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TransactionalCompareApplication.class, args);
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.spring.transactional.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Car {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String make;
|
||||
|
||||
private String model;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(Long id, String make, String model) {
|
||||
this.id = id;
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car{" +
|
||||
"id=" + id +
|
||||
", make='" + make + '\'' +
|
||||
", model='" + model + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.spring.transactional.repository;
|
||||
|
||||
import com.baeldung.spring.transactional.entity.Car;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CarRepository extends JpaRepository<Car, Long> {
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.spring.transactional.service;
|
||||
|
||||
import com.baeldung.spring.transactional.entity.Car;
|
||||
import com.baeldung.spring.transactional.repository.CarRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
|
||||
@Service
|
||||
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.SUPPORTS, readOnly = false, timeout = 30)
|
||||
public class CarService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
@Transactional(rollbackFor = IllegalArgumentException.class, noRollbackFor = EntityExistsException.class,
|
||||
rollbackForClassName = "IllegalArgumentException", noRollbackForClassName = "EntityExistsException")
|
||||
public Car save(Car car) {
|
||||
return carRepository.save(car);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.spring.transactional.service;
|
||||
|
||||
import com.baeldung.spring.transactional.entity.Car;
|
||||
import com.baeldung.spring.transactional.repository.CarRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional(Transactional.TxType.SUPPORTS)
|
||||
public class RentalService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
@Transactional(rollbackOn = IllegalArgumentException.class, dontRollbackOn = EntityExistsException.class)
|
||||
public Car rent(Car car) {
|
||||
return carRepository.save(car);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
- [JPA Entity Lifecycle Events](https://www.baeldung.com/jpa-entity-lifecycle-events)
|
||||
- [Working with Lazy Element Collections in JPA](https://www.baeldung.com/java-jpa-lazy-collections)
|
||||
- [Calling Stored Procedures from Spring Data JPA Repositories](https://www.baeldung.com/spring-data-jpa-stored-procedures)
|
||||
- [Custom Naming Convention with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-custom-naming)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
Reference in New Issue
Block a user