Skip to content

Commit bd6fe72

Browse files
author
eugenp
committed
minor cleanup work - formatting
1 parent 4d56241 commit bd6fe72

5 files changed

Lines changed: 222 additions & 209 deletions

File tree

spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtil.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,31 @@
77

88
public class HibernateAnnotationUtil {
99

10-
private static SessionFactory sessionFactory;
11-
12-
private static SessionFactory buildSessionFactory() {
10+
private static SessionFactory sessionFactory;
11+
12+
private static SessionFactory buildSessionFactory() {
1313
try {
1414
// Create the SessionFactory from hibernate-annotation.cfg.xml
15-
Configuration configuration = new Configuration();
16-
configuration.configure("hibernate-annotation.cfg.xml");
17-
System.out.println("Hibernate Annotation Configuration loaded");
18-
19-
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
20-
System.out.println("Hibernate Annotation serviceRegistry created");
21-
22-
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
23-
24-
15+
Configuration configuration = new Configuration();
16+
configuration.configure("hibernate-annotation.cfg.xml");
17+
System.out.println("Hibernate Annotation Configuration loaded");
18+
19+
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
20+
System.out.println("Hibernate Annotation serviceRegistry created");
21+
22+
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
23+
2524
return sessionFactory;
26-
}
27-
catch (Throwable ex) {
25+
} catch (Throwable ex) {
2826
System.err.println("Initial SessionFactory creation failed." + ex);
2927
ex.printStackTrace();
3028
throw new ExceptionInInitializerError(ex);
3129
}
3230
}
33-
34-
public static SessionFactory getSessionFactory() {
35-
if(sessionFactory == null) sessionFactory = buildSessionFactory();
31+
32+
public static SessionFactory getSessionFactory() {
33+
if (sessionFactory == null)
34+
sessionFactory = buildSessionFactory();
3635
return sessionFactory;
3736
}
3837
}

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

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,49 @@
1313

1414
public class HibernateOneToManyAnnotationMain {
1515

16-
public static void main(String[] args) {
17-
18-
Cart cart = new Cart();
19-
cart.setName("MyCart");
20-
21-
Items item1 = new Items("I10", 10, 1, cart);
22-
Items item2 = new Items("I20", 20, 2, cart);
23-
Set<Items> itemsSet = new HashSet<Items>();
24-
itemsSet.add(item1); itemsSet.add(item2);
25-
26-
cart.setItems(itemsSet);
27-
cart.setTotal(10*1 + 20*2);
28-
29-
SessionFactory sessionFactory = null;
30-
Session session = null;
31-
Transaction tx = null;
32-
try{
33-
//Get Session
34-
sessionFactory = HibernateAnnotationUtil.getSessionFactory();
35-
session = sessionFactory.getCurrentSession();
36-
System.out.println("Session created");
37-
//start transaction
38-
tx = session.beginTransaction();
39-
//Save the Model object
40-
session.save(cart);
41-
session.save(item1);
42-
session.save(item2);
43-
//Commit transaction
44-
tx.commit();
45-
System.out.println("Cart ID="+cart.getId());
46-
System.out.println("item1 ID="+item1.getId()+", Foreign Key Cart ID="+item1.getCart().getId());
47-
System.out.println("item2 ID="+item2.getId()+", Foreign Key Cart ID="+item1.getCart().getId());
48-
49-
}catch(Exception e){
50-
System.out.println("Exception occured. "+e.getMessage());
51-
e.printStackTrace();
52-
}finally{
53-
if(!sessionFactory.isClosed()){
54-
System.out.println("Closing SessionFactory");
55-
sessionFactory.close();
56-
}
57-
}
58-
}
16+
public static void main(String[] args) {
17+
18+
Cart cart = new Cart();
19+
cart.setName("MyCart");
20+
21+
Items item1 = new Items("I10", 10, 1, cart);
22+
Items item2 = new Items("I20", 20, 2, cart);
23+
Set<Items> itemsSet = new HashSet<Items>();
24+
itemsSet.add(item1);
25+
itemsSet.add(item2);
26+
27+
cart.setItems(itemsSet);
28+
cart.setTotal(10 * 1 + 20 * 2);
29+
30+
SessionFactory sessionFactory = null;
31+
Session session = null;
32+
Transaction tx = null;
33+
try {
34+
// Get Session
35+
sessionFactory = HibernateAnnotationUtil.getSessionFactory();
36+
session = sessionFactory.getCurrentSession();
37+
System.out.println("Session created");
38+
// start transaction
39+
tx = session.beginTransaction();
40+
// Save the Model object
41+
session.save(cart);
42+
session.save(item1);
43+
session.save(item2);
44+
// Commit transaction
45+
tx.commit();
46+
System.out.println("Cart ID=" + cart.getId());
47+
System.out.println("item1 ID=" + item1.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
48+
System.out.println("item2 ID=" + item2.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
49+
50+
} catch (Exception e) {
51+
System.out.println("Exception occured. " + e.getMessage());
52+
e.printStackTrace();
53+
} finally {
54+
if (!sessionFactory.isClosed()) {
55+
System.out.println("Closing SessionFactory");
56+
sessionFactory.close();
57+
}
58+
}
59+
}
5960

6061
}

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

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,53 @@
1111
import javax.persistence.Table;
1212

1313
@Entity
14-
@Table(name="CART")
14+
@Table(name = "CART")
1515
public class Cart {
1616

17-
@Id
18-
@GeneratedValue(strategy=GenerationType.IDENTITY)
19-
@Column(name="cart_id")
20-
private long id;
21-
22-
@Column(name="total")
23-
private double total;
24-
25-
@Column(name="name")
26-
private String name;
27-
28-
@OneToMany(mappedBy="cart")
29-
private Set<Items> items;
30-
31-
public long getId() {
32-
return id;
33-
}
34-
public void setId(long id) {
35-
this.id = id;
36-
}
37-
public double getTotal() {
38-
return total;
39-
}
40-
public void setTotal(double total) {
41-
this.total = total;
42-
}
43-
public String getName() {
44-
return name;
45-
}
46-
public void setName(String name) {
47-
this.name = name;
48-
}
49-
public Set<Items> getItems() {
50-
return items;
51-
}
52-
public void setItems(Set<Items> items) {
53-
this.items = items;
54-
}
55-
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.IDENTITY)
19+
@Column(name = "cart_id")
20+
private long id;
21+
22+
@Column(name = "total")
23+
private double total;
24+
25+
@Column(name = "name")
26+
private String name;
27+
28+
@OneToMany(mappedBy = "cart")
29+
private Set<Items> items;
30+
31+
public long getId() {
32+
return id;
33+
}
34+
35+
public void setId(long id) {
36+
this.id = id;
37+
}
38+
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+
}
54+
55+
public Set<Items> getItems() {
56+
return items;
57+
}
58+
59+
public void setItems(Set<Items> items) {
60+
this.items = items;
61+
}
62+
5663
}

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

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,76 @@
1010
import javax.persistence.Table;
1111

1212
@Entity
13-
@Table(name="ITEMS")
13+
@Table(name = "ITEMS")
1414
public class Items {
1515

16-
@Id
17-
@GeneratedValue(strategy=GenerationType.IDENTITY)
18-
@Column(name="id")
19-
private long id;
20-
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;
29-
30-
@ManyToOne
31-
@JoinColumn(name="cart_id", nullable=false)
32-
private Cart cart;
33-
34-
//Hibernate requires no-args constructor
35-
public Items(){}
36-
37-
public Items(String itemId, double total, int qty, Cart c){
38-
this.itemId=itemId;
39-
this.itemTotal=total;
40-
this.quantity=qty;
41-
this.cart=c;
42-
}
43-
public String getItemId() {
44-
return itemId;
45-
}
46-
public void setItemId(String itemId) {
47-
this.itemId = itemId;
48-
}
49-
public double getItemTotal() {
50-
return itemTotal;
51-
}
52-
public void setItemTotal(double itemTotal) {
53-
this.itemTotal = itemTotal;
54-
}
55-
public int getQuantity() {
56-
return quantity;
57-
}
58-
public void setQuantity(int quantity) {
59-
this.quantity = quantity;
60-
}
61-
public Cart getCart() {
62-
return cart;
63-
}
64-
public void setCart(Cart cart) {
65-
this.cart = cart;
66-
}
67-
public long getId() {
68-
return id;
69-
}
70-
public void setId(long id) {
71-
this.id = id;
72-
}
73-
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
@Column(name = "id")
19+
private long id;
20+
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;
29+
30+
@ManyToOne
31+
@JoinColumn(name = "cart_id", nullable = false)
32+
private Cart cart;
33+
34+
// Hibernate requires no-args constructor
35+
public Items() {
36+
}
37+
38+
public Items(String itemId, double total, int qty, Cart c) {
39+
this.itemId = itemId;
40+
this.itemTotal = total;
41+
this.quantity = qty;
42+
this.cart = c;
43+
}
44+
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+
69+
public Cart getCart() {
70+
return cart;
71+
}
72+
73+
public void setCart(Cart cart) {
74+
this.cart = cart;
75+
}
76+
77+
public long getId() {
78+
return id;
79+
}
80+
81+
public void setId(long id) {
82+
this.id = id;
83+
}
84+
7485
}

0 commit comments

Comments
 (0)