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

View File

@ -19,11 +19,6 @@ public class Cart {
@Column(name = "cart_id") @Column(name = "cart_id")
private long id; private long id;
@Column(name = "total")
private double total;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "cart") @OneToMany(mappedBy = "cart")
private Set<Items> items; private Set<Items> items;
@ -36,21 +31,6 @@ public class Cart {
this.id = id; 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() { public Set<Items> getItems() {
return items; return items;

View File

@ -18,14 +18,6 @@ public class Items {
@Column(name = "id") @Column(name = "id")
private long 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 @ManyToOne
@JoinColumn(name = "cart_id", nullable = false) @JoinColumn(name = "cart_id", nullable = false)
@ -35,37 +27,10 @@ public class Items {
public Items() { public Items() {
} }
public Items(String itemId, double total, int qty, Cart c) { public Items(Cart c) {
this.itemId = itemId;
this.itemTotal = total;
this.quantity = qty;
this.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() { public Cart getCart() {
return cart; return cart;
} }

View File

@ -1,16 +1,11 @@
CREATE TABLE `Cart` ( CREATE TABLE `Cart` (
`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`total` decimal(10,0) NOT NULL,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`cart_id`) PRIMARY KEY (`cart_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `Items` ( CREATE TABLE `Items` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cart_id` int(11) unsigned NOT NULL, `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`), PRIMARY KEY (`id`),
KEY `cart_id` (`cart_id`), KEY `cart_id` (`cart_id`),
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`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(); cartItems = cart.getItems();
Assert.assertNull(cartItems); Assert.assertNull(cartItems);
Items item1 = new Items(); Items item1 = new Items();
item1.setItemId("I10");
item1.setItemTotal(10);
item1.setQuantity(1);
item1.setCart(cart); item1.setCart(cart);
assertNotNull(item1); assertNotNull(item1);
Set<Items> itemsSet = new HashSet<Items>(); Set<Items> itemsSet = new HashSet<Items>();