Skip to content

Commit 0bbfa1e

Browse files
mobibluntmaibin
authored andcommitted
Modification to Model Hibernate One to Many Tutorial (eugenp#1132)
* Modifications to model on Hibernate One to manyTutorial * Modifications to model on Hibernate One to manyTutorial * Modifications to model on Hibernate One to manyTutorial
1 parent c39a596 commit 0bbfa1e

5 files changed

Lines changed: 4 additions & 68 deletions

File tree

‎spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ public class HibernateOneToManyAnnotationMain {
1616
public static void main(String[] args) {
1717

1818
Cart cart = new Cart();
19-
cart.setName("MyCart");
2019

21-
Items item1 = new Items("I10", 10, 1, cart);
22-
Items item2 = new Items("I20", 20, 2, cart);
20+
Items item1 = new Items(cart);
21+
Items item2 = new Items(cart);
2322
Set<Items> itemsSet = new HashSet<Items>();
2423
itemsSet.add(item1);
2524
itemsSet.add(item2);
2625

2726
cart.setItems(itemsSet);
28-
cart.setTotal(10 * 1 + 20 * 2);
27+
2928

3029
SessionFactory sessionFactory = null;
3130
Session session = null;

‎spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ public class Cart {
1919
@Column(name = "cart_id")
2020
private long id;
2121

22-
@Column(name = "total")
23-
private double total;
24-
25-
@Column(name = "name")
26-
private String name;
2722

2823
@OneToMany(mappedBy = "cart")
2924
private Set<Items> items;
@@ -36,21 +31,6 @@ public void setId(long id) {
3631
this.id = id;
3732
}
3833

39-
public double getTotal() {
40-
return total;
41-
}
42-
43-
public void setTotal(double total) {
44-
this.total = total;
45-
}
46-
47-
public String getName() {
48-
return name;
49-
}
50-
51-
public void setName(String name) {
52-
this.name = name;
53-
}
5434

5535
public Set<Items> getItems() {
5636
return items;

‎spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java‎

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ public class Items {
1818
@Column(name = "id")
1919
private long id;
2020

21-
@Column(name = "item_id")
22-
private String itemId;
23-
24-
@Column(name = "item_total")
25-
private double itemTotal;
26-
27-
@Column(name = "quantity")
28-
private int quantity;
2921

3022
@ManyToOne
3123
@JoinColumn(name = "cart_id", nullable = false)
@@ -35,37 +27,10 @@ public class Items {
3527
public Items() {
3628
}
3729

38-
public Items(String itemId, double total, int qty, Cart c) {
39-
this.itemId = itemId;
40-
this.itemTotal = total;
41-
this.quantity = qty;
30+
public Items(Cart c) {
4231
this.cart = c;
4332
}
4433

45-
public String getItemId() {
46-
return itemId;
47-
}
48-
49-
public void setItemId(String itemId) {
50-
this.itemId = itemId;
51-
}
52-
53-
public double getItemTotal() {
54-
return itemTotal;
55-
}
56-
57-
public void setItemTotal(double itemTotal) {
58-
this.itemTotal = itemTotal;
59-
}
60-
61-
public int getQuantity() {
62-
return quantity;
63-
}
64-
65-
public void setQuantity(int quantity) {
66-
this.quantity = quantity;
67-
}
68-
6934
public Cart getCart() {
7035
return cart;
7136
}

‎spring-hibernate4/src/main/resources/one_to_many.sql‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
CREATE TABLE `Cart` (
22
`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
3-
`total` decimal(10,0) NOT NULL,
4-
`name` varchar(10) DEFAULT NULL,
53
PRIMARY KEY (`cart_id`)
64
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
75

86
CREATE TABLE `Items` (
97
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
108
`cart_id` int(11) unsigned NOT NULL,
11-
`item_id` varchar(10) NOT NULL,
12-
`item_total` decimal(10,0) NOT NULL,
13-
`quantity` int(3) NOT NULL,
149
PRIMARY KEY (`id`),
1510
KEY `cart_id` (`cart_id`),
1611
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`)

‎spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ public void givenSession_checkIfDatabaseIsPopulated_afterCommit() {
6363
cartItems = cart.getItems();
6464
Assert.assertNull(cartItems);
6565
Items item1 = new Items();
66-
item1.setItemId("I10");
67-
item1.setItemTotal(10);
68-
item1.setQuantity(1);
6966
item1.setCart(cart);
7067
assertNotNull(item1);
7168
Set<Items> itemsSet = new HashSet<Items>();

0 commit comments

Comments
 (0)