-
Notifications
You must be signed in to change notification settings - Fork 3
Working with Repository
Somkiat Puisungnoen edited this page Dec 18, 2024
·
7 revisions
Using Spring Data JPA
Create User Entity => User.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
// Setter/Getter methods
Create Repository with JpaRepository => UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Integer> {
@Query("SELECT user FROM User user ORDER BY user.id DESC")
@Transactional(readOnly = true)
List<User> findAllUsers();
}
- schema.sql
- data.sql
application.properties
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=none