maintain mvc architecutre in Spring MVCf 3 - spring

I have two tables that have relationship like
tbl_department whose entities are dept_id and dept_name where dept_id is primary key and next table is tbl_employee whose entities are emp_id, emp_name and dept_id where dept_id is foreign key that references the dept_id of tbl_department. Now, in my jsp page i want to display the format like this:
Emp_id|Emp_name|dept_name.
I am doing above display in JSP page using the JSTL sql tag, and I know it violates the MVC architecture, so, how could I solve this problem i.e. how can I do this relationship mapping it in either Model or Controller in Spring instead in JSP page. Is there any examples or tutorial I can find for this kind of problem
Thank you

First, you have to decide whether it would be worth your while using Hibernate. You should research this yourself to decide whether the rest of your app would benefit from using it (too big a topic to address here).
Assuming you only want to do the use case you have outlined in the question, I'd suggest you use Spring's JDBC support and create a Service (or just a DAO) to perform the SQL that you are currently using in your JSP. I'd create a simple DTO class to hold the Emp_id|Emp_name|dept_name data and populate that in your Service/DAO and then pass it to the JSP via the Controller.
This Spring MVC Example (skeleton) app would be a good place to start, to see how those components are created and wired together.

Related

Spring Boot how i can add Entity in table?

pls help me understand how i can add Entity in table.
(Use Tutorial) I create Spring boot app - login/registration/home - with Security, SpringJpa, Hibernate and Thymeleaf. Tutorial it is always cool, but can i do something without that?
I decide to expend my app, and understand that i am real beginner.
So, my problem, i have home.html page, with Courses (three courses), that i want to add in my new page cabinet.html one of this course. New page have empty bootstrap table, but i want add course and see this entity in table!
link to view html page https://drive.google.com/drive/folders/1dnn-IKkpaBeouyqnGU13YLf1GBPVd_jQ?usp=sharing
My Question, after registration and login, what i need to write in backend, that on of my course appear after (click button add) in List Table in the Cabinet?
I think this is may be :
Course Entity (id, string name, string date) that saves to MySQL. Courses Repository. Courses service with method find-All or save? ServiceImpl, Controller, PostMapping with RequestParam?
And what thameleaf tegs i need to add in my table that field courses entity views in table? Pls help

Spring data with R2DBC(Mysql) Example for one to many association

Can I know how to do association(One to Many) in Spring data JDBC with R2DBC(Mysql).Please provide small code example or git link if possible.
For Example, I have one employee table and address table is child of employee.
One employee can have multiple addresses.
If I want to retrieve employee, I want address as well to corresponding employee as JSON.
Note: Software stack is Spring Functional Reactive, R2DBC with Mysql
Spring Data R2DBC currently does not support aggregates.
This means every entity gets mapped to just one table and can't have object references to other entities.
Therefore the correct way to model your example is to have no object reference between Empolyee and Address but have an employeeId in the Address and use that to select the desired addresses.

Save multiple row data and some other data into two tables simultaneously in spring hibernate

I want to save data of a form into two tables simultaneously. The form contains multiple identical rows with some fields like
item, unit, unit_price, total.
1.) In the table1 I would like to save some data like id(auto generated, primary key), date, creator.
2.) In table2 I would like to save the id(auto generated, primary key), fid(id of the table1, foreign key), item, unit, unit price, total. Remember, in table2 multiple rows will be save from the form data.
I can't figure it out. Please help me with a valid step by step example, and please don't use static or pre-defined data, take the data from form only.
Is your form an HTML form on a web page, and are you writing a Spring MVC application? Have you setup Spring Security, or some other authentication mechanism such that you know who the user is? If so, here is one approach:
Define a plain old Java object (POJO) with fields id, unit, unit-price, total, creator, currentDate; perhaps you would name it Item.
Define another plain old Java object perhaps named "ItemHistory", with its own ID and a reference to an Item.
In the first POJO ("Item") include a reference to a list of ItemHistorys.
Annotate both classes with Hibernate/JPA annotations, such that the Item has a OneToMany relationship to ItemHistory.
Define a Spring MVC controller with a method annotated as a POST method, with an Item as one parameter, and an Authentication as another parameter (supposing you have configured Spring Security, the Authentication will automatically be populated with the identity of the currently-logged-in user. In this method, set the currentDate and author of the Item to the current date and the principal from the Authentication parameter. If it is a new Item (no id yet), add the first ItemHistory as a copy of the Item itself. If it is an existing item, add another ItemHistory if needed.
Define a Spring bean, perhaps named ItemService, with a method annotated as transactional, that takes an Item and saves it. Call this bean from the REST controller. Such a Spring bean is known as a DAO (data access object).
There are other strategies, but if you already have Spring MVC, Spring Security, and Hibernate/JPA configured to your needs, this one is pretty straight-forward.

Validate data cannot be deleted if referenced by other table

I create REST API application using Spring & JPA using Spring Boot.
I've Employee table which referenced by many tables like Family, ID Card, etc (more than 5 tables) and implement soft delete (set mark_for_delete = true)
I want add validation if employee data still referenced by other table, i want throw exception that said "can not delete employee"
What is the best way to do this validation?
Thanks
You could use a table that stores the number of references that has an Employee.
Anytime you insert or update the Family, ID Card, etc you should update this. To make the validation just verify the number of references is 0.
Doing that way you can add more tables if necessary without change the validation code.

Data Annotation on multiple table in asp.net MVC 2

I want to implement data annotation on three tables Link, LinkRef and LinkMeta. Link is main table and other two have LinkId as primary key. Because the system is multi-lingual Link title will be in more than one language so as other details. No of languages will vary. I want to know can i use data annotation in this scenario and if yes then how? Im using Linq2SQL for creating model.
Can Data annotations be applied to class objects?

Resources