Modification to Model Hibernate One to Many Tutorial (#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
This commit is contained in:
Chima Ejiofor 2017-02-08 16:35:33 +01:00 committed by maibin
parent c39a596981
commit 0bbfa1e10b
5 changed files with 4 additions and 68 deletions

View File

@ -16,16 +16,15 @@ public class HibernateOneToManyAnnotationMain {
public static void main(String[] args) {
Cart cart = new Cart();
cart.setName("MyCart");
Items item1 = new Items("I10", 10, 1, cart);
Items item2 = new Items("I20", 20, 2, cart);
Items item1 = new Items(cart);
Items item2 = new Items(cart);
Set<Items> itemsSet = new HashSet<Items>();
itemsSet.add(item1);
itemsSet.add(item2);
cart.setItems(itemsSet);
cart.setTotal(10 * 1 + 20 * 2);
SessionFactory sessionFactory = null;
Session session = null;

View File

@ -19,11 +19,6 @@ public class Cart {
@Column(name = "cart_id")
private long id;
@Column(name = "total")
private double total;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "cart")
private Set<Items> items;
@ -36,21 +31,6 @@ public class Cart {
this.id = id;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Items> getItems() {
return items;

View File

@ -18,14 +18,6 @@ public class Items {
@Column(name = "id")
private long id;
@Column(name = "item_id")
private String itemId;
@Column(name = "item_total")
private double itemTotal;
@Column(name = "quantity")
private int quantity;
@ManyToOne
@JoinColumn(name = "cart_id", nullable = false)
@ -35,37 +27,10 @@ public class Items {
public Items() {
}
public Items(String itemId, double total, int qty, Cart c) {
this.itemId = itemId;
this.itemTotal = total;
this.quantity = qty;
public Items(Cart c) {
this.cart = c;
}
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public double getItemTotal() {
return itemTotal;
}
public void setItemTotal(double itemTotal) {
this.itemTotal = itemTotal;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Cart getCart() {
return cart;
}

View File

@ -1,16 +1,11 @@
CREATE TABLE `Cart` (
`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`total` decimal(10,0) NOT NULL,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`cart_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `Items` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cart_id` int(11) unsigned NOT NULL,
`item_id` varchar(10) NOT NULL,
`item_total` decimal(10,0) NOT NULL,
`quantity` int(3) NOT NULL,
PRIMARY KEY (`id`),
KEY `cart_id` (`cart_id`),
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`)

View File

@ -63,9 +63,6 @@ public class HibernateOneToManyAnnotationMainTest {
cartItems = cart.getItems();
Assert.assertNull(cartItems);
Items item1 = new Items();
item1.setItemId("I10");
item1.setItemTotal(10);
item1.setQuantity(1);
item1.setCart(cart);
assertNotNull(item1);
Set<Items> itemsSet = new HashSet<Items>();