SLN Spotlight_SAP Labs India: Entity class overview in JPA based WEB applications
This is the continuation of previous blog(blog series) and it covers the Entity class created in JPA based web application that how POJO class created in JAVA can be mapped with the database tables, views and calculation views created in HANA DB.
Entity class created just like POJO class standard, but we need to take care few thing in order to map with the database. We are using here the annotation of persistence class to map it with the database table, few basic annotation which will be require to map it with the database is
@Entity: which identifies the class as Entity class,
@Table (name = <table name in DB>) : Which helps to map the POJO class with the database table and columns mapping has to be the same as it there in database along with the data type.
@Id: Identifies the column as primary key, as per the eclipselink documentation, whether or not database table has any primary key or not but any of the column must have this annotation, otherwise will get the compile time exception.
@Column: above the variable, maps with the table’s columns and in case of String we can define the length, default length is 255, if we don’t define the length and same goes with decimal also, we can define precision and scale also.
@OneToMany (cascade=CascadeType.ALL, orphanRemoval=true): In this annotation use to create foreign relation with the table, like in below example Employee table has order id column as well as order table has order id columns, so here CDS association has been created in and its cardinality is [1..*] as shown below. OrphanRemoval is true, when we want to delete any record from Employee table, will affect the order table also.
@OneToOne(cascade=CascadeType.ALL, orphanRemoval=true): In this annotation, when we want to have association between main table(e.g. Employee) and secondary table(e.g. Order table) then @One to One mapping is required.
@CascadeOnDelete: Annotation is used along with the mapping to delete records from respective mapped table.
@JoinColumn: Annotation is used to define the relationship between the main table and foreign table. It basically denotes the mapping between columns.
1.1. Following image,
As shown above, one-to-one, many-to-one can also be implemented.
1.3 Employee and Order Table in Database:
Here table is created by CDS (Core Data service) and association is also implemented [1…*].
Employee Table info.
Order table info.
So with above explanation, it would be understandable that how mapping of columns of database columns can be done with the Entity class variable and how one-to-one, one-to-many, many-to-one relationship can be created in entity class and in table. In the next blog, I will be covering about the adding the custom annotation and labels in the OData metadata.