You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Spring data JPA will do operations with the connected DB
and do the ORM operations.
1. ORM AND JPA
- ORM: Object Relation Mapping
- IN ORM : Class is Table , and vars. will be column filed and var type str, int will be column type
- By using the ORM Tool we map these thing in the table ex : Hibernet
- JPA : if library is changed from Hibernet to other library the specification like JPA (java Persistent API)
- will make easy to implements with other ORM library
2. Creating Table and Inserting Data using the JPA
// setting up the student Model @Component@Scope("prototype")
@EntitypublicclassStudent {
@Idprivateintrollno;
privateStringname;
privateintmarks;
}
// getter setters
2.2 Fetching all objects
System.out.println(repo.findAll());
System.out.println(repo.findAll());
// find By IDSystem.out.println(repo.findById(101));
// Student sfind = repo.findById(104); // give the error of null pointer// Optional<Student> sfind = repo.findById(104);Optional<Student> sfind = repo.findById(103);
System.out.println(sfind.orElse(newStudent())); // get the other new obj if no data
// new method by using the query annotation// using the class name and property nam in JPA@Query("select s from Student s where s.name= ?1") // ? is for the 1st parameterList<Student> findByStudentName(Stringname);
// //DSL Query , By default the JPA will implement the query for below things like :List<Student> findByName(Stringname);
List<Student> findByMarks(intmarks);
List<Student> findByMarksGreaterThan(intmarks);
List<Student> findByNameAndMarks(Stringname, intmarks);
}
//search By Name// using the @Query annotationSystem.out.println(repo.findByStudentName("John"));
// using the DSL querySystem.out.println(repo.findByStudentName("Jane"));
System.out.println(repo.findByMarksGreaterThan(30));
System.out.println(repo.findByMarks(23));
2.3 Update and Delete
// Update and Deletes3.setRollno(103);
s3.setName("JP");
s3.setMarks(99);
repo.save(s3);
repo.delete(s3);