Top Banner
Boost your Java Enterprise Stack with MongoDB [email protected] / twitter: @Bernd_Z [email protected] 26. Nov. 2013 @ Leightweight Java User Group Munich
22

LJUGM - Boost your Java Enterprise Stack with MongoDB

Jan 13, 2015

Download

Education

Bernd Zuther

If you want to use MongoDB in the Java Enterprise World you have to address two questions.

The first one is: Should I use the native driver or should I use a persistence framework? Spring is a common framework in many Java applications. It has created a very good object-document-mapping framework with Spring Data for MongoDB. It is easy to integrate into new and existing Java applications.

The second question is: How can I ensure my deliverables against error? So you need a testing framework. NoSQLUnit is such a framework that helps you to write tests against a real MongoDB Server or an in-memory clone like Fongo.

This talk will cover Spring Data and NoSQLUnit. It will give a basic overview about what things are useful before you can deploy your application, e.g. in a cloud service like MongoSoup.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Boost your Java Enterprise Stack

with MongoDB

!

!

[email protected] / twitter: @Bernd_Z [email protected]

!26. Nov. 2013 @ Leightweight Java User Group Munich

Page 2: LJUGM  - Boost your Java Enterprise Stack with MongoDB
Page 3: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Continuous Delivery

Page 4: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Standard JEE Stack

Frontend Layer

Service Layer

Repository Layer

Page 5: LJUGM  - Boost your Java Enterprise Stack with MongoDB
Page 6: LJUGM  - Boost your Java Enterprise Stack with MongoDB

MongoDB{ "_id" : ObjectId("51f2ce49c2e6dce83534b73f"), "orderId" : NumberLong(666), "orderItems" : [ { "product" : { "_id" : ObjectId("51f2ce48c2e6dce83534b6ba"), "articleId" : "100", "name" : "Margherita", "urlname" : "margherita", "description" : "our famous tomato sauce and genuine mozzarella", "type" : "PIZZA", "category" : "vegetarian", "price" : 3.8 }, "uuid" : "839f8305-a5e7-408a-96d5-e3d2819d6ad3" } ], "orderDate" : ISODate("2011-09-13T07:24:00Z"), "deliveryAddress" : { "firstname" : "Christan", "lastname" : "Kroemer", "street" : „Lindwurm Straße", "zip" : "80337", "city" : "Munich", "houseNumber" : "97" }, "userId" : "51f2ce48c2e6dce83534b70b" }

Page 7: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Hibernate ORM

mapping of POJOs to relational database tables

@Entity @Table(name = „ORDER") public class Order implements Serializable { ! @Id @GeneratedValue @Column(name="ID") private Long id; ! @OneToOne(cascade = CascadeType.ALL, optional = false, orphanRemoval = false) @JoinColumn(name = "USER_ID", nullable = false) @ForeignKey(name = "ORDER_USER_FK") private User user; ! @OneToMany(mappedBy = "order") @Fetch(FetchMode.JOIN) private List<OrderItem> orderItems;

Page 8: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Spring Data

annotated sample class here

@Document(collection = Order.COLLECTION_NAME) public class Order implements Serializable { public static final String COLLECTION_NAME = "order"; ! @Id private ObjectId id; ! @Indexed(unique = true) private long orderId; ! @DBRef private User user; private List<OrderItem> orderItems; ! @Indexed private Date orderDate = new Date();

Page 9: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Unit Tests

relational in memory DB for testing

- Fast

- Isolated

- Repeatable

- Self-verifying

- Timely

Page 10: LJUGM  - Boost your Java Enterprise Stack with MongoDB

DBUnit<?xml version="1.0" encoding="UTF-8"?> <dataset> <User id="0" title="Mr" firstName="Bernd" lastName="Zuther"/> <User id="1" title="Mr" firstName="Christian" lastName="Kroemer"/> </dataset>

@Test @DatabaseSetup("sampleData.xml") public void testFind() throws Exception { List<User> userList = userService.findByName("Christian"); assertEquals(1, userList.size()); assertEquals("Christian", userList.get(0).getFirstName()); }

Page 11: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Fongo

alternative for the Mongo world TODO: does mongo soup fit in there anywhere?

<dependency> <groupId>com.github.fakemongo</groupId> <artifactId>fongo</artifactId> <version>1.3.2</version> </dependency> !!!!<bean name="fongo" class=“com.github.fakemongo.Fongo"> <constructor-arg value="InMemoryMongo" /> </bean> !<bean id="mongo" factory-bean="fongo" factory-method="getMongo" />

Page 12: LJUGM  - Boost your Java Enterprise Stack with MongoDB

NoSQLUnit@ActiveProfiles("test") @ContextConfiguration(locations = "classpath:com/comsysto/shop/ui/spring-context.xml") @UsingDataSet(locations="initialData.json", loadStrategy=LoadStrategyEnum.CLEAN_INSERT) public class OrderRepositoryImplTest extends AbstractJUnit4SpringContextTests { ! @Autowired private OrderRepository orderRepository; @Autowired private ApplicationContext applicationContext; @Rule public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb("pizza"); ! @Test public void testFindAll() { Sort sortOrder = new Sort(Sort.Direction.ASC, "orderId"); List<Order> retrievedOrders = orderRepository.findAll(10, 0, sortOrder); assertNotNull(retrievedOrders); assertEquals(5, retrievedOrders.size()); } }

Presenter

Page 13: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Continuous Delivery

Page 14: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Infrastructure

Page 15: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Danger Area

Page 16: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Downtime

Page 17: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Flyway

relational in memory DB for testing

Page 18: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Flyway

relational in memory DB for testing

Page 19: LJUGM  - Boost your Java Enterprise Stack with MongoDB

MongoDB - Schemaless

relational in memory DB for testing

{ name : "Bernd", age : 30, interests : "football" } !{ name : "Christian", age : 25 }

Page 20: LJUGM  - Boost your Java Enterprise Stack with MongoDB

MongoDB - Schemaless

relational in memory DB for testing

Page 21: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Build... Test... Live!

https://github.com/comsysto/mongodb-onlineshop/

Page 22: LJUGM  - Boost your Java Enterprise Stack with MongoDB

Questions

Presenter