Creating Yoga Centre
The next step would be defining the backend structure. Instead of creating tables manually in the database, we would be using Spring JPA. Spring JPA helps generate tables at the backend database using the classes which are annotated with @Entity. This makes our code database independent because the JPA layer takes care of the syntax as per the database we choose.
YogaUser, table name = yoga_users
We will create an Entity class (“YogaUser”) and annotate it with @Entity to let JPA know that this class needs to be converted into a table in the database.
• Generally, the class name becomes the table name. Spring by default uses
org.springframework.boot.orm.jpa.SpringNamingStrategy splits camel case names with an underscore. E.g. YogaUser will be transformed to yoga_user. But if we want to define a custom table name then we can use @Table annotation. Like @Table(name=”yoga_user_master”)
• The variables of this class are columns of the table and are named using the same camelcase to underscore strategy,
e.g. UserId will become user_id column and userName will become user_name and likewise. But if we want to define a custom column name, we can use
• @Data is a Lombok annotation that provides the getters and setters for all the variables implicitly to avoid the boilerplate code i.e. we don’t have to write them manually.
• Next, we have to define an Identifier. Identifiers represent the primary key of an entity. This implies that the values are unique so that they can identify a specific entity, that they aren't null, and that they won't be modified. The most straightforward way to define an identifier is by using the @Id annotation.
If we want to automatically generate the primary key value, we can add the @GeneratedValue annotation.
This can use four generation types: AUTO, IDENTITY, SEQUENCE, and TABLE. Apart from these, we can also use CUSTOM.
Here we would be using the UUID generator strategy to keep it simple.
@GenericGenerator is a hibernate annotation used to denote a custom generator, which can be a class or shortcut to a generator supplied by Hibernate. We can use a predefined class or custom class (generator) for the generation of our primary key.
There are 2 main attributes of a generic generator:
a) name: It is the name of the generator which will be further used in “@GeneratedValue(generator = “ ” )”.
b) strategy: It is used to define the name of the custom class being used. It is recommended to provide the class name along with the package when using a custom class.
The @GeneratedValue annotation specifies how to generate values for the given column. This is primarily intended for primary key fields. So, this annotation contains some strategies that are used to generate the value of a column automatically.
If we don't explicitly specify a value, the generation type defaults to AUTO.
Some attributes of @GeneratedValue are:
strategy: This attribute is used to generate value with the help of any four generation types.
The four-generation types of strategy are:
a) AUTO: It indicates that the persistence provider should select an appropriate strategy based on the specified database dialect. The persistence provider will determine values based on the type of the primary key attribute. This type can be numerical or UUID.
b) IDENTITY: It indicates that the persistence provider must assign primary keys for the entities using a database identity column.
c) SEQUENCE: It indicates that the persistence provider must assign primary keys for the entities using the database sequence.
d) TABLE: It uses an underlying database table that holds segments of identifier generation values.
generator: The name of the primary key generator to use as specified in the Sequence generator or Table generator annotation. Here, we can also provide the name defined at @GenericGenerator.
In the above code, we have used the “@Column” annotation provided by “javax.persistence”. Here, we have set the “nullable” value as false for some of the attributes. This means that the value of these attributes cannot be null or empty.
Similarly, we will create the table “events”, “yoga_members” and “event_bookings”.
2. Entity Relationships:
@OneToMany: One-to-many mapping means that one instance of an entity is mapped with many instances of another entity. In terms of DB, it's like one row in a table is mapped to multiple rows in another table.
@Many-to-One: A many-to-one mapping means that many instances of this entity are mapped to one instance of another entity.
@Many-to-many: A many-to-many mapping means that many instances of this entity are mapped to many instances of another entity.
The “events” table will have a one-to-many relationship with the “event_bookings” table as one event will have multiple event_bookings.
• We will define EventBooking as a List and annotate it with “@OneToMany”.
• Here, we will use the “mappedBy” parameter to indicate that this column is referred to by another entity and will put the value as “event” which we had defined in the EventBooking class.
Similarly, we will also create a “yoga_members” table and use the “@OneToMany” annotation to create its relation with the EventBooking entity.
One member can apply for multiple events. So we will use the “@ManyToOne” annotation in the EventBooking class as multiple events will correspond to one member.
We will declare the “YogaMember” variable as “yogaMember”, this will be further used to map the entities. We will define a variable “eventBookingDate” with the definition as “DATETIME” and also define “@DateTimeFormat(style = "yyyy-MM-dd HH:mm:ss")” so that any input will be considered in the same format.
The primary key of the “yoga_members” table (“memberId”) will be the foreign key of “event_bookings”. In order to achieve this relationship, we will use the “@JoinColumn” annotation in the “EventBooking” class and pass “memberId” as the name in its parameter.
Similarly, we will also create a relation between Event Bookings and Event. Here, one event will have multiple event bookings.