✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page
  • Configuration
  • Embedded
  • OneToOne
  • OneToMany & ManyToOne
  • ManyToMany
  • @Qualifier("")
  • Repository Interface
  • Example Repository
  • Pagination and Sorting Repo

Was this helpful?

  1. Spring Boot
  2. Spring Data

Spring Data JPA

better than jdbc !

Configuration

// FOR JPA
spring:
  datasource:
    url: jdbc:postgresql://[host]:[port]/[db name]
    username: username
    password: [password]

  jpa:
   hibernate.ddl-auto: update
   show-sql: true
   properties:
     hibernate:
       dialect: org.hibernate.dialect.PostgreSQLDialect

Embedded

@Embedded
private AnotherClass anotherClass;

// another class

@AttributeOverrides({
		@AttributeOverride(name = "something [in db]", 
		column = @Column(name = "something_field")),
})
public class AnotherClass {
	private String somethingField;
}

OneToOne

// example : class course one to one with courseMaterial
// course model
@OneToOne(mappedBy = "course")
private CourseMaterial courseMaterial;

// courseMaterial model
@OneToOne(
	cascade = CascadeType.ALL,
	fetch = FetchType.LAZY
)
@JoinColumn(
	name = "course_id",
	referencedColumnName = "courseId"
)
private Course course;

OneToMany & ManyToOne

// one teacher can teach many course
// one course can be taught by many teachers

// teacher class
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "teacher_id", referencedColumnName = "teacherId")
private List<Course> courses;

// course class
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "teacher_id", referencedColumnName = "teacherId")
private Teacher teacher;

ManyToMany

// many students can take many courses

@ManyToMany(
	cascade = CascadeType.ALL
)
@JoinTable(
	name = "student_course_map", 
	joinColumns = @JoinColumn(
		name = "course_id", 
		referencedColumnName = "courseId"
	), 
	inverseJoinColumns = @JoinColumn(
		name = "student_id", 
		referencedColumnName = "studentId"
	)
)
private List<Student> students;

@Qualifier("")

  • You can mark a component with name, ex : @Component("fakedao") and when performing dependency injection, you can specifies which component to exactly inject, ex : @Qualifier("fakedao")

@Autowired
public PersonService(@Qualifier("fakeDao") PersonDao personDao) {
    this.personDao = personDao;
}

Repository Interface

@Repository
public interface TRepository extends JpaRepository<T, Long> {

}

Example Repository

@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {

	List<Student> findByFirstName(String firstName);

	// JPQL-native
	@Query(value = "select * from tbl_student s where s.email_address = ?1", nativeQuery = true)
	Student getStudentByEmailAddressNative(String emailId);

	// JPQL
	@Query("select s from Student s where s.emailId = ?1")
	Student findStudentByEmail(String emailId);

	// Native Named Param
	@Query (
		value = "select * from tbl_student s where s.email_address = :emailId",
		nativeQuery = true
	)
	Student getStudentByEmailAddressNativeNamed(@Param("emailId") String emailId);

	@Modifying
	@Transactional
	@Query (
		value = "update tbl_student set first_name = ?1 where email_address = ?2",
		nativeQuery = true
	)
	Integer updatStudentEmailIdByFirstName(String firstName, String emailId);
}

Pagination and Sorting Repo

@Repository
public interface PersonPageRepository extends PagingAndSortingRepository<Person, Integer> {
}


public Page<Person> findAll(@RequestParam int page, @RequestParam int size) {
    PageRequest pr = PageRequest.of(page,size);
    return repository.findAll(pr);
}
PreviousRedisNextJDBC

Last updated 1 year ago

Was this helpful?