-
Notifications
You must be signed in to change notification settings - Fork 0
/
new
1334 lines (1181 loc) · 37.9 KB
/
new
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.model.Employee;
import com.model.EmployeeHobby;
import com.model.EmployeeSkill;
import com.service.EmployeeHobbyService;
import com.service.EmployeeService;
import com.service.EmployeeSkillService;
@Controller
public class MainController
{
@Autowired
EmployeeService employeeService;
@Autowired
EmployeeHobbyService employeeHobbyService;
@Autowired
EmployeeSkillService employeeSkillService;
public List<EmployeeHobby> getEmployeeHobby()
{
List<EmployeeHobby> hobby= new ArrayList<>();
hobby.add(new EmployeeHobby("cricket"));
hobby.add(new EmployeeHobby("hockey"));
hobby.add(new EmployeeHobby("football"));
hobby.add(new EmployeeHobby("vollyball"));
return hobby;
}
public List<EmployeeSkill> getEmployeeSkill()
{
List<EmployeeSkill> skills=new ArrayList<>();
skills.add(new EmployeeSkill("C"));
skills.add(new EmployeeSkill("C++"));
skills.add(new EmployeeSkill("JAVA"));
skills.add(new EmployeeSkill("PHP"));
skills.add(new EmployeeSkill(".NET"));
return skills;
}
@RequestMapping(value="/")
public ModelAndView getHome(ModelAndView model)
{
List<Employee> employess=employeeService.getAllEmployee();
List<EmployeeHobby> hobbies=employeeHobbyService.getAllHobby();
List<EmployeeSkill> skills=employeeSkillService.getAllSkill();
model.addObject("employess", employess);
model.addObject("hobbies", hobbies);
model.addObject("skills",skills);
model.setViewName("home");
return model;
}
@RequestMapping(value="/newEmployee",method = RequestMethod.GET)
public ModelAndView newEmployee(ModelAndView model)
{
/*for Employee hobby checkbox*/
List<EmployeeHobby> hobby= getEmployeeHobby();
/*for Employee Skills listbox*/
List<EmployeeSkill> skill=getEmployeeSkill();
/*for Employee Country dropdown*/
model.addObject("hobby",hobby);
model.addObject("skill",skill);
Employee employee = new Employee();
model.addObject("employee",employee);
model.setViewName("EmployeeForm");
return model;
}
@RequestMapping(value="/editEmployee")
public ModelAndView editEmployee(HttpServletRequest req)
{
ModelAndView model=new ModelAndView("EmployeeForm");
int eid=Integer.parseInt(req.getParameter("eid"));
Employee employee=employeeService.getEmployeeById(eid);
List<EmployeeHobby> hobby= getEmployeeHobby();
List<EmployeeSkill> skill=getEmployeeSkill();
List<EmployeeSkill> empskill=employeeSkillService.getSkillsForEmployee(eid);
List<EmployeeHobby> emphobby=employeeHobbyService.getHobbiesForEmployee(eid);
employee.setEmployeeskill(empskill);
model.addObject("hobby",hobby);
model.addObject("skill",skill);
model.addObject("employee", employee);
model.addObject("emphobby",emphobby);
model.addObject("empskill", empskill);
return model;
}
@RequestMapping(value="/saveEmployee",method=RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute Employee employee)
{
ModelAndView model=new ModelAndView();
/* System.out.println(employee.toString());*/
List<EmployeeHobby> hobby=employee.getEmployeehobby();
List<EmployeeSkill> skills=employee.getEmployeeskill();
/*System.out.println(hobby);*/
for(EmployeeHobby h:hobby)
h.setEmployee(employee);
for(EmployeeSkill s:skills)
s.setEmployee(employee);
if(employee.getEid()==0)
{
employeeService.insertEmployee(employee);
employeeHobbyService.insertHobbies(hobby);
employeeSkillService.insertSkills(skills);
}
else
{
employeeService.updateEmployee(employee);
employeeHobbyService.updateHobbies(hobby, employee.getEid());
employeeSkillService.updateSkills(skills,employee.getEid() );
}
model.setViewName("redirect:/");
return model;
}
@RequestMapping("/deleteEmployee")
public ModelAndView deleteEmployee(HttpServletRequest req)
{
ModelAndView model=new ModelAndView();
int eid=Integer.parseInt(req.getParameter("eid"));
employeeService.deleteEmployee(eid);
model.setViewName("redirect:/");
return model;
}
@RequestMapping(value="/searchEmployee")
public ModelAndView searchEmployee(ModelAndView model)
{
Employee e=new Employee();
List<EmployeeHobby> hobby=getEmployeeHobby();
List<EmployeeSkill> skill=getEmployeeSkill();
model.addObject("hobby", hobby);
model.addObject("skill", skill);
model.addObject("employee", e);
model.setViewName("SearchEmployee");
return model;
}
@RequestMapping(value="/search",method=RequestMethod.POST)
public ModelAndView searchResult(@ModelAttribute Employee employee)
{
ModelAndView model=new ModelAndView("SearchEmployee");
List<EmployeeHobby> hobby=getEmployeeHobby();
List<EmployeeSkill> skill=getEmployeeSkill();
model.addObject("hobby", hobby);
model.addObject("skill", skill);
List<Employee> list=employeeService.searchByEmployee(employee);
model.addObject("listEmployee", list);
return model;
}
}
-------------
package com.dao;
import java.util.List;
import com.model.Employee;
public interface EmployeeDao
{
public void insertEmployee(Employee employee);
public Employee getEmployeeById(int eid);
public List<Employee> getAllEmployee();
public Employee updateEmployee(Employee employee);
public void deleteEmployee(int eid);
public List<Employee> searchByEmployee(Employee employee);
}
-----------------
package com.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.model.Employee;
import com.model.EmployeeHobby;
import com.model.EmployeeSkill;
import com.service.EmployeeHobbyService;
import com.service.EmployeeSkillService;
@Repository
public class EmployeeDaoImpl implements EmployeeDao{
@Autowired
SessionFactory sessionFactory;
@Autowired
EmployeeHobbyService employeeHobbyService;
@Autowired
EmployeeSkillService employeeSkillService;
@Transactional
public void insertEmployee(Employee employee) {
sessionFactory.getCurrentSession().save(employee);
this.sessionFactory.close();
}
@Transactional
public Employee getEmployeeById(int eid) {
Employee e=(Employee) sessionFactory.getCurrentSession().get(Employee.class,eid);
this.sessionFactory.close();
return e;
}
@SuppressWarnings("unchecked")
@Transactional
public List<Employee> getAllEmployee() {
List<Employee> em=sessionFactory.getCurrentSession().createQuery(" from Employee").list();
this.sessionFactory.close();
return em;
}
@Transactional
public Employee updateEmployee(Employee employee) {
sessionFactory.getCurrentSession().update(employee);
this.sessionFactory.close();
return employee;
}
@SuppressWarnings({ "unused", "unchecked" })
@Transactional
public void deleteEmployee(int eid) {
Employee emp=(Employee)sessionFactory.getCurrentSession().load(Employee.class, eid);
List<EmployeeHobby> eh=sessionFactory.getCurrentSession().createQuery("from EmployeeHobby where employee=:emp").setParameter("emp", emp).list();
for(EmployeeHobby h:eh)
this.sessionFactory.getCurrentSession().delete(h);
List<EmployeeSkill> es=sessionFactory.getCurrentSession().createQuery("from EmployeeSkill where employee=:emp").setParameter("emp",emp).list();
for(EmployeeSkill s:es)
this.sessionFactory.getCurrentSession().delete(s);
this.sessionFactory.getCurrentSession().delete(emp);
this.sessionFactory.close();
}
@SuppressWarnings("unchecked")
@Transactional
private List<Employee> getEmployee(Employee employee)
{
StringBuffer qry=new StringBuffer();
int flag=0;
qry.append("from Employee ");
if(employee.getEid() != 0 && flag == 0)
{
flag=1;
qry.append(" where eid=:eid ");
}
else if(employee.getEid() != 0 && flag == 1)
{
flag=1;
qry.append(" and eid=:eid");
}
if(employee.getEname() != "" && flag == 1)
{
qry.append(" and ename=:ename ");
}
else if(employee.getEname() != "" && flag == 0)
{
flag=1;
qry.append(" where ename = :ename ");
}
if(employee.getSalary() != 0 && flag ==1)
{
qry.append(" and salary=:salary ");
}
else if(employee.getSalary() !=0 && flag == 0)
{
flag=1;
qry.append(" where salary=:salary ");
}
if(employee.getGender() != null && flag ==1)
{
qry.append(" and gender=:gender ");
}
else if(employee.getGender() != null && flag == 0)
{
flag=1;
qry.append(" where gender = :gender ");
}
if(employee.getEmployeehobby() != null && flag==1)
{
qry.append("and employeehobby = :employeehobby");
}
else if(employee.getEmployeehobby() != null && flag==0)
{
flag=1;
qry.append("where employeehobby = :employeehobby");
}
/*if(employee.getEmployeehobby() != null && flag !=0)
{
flag=1;
qry.append(" and employeehobby in (:hobbylist)");
}
*/
System.out.println(qry.toString());
Query q= this.sessionFactory.getCurrentSession().createQuery(qry.toString());
if(employee.getEid() != 0)
q.setParameter("eid", employee.getEid());
if(employee.getEname() != "")
q.setParameter("ename", employee.getEname());
if(employee.getSalary() != 0)
q.setParameter("salary", employee.getSalary());
if(employee.getGender() != null)
q.setParameter("gender", employee.getGender());
if(employee.getEmployeehobby() != null)
q.setParameterList("employeehobby", employee.getEmployeehobby());
List<Employee> list=q.list();
return list;
}
@Override
@Transactional
public List<Employee> searchByEmployee(Employee employee) {
List<Employee> employees=getEmployee(employee);
for(Employee emp:employees)
{
emp.setEmployeehobby(employeeHobbyService.getHobbiesForEmployee(emp.getEid()));
emp.setEmployeeskill(employeeSkillService.getSkillsForEmployee(emp.getEid()));
}
return employees;
}
}
--------
package com.dao;
import java.util.List;
import com.model.Employee;
import com.model.EmployeeHobby;
public interface EmployeeHobbyDao
{
public void insertHobbies(List<EmployeeHobby> hobbieslist);
public List<EmployeeHobby> getHobbiesForEmployee(int eid);
public List<EmployeeHobby> getAllHobby();
public void updateHobbies(List<EmployeeHobby> hobbieslist,int eid);
public List<EmployeeHobby> getHobbyFromObject(Employee e);
}
-----------------
package com.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.model.Employee;
import com.model.EmployeeHobby;
@Repository
public class EmployeeHobbyDaoImpl implements EmployeeHobbyDao {
@Autowired
SessionFactory sesssionFactory;
public void insertHobbies(List<EmployeeHobby> hobbieslist) {
for(EmployeeHobby h: hobbieslist)
{
sesssionFactory.getCurrentSession().save(h);
}
this.sesssionFactory.close();
}
@SuppressWarnings("unchecked")
public List<EmployeeHobby> getHobbiesForEmployee(int eid) {
List<EmployeeHobby> hobb=sesssionFactory.getCurrentSession().createQuery("select e from EmployeeHobby e join fetch e.employee where e.employee=:emp").setParameter("emp",(Employee)sesssionFactory.getCurrentSession().load(Employee.class, eid)).list();
this.sesssionFactory.close();
return hobb;
}
@SuppressWarnings({ "unused", "unchecked" })
public void updateHobbies(List<EmployeeHobby> hobbieslist,int eid) {
Employee e=(Employee)sesssionFactory.getCurrentSession().load(Employee.class, eid);
List<EmployeeHobby> eh=sesssionFactory.getCurrentSession().createQuery("from EmployeeHobby where employee=:emp").setParameter("emp", e).list();
for(EmployeeHobby h:eh)
{
this.sesssionFactory.getCurrentSession().delete(h);
}
for(EmployeeHobby h:hobbieslist)
{
sesssionFactory.getCurrentSession().save(h);
}
this.sesssionFactory.close();
}
@SuppressWarnings("unchecked")
public List<EmployeeHobby> getAllHobby() {
List<EmployeeHobby> h=sesssionFactory.getCurrentSession().createQuery("from EmployeeHobby").list();
this.sesssionFactory.close();
return h;
}
@SuppressWarnings("unchecked")
@Override
public List<EmployeeHobby> getHobbyFromObject(Employee e) {
return this.sesssionFactory.getCurrentSession().createQuery("from EmployeeHobby where employee=:emp").setParameter("emp", e).list();
}
}
----------------
package com.dao;
import java.util.List;
import com.model.EmployeeSkill;
public interface EmployeeSkillDao
{
public void insertSkills(List<EmployeeSkill> skilllist);
public List<EmployeeSkill> getSkillsForEmployee(int eid);
public List<EmployeeSkill> getAllSkill();
public void updateSkills(List<EmployeeSkill> skillslist,int eid);
}
---------------
package com.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.model.Employee;
import com.model.EmployeeSkill;
@Repository
public class EmployeeSkillDaoImpl implements EmployeeSkillDao {
@Autowired
SessionFactory sessionFactory;
@Override
public void insertSkills(List<EmployeeSkill> skilllist) {
for(EmployeeSkill es:skilllist)
{
sessionFactory.getCurrentSession().save(es);
}
this.sessionFactory.close();
}
@SuppressWarnings("unchecked")
@Override
public List<EmployeeSkill> getSkillsForEmployee(int eid) {
Employee e=(Employee) sessionFactory.getCurrentSession().load(Employee.class,eid);
List<EmployeeSkill> skill=sessionFactory.getCurrentSession().createQuery("from EmployeeSkill where employee=:emp").setParameter("emp",e).list();
this.sessionFactory.close();
return skill;
}
@SuppressWarnings("unchecked")
@Override
public List<EmployeeSkill> getAllSkill() {
List<EmployeeSkill> sk=sessionFactory.getCurrentSession().createQuery("from EmployeeSkill").list();
this.sessionFactory.close();
return sk;
}
@SuppressWarnings("unchecked")
@Override
public void updateSkills(List<EmployeeSkill> skillslist, int eid) {
Employee e=(Employee) sessionFactory.getCurrentSession().load(Employee.class, eid);
List<EmployeeSkill> es=this.sessionFactory.getCurrentSession().createQuery("from EmployeeSkill where employee=:e").setParameter("e", e).list();
for(EmployeeSkill es1:es)
{
this.sessionFactory.getCurrentSession().delete(es1);
}
for(EmployeeSkill es1:skillslist)
{
this.sessionFactory.getCurrentSession().save(es1);
}
this.sessionFactory.close();
}
}
-------------------
package com.model;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="employee")
public class Employee
{
@Id
@Column
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int eid;
@Column
private String ename;
@Column
private int salary;
@Column
private String gender;
@OneToMany(mappedBy="employee",fetch=FetchType.EAGER)
private List<EmployeeHobby> employeehobby;
@OneToMany(mappedBy="employee",fetch=FetchType.LAZY)
private List<EmployeeSkill> employeeskill;
public Employee() {
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public List<EmployeeHobby> getEmployeehobby() {
return employeehobby;
}
public void setEmployeehobby(List<EmployeeHobby> employeehobby) {
this.employeehobby = employeehobby;
}
public List<EmployeeSkill> getEmployeeskill() {
return employeeskill;
}
public void setEmployeeskill(List<EmployeeSkill> employeeskill) {
this.employeeskill = employeeskill;
}
}
------------
package com.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class EmployeeHobby
{
@Id
@Column
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int hid;
@Column
private String hname;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="eid")
private Employee employee;
public EmployeeHobby()
{
}
public EmployeeHobby(String hname) {
super();
this.hname = hname;
}
public int getHid() {
return hid;
}
public void setHid(int hid) {
this.hid = hid;
}
public String getHname() {
return hname;
}
public void setHname(String hname) {
this.hname = hname;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Override
public String toString() {
return "EmployeeHobby [hid=" + hid + ", hname=" + hname + ", employee=" + employee + "]".toString();
}
}
-------------------
package com.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class EmployeeSkill
{
@Id
@Column
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int sid;
@Column
private String sname;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="eid")
private Employee employee;
public EmployeeSkill() {
}
public EmployeeSkill(String sname) {
super();
this.sname = sname;
}
public EmployeeSkill(int sid, String sname, Employee employee) {
super();
this.sid = sid;
this.sname = sname;
this.employee = employee;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
---------------
package com.service;
import java.util.List;
import com.model.Employee;
import com.model.EmployeeHobby;
public interface EmployeeHobbyService {
public void insertHobbies(List<EmployeeHobby> hobbieslist);
public List<EmployeeHobby> getHobbiesForEmployee(int eid);
public List<EmployeeHobby> getAllHobby();
public void updateHobbies(List<EmployeeHobby> hobbieslist,int eid);
public List<EmployeeHobby> getHobbyFromObject(Employee e);
}
------------------------
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dao.EmployeeHobbyDao;
import com.model.Employee;
import com.model.EmployeeHobby;
@Service
@Transactional
public class EmployeeHobbyServiceImpl implements EmployeeHobbyService {
@Autowired
EmployeeHobbyDao employeeHobbyDao;
@Transactional
public void insertHobbies(List<EmployeeHobby> hobbieslist) {
employeeHobbyDao.insertHobbies(hobbieslist);
}
@Transactional
public List<EmployeeHobby> getHobbiesForEmployee(int eid) {
return employeeHobbyDao.getHobbiesForEmployee(eid);
}
@Transactional
public void updateHobbies(List<EmployeeHobby> hobbieslist, int eid) {
employeeHobbyDao.updateHobbies(hobbieslist, eid);
}
@Transactional
public List<EmployeeHobby> getAllHobby() {
return employeeHobbyDao.getAllHobby();
}
@Override
@Transactional
public List<EmployeeHobby> getHobbyFromObject(Employee e) {
return employeeHobbyDao.getHobbyFromObject(e);
}
}
----------------------------
package com.service;
import java.util.List;
import com.model.Employee;
public interface EmployeeService
{
public void insertEmployee(Employee employee);
public Employee getEmployeeById(int eid);
public List<Employee> getAllEmployee();
public Employee updateEmployee(Employee employee);
public void deleteEmployee(int eid);
public List<Employee> searchByEmployee(Employee employee);
}
----------------------
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.EmployeeDao;
import com.model.Employee;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeDao employeeDao;
public void insertEmployee(Employee employee) {
employeeDao.insertEmployee(employee);
}
public Employee getEmployeeById(int eid) {
return employeeDao.getEmployeeById(eid);
}
public List<Employee> getAllEmployee() {
return employeeDao.getAllEmployee();
}
public Employee updateEmployee(Employee employee) {
return employeeDao.updateEmployee(employee);
}
public void deleteEmployee(int eid) {
employeeDao.deleteEmployee(eid);
}
@Override
public List<Employee> searchByEmployee(Employee employee) {
return employeeDao.searchByEmployee(employee);
}
}
-----------
package com.service;
import java.util.List;
import com.model.EmployeeSkill;
public interface EmployeeSkillService
{
public void insertSkills(List<EmployeeSkill> skilllist);
public List<EmployeeSkill> getSkillsForEmployee(int eid);
public List<EmployeeSkill> getAllSkill();
public void updateSkills(List<EmployeeSkill> skillslist,int eid);
}
-------------------
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dao.EmployeeSkillDao;
import com.model.EmployeeSkill;
@Service
@Transactional
public class EmployeeSkillServiceImpl implements EmployeeSkillService{
@Autowired
EmployeeSkillDao employeeSkillDao;
@Override
@Transactional
public void insertSkills(List<EmployeeSkill> skilllist) {
employeeSkillDao.insertSkills(skilllist);
}
@Override
@Transactional
public List<EmployeeSkill> getSkillsForEmployee(int eid) {
return employeeSkillDao.getSkillsForEmployee(eid);
}
@Override
@Transactional
public List<EmployeeSkill> getAllSkill() {
return employeeSkillDao.getAllSkill();
}
@Override
@Transactional
public void updateSkills(List<EmployeeSkill> skillslist, int eid) {
employeeSkillDao.updateSkills(skillslist, eid);
}
}
----------------
package otherclasses;
import com.model.Employee;
import com.model.EmployeeHobby;
public class SuperEmployee
{
private Employee employee;
private EmployeeHobby employeeHobby;
public SuperEmployee(Employee employee, EmployeeHobby employeeHobby) {
this.employee = employee;
this.employeeHobby = employeeHobby;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public EmployeeHobby getEmployeeHobby() {
return employeeHobby;
}
public void setEmployeeHobby(EmployeeHobby employeeHobby) {
this.employeeHobby = employeeHobby;
}
}
---------------------
EMPLOYEE.JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Form</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript" src='<c:url value="/resources/js/externaljqry.js"/>'></script>
</head>
<body>
<form:form id="form" action="saveEmployee" method="post" modelAttribute="employee">
ID:-<form:input path="eid" readonly="true"/><br>
Name:-<form:input path="ename"/><span id="spename"></span><br>
Salary:-<form:input path="salary"/><span id="spsalary"></span><br>
Gender:- <form:radiobutton path="gender" value="male" />Male
<form:radiobutton path="gender" value="female"/>Female <span id="spgender"></span><br>
<c:choose>
<c:when test="${employee.getEid() == 0 }">
Hobby:-<c:forEach items="${hobby}" var="checkbox" varStatus="count">
<form:checkbox path="employeehobby" label="${checkbox.getHname()}" value="${checkbox.getHname()}"/>
</c:forEach><span id="spemployeehobby"></span><br>
Skill:- <form:select path="employeeskill" multiple="false">
<form:option value="" label="-------SELECT-------" disabled="true"></form:option>
<c:forEach items="${skill}" var="drop">
<form:option value="${drop.getSname()}" label="${drop.getSname()}"></form:option>
</c:forEach>
</form:select><span id="spemployeeskill"></span>
<br><input type="submit" value="Add" onclick="return validate();"/>
<button onclick="validate();"></button>
</c:when>
<c:otherwise>
Hobby:-<c:forEach items="${hobby}" var="checkbox" varStatus="count" >
<c:set var="brk" value="0" scope="page" />
<c:set var="b" value="0" scope="page"/>
<c:forEach items="${emphobby}" var="hobbycheck" varStatus="c2">
<c:if test="${checkbox.getHname() eq hobbycheck.getHname() and brk eq 0}">
<c:set var="b" value="1" scope="page"/>
<c:set var="brk" value="1" scope="page" />
</c:if>
<c:if test="${checkbox.getHname() ne hobbycheck.getHname() and brk eq 0 }">
<c:set var="b" value="2" scope="page" />
<c:set var="brk" value="0" scope="page" />
</c:if>
</c:forEach>
<c:if test="${b eq 1}">
<form:checkbox path="employeehobby" label="${checkbox.getHname()}" checked="checked" value="${checkbox.getHname()}"></form:checkbox>
</c:if>
<c:if test="${b eq 2}">
<form:checkbox path="employeehobby" label="${checkbox.getHname()}" value="${checkbox.getHname()}"></form:checkbox>
</c:if>
</c:forEach><br>
Skill:-
<form:select path="employeeskill" multiple="true">
<form:option value="" label="-------SELECT-------" disabled="true"></form:option>
<c:forEach items="${skill}" var="sk">
<c:set var="brk1" value="0" scope="page" />
<c:set var="b1" value="0" scope="page"/>
<c:forEach items="${empskill}" var="skillcheck" varStatus="c2">
<c:if test="${sk.getSname() eq skillcheck.getSname() and brk1 eq 0}">
<c:set var="b1" value="1" scope="page"/>
<c:set var="brk1" value="1" scope="page" />
</c:if>
<c:if test="${sk.getSname() ne skillcheck.getSname() and brk1 eq 0 }">
<c:set var="b1" value="2" scope="page" />
<c:set var="brk1" value="0" scope="page" />
</c:if>
</c:forEach>
<c:if test="${b1 eq 1}">
<form:option label="${sk.getSname()}" selected="selected" value="${sk.getSname()}"></form:option>
</c:if>
<c:if test="${b1 eq 2}">
<form:option label="${sk.getSname()}" value="${sk.getSname()}"></form:option>
</c:if>
</c:forEach>
</form:select>
<input type="submit" value="Update"/>
</c:otherwise>
</c:choose>
</form:form>
</body>
</html>
------------------
home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>