Merge remote-tracking branch 'upstream/master'

This commit is contained in:
anuragkumawat
2022-03-12 00:11:08 +05:30
31 changed files with 1187 additions and 43 deletions
@@ -52,9 +52,11 @@ public class HibernateManyToManyAnnotationMainIntegrationTest {
for(Employee employee : employeeList) {
assertNotNull(employee.getProjects());
assertEquals(2, employee.getProjects().size());
}
for(Project project : projectList) {
assertNotNull(project.getEmployees());
assertEquals(2, project.getEmployees().size());
}
}
@@ -70,6 +72,11 @@ public class HibernateManyToManyAnnotationMainIntegrationTest {
for (String emp : employeeData) {
Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]);
employee.setProjects(projects);
for (Project proj : projects) {
proj.getEmployees().add(employee);
}
session.persist(employee);
}
}
@@ -0,0 +1,99 @@
package com.baeldung.mongo;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.bson.Document;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
public class PushOperations {
private static MongoClient mongoClient;
private static String testCollectionName;
private static String databaseName;
public static void setUp() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
}
databaseName = "baeldung";
testCollectionName = "orders";
}
public static void pushOperationUsingDBObject() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(testCollectionName);
DBObject listItem = new BasicDBObject("items", new BasicDBObject("itemName", "PIZZA MANIA").append("quantity", 1)
.append("price", 800));
BasicDBObject searchFilter = new BasicDBObject("customerId", 1023);
BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$push", listItem);
UpdateResult updateResult = collection.updateOne(searchFilter, updateQuery);
System.out.println("updateResult:- " + updateResult);
}
public static void pushOperationUsingDocument() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(testCollectionName);
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.push("items", item));
System.out.println("updateResult:- " + updateResult);
}
public static void addToSetOperation() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(testCollectionName);
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.addToSet("items", item));
System.out.println("updateResult:- " + updateResult);
}
public static void main(String args[]) {
//
// Connect to cluster (default is localhost:27017)
//
setUp();
//
// Push document into the array using DBObject
//
pushOperationUsingDBObject();
//
// Push document into the array using Document.
//
pushOperationUsingDocument();
//
// Push document into the array using addToSet operator.
//
addToSetOperation();
}
}
@@ -0,0 +1,92 @@
package com.baeldung.mongo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import org.bson.Document;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
public class PushOperationLiveTest {
private static MongoClient mongoClient;
private static MongoDatabase db;
private static MongoCollection<Document> collection;
@BeforeClass
public static void setup() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDatabase("baeldung");
collection = db.getCollection("orders");
collection.insertOne(
Document.parse("{\n" + " \"customerId\": 1023,\n" + " \"orderTimestamp\": NumberLong(\"1646460073000\"),\n" + " \"shippingDestination\": \"336, Street No.1 Pawai Mumbai\",\n" + " \"purchaseOrder\": 1000,\n"
+ " \"contactNumber\":\"9898987676\",\n" + " \"items\": [ \n" + " {\n" + " \"itemName\": \"BERGER\",\n" + " \"quantity\": 1,\n" + " \"price\": 500\n" + " },\n"
+ " {\n" + " \"itemName\": \"VEG PIZZA\",\n" + " \"quantity\": 1,\n" + " \"price\": 800\n" + " } \n" + " ]\n" + " }"));
}
}
@Test
public void givenOrderCollection_whenPushOperationUsingDBObject_thenCheckingForDocument() {
DBObject listItem = new BasicDBObject("items", new BasicDBObject("itemName", "PIZZA MANIA").append("quantity", 1)
.append("price", 800));
BasicDBObject searchFilter = new BasicDBObject("customerId", 1023);
BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$push", listItem);
UpdateResult updateResult = collection.updateOne(searchFilter, updateQuery);
Document orderDetail = collection.find(Filters.eq("customerId", 1023))
.first();
assertNotNull(orderDetail);
assertFalse(orderDetail.isEmpty());
}
@Test
public void givenOrderCollection_whenPushOperationUsingDocument_thenCheckingForDocument() {
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.push("items", item));
Document orderDetail = collection.find(Filters.eq("customerId", 1023))
.first();
assertNotNull(orderDetail);
assertFalse(orderDetail.isEmpty());
}
@Test
public void givenOrderCollection_whenAddToSetOperation_thenCheckingForDocument() {
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.addToSet("items", item));
Document orderDetail = collection.find(Filters.eq("customerId", 1023))
.first();
assertNotNull(orderDetail);
assertFalse(orderDetail.isEmpty());
}
@AfterClass
public static void cleanUp() {
mongoClient.close();
}
}
+2
View File
@@ -27,6 +27,7 @@
<module>hbase</module>
<module>hibernate5</module>
<module>hibernate-mapping</module> <!-- long running -->
<module>hibernate-mapping-2</module>
<module>hibernate-ogm</module>
<module>hibernate-annotations</module>
<module>hibernate-exceptions</module>
@@ -73,6 +74,7 @@
<module>spring-data-jpa-crud</module>
<module>spring-data-jpa-crud-2</module>
<module>spring-data-jpa-enterprise</module>
<module>spring-data-jpa-enterprise-2</module>
<module>spring-data-jpa-filtering</module>
<module>spring-data-jpa-query</module>
<module>spring-data-jpa-query-2</module>