Steps For Creating Spring Boot Application
Steps For Creating Spring Boot Application
1. Spring initializr :
- Group.
– Artifact.
– Dependencies (Spring Web: RESTfull, Spring Data JPA: persistence, Thymeleaf: HTML,
MySQL Driver: database).
2. Create the database and its tables.
3. Creating packages :
- Model: It must have @Entity in the first and @Id @GeneratedValue(strategy =
GenerationType.IDENTITY) for the ID that auto increments.
One-to-One Relationship :
. The entity with the foreign key in its table is the child.
. The parent (artist) should use @OneToOne(mappedBy = “artist”,
cascade = CascadeType.ALL, fetch = FetchType.LAZY). The key here is
mappedBy. This tells JPA that rankings are mapped by the artist entity on the
ranking object.
. The child (ranking) should have @OneToOne and
@JoinColumn(name = “artist_id”). This tells JPA that Artist is mapped to
ranking via the artist_id.
One-to-Many Relationship:
. The entity with the foreign key in its table is the child.
. The parent (artist) should use @OneToMany(mappedBy = “artist”,
cascade = CascadeType.ALL, fetch = FetchType.LAZY). The key here is
mappedBy. This tells JPA that rankings are mapped by the artist entity on
the ranking object.
. The child (recording) should have @ManyToOne and
@JoinColumn(name = “artist_id”). This tells JPA that Artist is mapped to
ranking via the artist_id.
Many-to-Many Relationship:
. Choose one of the entities, doesn’t matter which one, to hold the
@JoinTable, consider this the parent entity.
.The parent (recording) must define its own foreign key as the join
column joinColumns = { @JoinColumn(name = “recording_id”) },.
.The parent (recording) entity must define the child as the
inverseJoinColumns. inverseJoinColumns = { @JoinColumn(name =
“release_id”)
.The child (release) uses @ManyToMany(mappedBy = “releases”) to
map the parent to itself.
Source : https://tenmilesquare.com/resources/software-development/spring-boot-jpa-relationship-
quick-guide/
Hint: ModelAndView class merely holds both to make it possible for a controller to return
both model and view in a single return value. For that we specify the name of the html file in
its constructor (ex : ModelAndView modelAndView = new ModelAndView("edit");) and add
the model to it as an object (ex: modelAndView.addObject("student", student);).
spring.jpa.hibernate.ddl-auto=update
server.error.whitelabel.enabled=false
server.port=8060
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false
5. Time for the front-end:
- First, html files should be in the templates folder and assets in static folder.
- We have to use the name space (xmlns:th=http://www.thymeleaf.org) it’s like taglibs:
. <a th:href="@{'/new'}"> : calls controller’s method that has new in the mapping
. <a th:href="@{'/edit/'+${student.id}}"> : like the previous, plus, it sends a variable to it
(see @PathVariable).
. <tr th:each="student:${studentlist}"> : to iterate all over a list
. <td th:text="${student.id}"> : to get an attribute value of an instance
. There is two syntaxes to accede to an attribute value of an instance:
Dollar Syntax : ${student.id}
Asterisk Syntax: where we have to select the object (instance) first by th:object =
"${student}" , then we accede to its attribute like this th:utext="*{id}" or
th:field="*{id}" (this syntax is usually used in forms).
Hint: th:utext="${student.id}" and th:utext="*{student.id}" are equivalent.
Docker
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default
spring.jpa.show-sql=true
spring.datasource.continue-on-error=false
spring.datasource.generate-unique-name=false
spring.datasource.username=app
spring.mvc.pathmatch.matching-strategy=ant-path-matcher