LJUGM - Boost your Java Enterprise Stack with MongoDB
-
Upload
bernd-zuther -
Category
Education
-
view
228 -
download
1
description
Transcript of 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
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" }
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;
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();
Unit Tests
relational in memory DB for testing
- Fast
- Isolated
- Repeatable
- Self-verifying
- Timely
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()); }
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" />
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
MongoDB - Schemaless
relational in memory DB for testing
{ name : "Bernd", age : 30, interests : "football" } !{ name : "Christian", age : 25 }
Build... Test... Live!
https://github.com/comsysto/mongodb-onlineshop/