Skip to content

Commit 866f3b4

Browse files
committed
add a controller and models
1 parent 747abd8 commit 866f3b4

File tree

4 files changed

+246
-3
lines changed

4 files changed

+246
-3
lines changed
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
package com.example;
22

3+
import java.math.BigDecimal;
4+
import java.util.Arrays;
5+
6+
import org.springframework.boot.CommandLineRunner;
37
import org.springframework.boot.SpringApplication;
48
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
import com.example.model.Base;
12+
import com.example.model.Pizza;
13+
import com.example.model.Topping;
514

615
@SpringBootApplication
716
public class DemoEbeanApplication {
817

9-
public static void main(String[] args) {
10-
SpringApplication.run(DemoEbeanApplication.class, args);
11-
}
18+
public static void main(String[] args) {
19+
SpringApplication.run(DemoEbeanApplication.class, args);
20+
}
21+
22+
@Bean
23+
CommandLineRunner runner(PizzaRepository pizzaRepository) {
24+
return a -> {
25+
Pizza pizza = new Pizza();
26+
pizza.setBase(new Base(1L));
27+
pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L)));
28+
pizza.setName("Hello Pizza");
29+
pizza.setPrice(new BigDecimal("1000"));
30+
pizzaRepository.save(pizza);
31+
};
32+
}
1233
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.example.model;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
6+
@Entity
7+
public class Base implements Serializable {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private Long id;
11+
private String name;
12+
13+
public Base(Long id) {
14+
this.id = id;
15+
}
16+
17+
Base() {
18+
19+
}
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "Base{" +
40+
"id=" + id +
41+
", name='" + name + '\'' +
42+
'}';
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (this == o) return true;
48+
if (!(o instanceof Base)) return false;
49+
50+
Base base = (Base) o;
51+
52+
if (id != null ? !id.equals(base.id) : base.id != null) return false;
53+
return name != null ? name.equals(base.name) : base.name == null;
54+
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
int result = id != null ? id.hashCode() : 0;
60+
result = 31 * result + (name != null ? name.hashCode() : 0);
61+
return result;
62+
}
63+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.example.model;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
import java.math.BigDecimal;
6+
import java.util.List;
7+
8+
@Entity
9+
public class Pizza implements Serializable {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
12+
private Long id;
13+
private String name;
14+
private BigDecimal price;
15+
@ManyToMany
16+
private List<Topping> toppings;
17+
@ManyToOne
18+
private Base base;
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public BigDecimal getPrice() {
37+
return price;
38+
}
39+
40+
public void setPrice(BigDecimal price) {
41+
this.price = price;
42+
}
43+
44+
public List<Topping> getToppings() {
45+
return toppings;
46+
}
47+
48+
public void setToppings(List<Topping> toppings) {
49+
this.toppings = toppings;
50+
}
51+
52+
public Base getBase() {
53+
return base;
54+
}
55+
56+
public void setBase(Base base) {
57+
this.base = base;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "Pizza{" +
63+
"id=" + id +
64+
", name='" + name + '\'' +
65+
", price=" + price +
66+
", toppings=" + toppings +
67+
", base=" + base +
68+
'}';
69+
}
70+
71+
@Override
72+
public boolean equals(Object o) {
73+
if (this == o) return true;
74+
if (!(o instanceof Pizza)) return false;
75+
76+
Pizza pizza = (Pizza) o;
77+
78+
if (id != null ? !id.equals(pizza.id) : pizza.id != null) return false;
79+
if (name != null ? !name.equals(pizza.name) : pizza.name != null) return false;
80+
if (price != null ? !price.equals(pizza.price) : pizza.price != null) return false;
81+
if (toppings != null ? !toppings.equals(pizza.toppings) : pizza.toppings != null) return false;
82+
return base != null ? base.equals(pizza.base) : pizza.base == null;
83+
84+
}
85+
86+
@Override
87+
public int hashCode() {
88+
int result = id != null ? id.hashCode() : 0;
89+
result = 31 * result + (name != null ? name.hashCode() : 0);
90+
result = 31 * result + (price != null ? price.hashCode() : 0);
91+
result = 31 * result + (toppings != null ? toppings.hashCode() : 0);
92+
result = 31 * result + (base != null ? base.hashCode() : 0);
93+
return result;
94+
}
95+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.example.model;
2+
3+
import javax.persistence.*;
4+
import java.io.Serializable;
5+
6+
@Entity
7+
public class Topping implements Serializable {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private Long id;
11+
private String name;
12+
13+
public Topping(Long id) {
14+
this.id = id;
15+
}
16+
17+
Topping() {
18+
19+
}
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
38+
@Override
39+
public String toString() {
40+
return "Topping{" +
41+
"id=" + id +
42+
", name='" + name + '\'' +
43+
'}';
44+
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o) return true;
49+
if (o == null || getClass() != o.getClass()) return false;
50+
51+
Topping topping = (Topping) o;
52+
53+
if (id != null ? !id.equals(topping.id) : topping.id != null) return false;
54+
return name != null ? name.equals(topping.name) : topping.name == null;
55+
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
int result = id != null ? id.hashCode() : 0;
61+
result = 31 * result + (name != null ? name.hashCode() : 0);
62+
return result;
63+
}
64+
}

0 commit comments

Comments
 (0)