Skip to content

Commit 433c246

Browse files
added example to use ManytoOne JPA Mapping, plus entity mappings for Cart, and CartDetail to support a 'shopping' cart
1 parent 72c5eca commit 433c246

File tree

4 files changed

+221
-2
lines changed

4 files changed

+221
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package guru.springframework.domain;
2+
3+
import javax.persistence.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Created by jt on 12/15/15.
9+
*/
10+
@Entity
11+
public class Cart implements DomainObject{
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.AUTO)
15+
private Integer id;
16+
17+
@Version
18+
private Integer version;
19+
20+
@OneToOne
21+
private User user;
22+
23+
@OneToMany(cascade = CascadeType.ALL, mappedBy = "cart", orphanRemoval = true)
24+
private List<CartDetail> cartDetails = new ArrayList<>();
25+
26+
@Override
27+
public Integer getId() {
28+
return id;
29+
}
30+
31+
@Override
32+
public void setId(Integer id) {
33+
this.id = id;
34+
}
35+
36+
public Integer getVersion() {
37+
return version;
38+
}
39+
40+
public void setVersion(Integer version) {
41+
this.version = version;
42+
}
43+
44+
public User getUser() {
45+
return user;
46+
}
47+
48+
public void setUser(User user) {
49+
this.user = user;
50+
}
51+
52+
public List<CartDetail> getCartDetails() {
53+
return cartDetails;
54+
}
55+
56+
public void setCartDetails(List<CartDetail> cartDetails) {
57+
this.cartDetails = cartDetails;
58+
}
59+
60+
public void addCartDetail(CartDetail cartDetail){
61+
cartDetails.add(cartDetail);
62+
cartDetail.setCart(this);
63+
}
64+
65+
public void removeCartDetail(CartDetail cartDetail){
66+
cartDetail.setCart(null);
67+
this.cartDetails.remove(cartDetail);
68+
}
69+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package guru.springframework.domain;
2+
3+
import javax.persistence.*;
4+
5+
/**
6+
* Created by jt on 12/15/15.
7+
*/
8+
@Entity
9+
public class CartDetail implements DomainObject {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.AUTO)
13+
private Integer id;
14+
15+
@Version
16+
private Integer version;
17+
18+
@ManyToOne
19+
private Cart cart;
20+
21+
@OneToOne
22+
private Product product;
23+
24+
@Override
25+
public Integer getId() {
26+
return id;
27+
}
28+
29+
@Override
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public Integer getVersion() {
35+
return version;
36+
}
37+
38+
public void setVersion(Integer version) {
39+
this.version = version;
40+
}
41+
42+
public Cart getCart() {
43+
return cart;
44+
}
45+
46+
public void setCart(Cart cart) {
47+
this.cart = cart;
48+
}
49+
50+
public Product getProduct() {
51+
return product;
52+
}
53+
54+
public void setProduct(Product product) {
55+
this.product = product;
56+
}
57+
}

src/main/java/guru/springframework/domain/User.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class User implements DomainObject {
2626
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
2727
private Customer customer;
2828

29+
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
30+
private Cart cart;
31+
2932
@Override
3033
public Integer getId() {
3134
return id;
@@ -84,4 +87,12 @@ public void setCustomer(Customer customer) {
8487
this.customer = customer;
8588
customer.setUser(this);
8689
}
90+
91+
public Cart getCart() {
92+
return cart;
93+
}
94+
95+
public void setCart(Cart cart) {
96+
this.cart = cart;
97+
}
8798
}

src/test/java/guru/springframework/services/UserServiceJpaDaoImplTest.java

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package guru.springframework.services;
22

33
import guru.springframework.config.JpaIntegrationConfig;
4-
import guru.springframework.domain.Customer;
5-
import guru.springframework.domain.User;
4+
import guru.springframework.domain.*;
65
import org.junit.Test;
76
import org.junit.runner.RunWith;
87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.boot.test.SpringApplicationConfiguration;
109
import org.springframework.test.context.ActiveProfiles;
1110
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1211

12+
import java.util.List;
13+
1314
/**
1415
* Created by jt on 12/14/15.
1516
*/
@@ -19,12 +20,18 @@
1920
public class UserServiceJpaDaoImplTest {
2021

2122
private UserService userService;
23+
private ProductService productService;
2224

2325
@Autowired
2426
public void setUserService(UserService userService) {
2527
this.userService = userService;
2628
}
2729

30+
@Autowired
31+
public void setProductService(ProductService productService) {
32+
this.productService = productService;
33+
}
34+
2835
@Test
2936
public void testSaveOfUser() throws Exception {
3037
User user = new User();
@@ -64,4 +71,79 @@ public void testSaveOfUserWithCustomer() throws Exception {
6471
assert savedUser.getCustomer().getId() != null;
6572

6673
}
74+
75+
@Test
76+
public void testAddCartToUser() throws Exception {
77+
User user = new User();
78+
79+
user.setUsername("someusername");
80+
user.setPassword("myPassword");
81+
82+
user.setCart(new Cart());
83+
84+
User savedUser = userService.saveOrUpdate(user);
85+
86+
assert savedUser.getId() != null;
87+
assert savedUser.getVersion() != null;
88+
assert savedUser.getCart() != null;
89+
assert savedUser.getCart().getId() != null;
90+
}
91+
92+
@Test
93+
public void testAddCartToUserWithCartDetails() throws Exception {
94+
User user = new User();
95+
96+
user.setUsername("someusername");
97+
user.setPassword("myPassword");
98+
99+
user.setCart(new Cart());
100+
101+
List<Product> storedProducts = (List<Product>) productService.listAll();
102+
103+
CartDetail cartItemOne = new CartDetail();
104+
cartItemOne.setProduct(storedProducts.get(0));
105+
user.getCart().addCartDetail(cartItemOne);
106+
107+
CartDetail cartItemTwo = new CartDetail();
108+
cartItemTwo.setProduct(storedProducts.get(1));
109+
user.getCart().addCartDetail(cartItemTwo);
110+
111+
User savedUser = userService.saveOrUpdate(user);
112+
113+
assert savedUser.getId() != null;
114+
assert savedUser.getVersion() != null;
115+
assert savedUser.getCart() != null;
116+
assert savedUser.getCart().getId() != null;
117+
assert savedUser.getCart().getCartDetails().size() == 2;
118+
}
119+
120+
@Test
121+
public void testAddAndRemoveCartToUserWithCartDetails() throws Exception {
122+
User user = new User();
123+
124+
user.setUsername("someusername");
125+
user.setPassword("myPassword");
126+
127+
user.setCart(new Cart());
128+
129+
List<Product> storedProducts = (List<Product>) productService.listAll();
130+
131+
CartDetail cartItemOne = new CartDetail();
132+
cartItemOne.setProduct(storedProducts.get(0));
133+
user.getCart().addCartDetail(cartItemOne);
134+
135+
CartDetail cartItemTwo = new CartDetail();
136+
cartItemTwo.setProduct(storedProducts.get(1));
137+
user.getCart().addCartDetail(cartItemTwo);
138+
139+
User savedUser = userService.saveOrUpdate(user);
140+
141+
assert savedUser.getCart().getCartDetails().size() == 2;
142+
143+
savedUser.getCart().removeCartDetail(savedUser.getCart().getCartDetails().get(0));
144+
145+
userService.saveOrUpdate(savedUser);
146+
147+
assert savedUser.getCart().getCartDetails().size() == 1;
148+
}
67149
}

0 commit comments

Comments
 (0)