F4F
JCHJava Community | Help. Code. Learn.
•Created by F4F on 2/19/2025 in #java-help
How to combine Left Join Fetch with specification
public interface PersonRepository extends JpaRepository<Person, Integer>, JpaSpecificationExecutor<Person> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<Person> findWithLockingById(Integer id);
@Query("SELECT p FROM Person p LEFT JOIN FETCH p.positions")
Page<Person> findAll(Specification<Person> spec, Pageable pageable);
}
@Transactional(readOnly = true)
public Page<PersonDto> findAll(Pageable pageable, Map<String, String> params) {
Specification<Person> filter = FilterSpecification.byCriteria(params);
return personRepository.findAll(filter, pageable)
.map(person -> creationStrategies.get(person.getType()).toDto(person));
}
Hello Guys my specification desn't work when I'm filtering. How I can combine specification with @Query - Person class is abstract and base class all others like Studet, Alien etc neet to extend. Problem is only when I'm trying to fetch all people and one of specific person got relationship OneToMany. So I decidet to use LeftJoinFetch - but now my filtering method doesn't work. Thanks6 replies
JCHJava Community | Help. Code. Learn.
•Created by F4F on 1/24/2025 in #java-help
ThreadPoolTaskExecutor - Configuration way.
Helo, what is the best way to configure ThreadPoolTaskExecutor and why?
1 -
@Bean(name = "thisIsBeanName")
public ThreadPoolTaskExecutor taskExecutor(ThreadProperties threadProperties) {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
properties..
2 - private final ThreadProperties threadProperties;
@Bean(name = "thisIsBeanName")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
properties...
28 replies
JCHJava Community | Help. Code. Learn.
•Created by F4F on 12/7/2024 in #java-help
N+1 Hibernate.
I've got problem with fetching data. Here is my code:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@SuperBuilder
@DiscriminatorColumn(name = "dtype")
public abstract class Person{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String surname;
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class Employee extends Person {
@OneToMany(mappedBy = "employee")
private List<Position> positions = new ArrayList<>();
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@SuperBuilder
@ToString
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "employee_id")
private Employee employee;
private String position;
private LocalDate startDate;
private LocalDate endDate;
}
@Transactional(readOnly = true)
public Page<PersonDto> findAll(Pageable pageable, Map<String, String> params) {
Specification<Person> filter = FilterSpecification.byCriteria(params);
return personRepository.findAll(filter, pageable)
.map(person -> creationStrategies.get(person.getType()).toDto(person));
}
I've got one superclass like Person becauseI can create a lot of person typelike Employee, alien, kid, retiree with deflaut params. But when I'm fetching all the people using findAll from JPA I'v got this problem:
Hibernate: select distinct p10.id,p1_0.dtype,p1_0.email,p1_0.height,p1_0.name,p1_0.person_number,p1_0.surname,p1_0.type,p1_0.version,p1_0.weight,p1_0.pension_amount,p1,p1_0.university_name,p1_0.year_of_study from person p1_0 where 1=1 offset ? rows fetch first ? rows only Hibernate: select p1_0.employee_id,p1_0.id,p1_0.end_date,p1_0.position,p1_0.salary,p1_0.start_date from position p1_0 where p1_0.employee_id=?
I'm tried fetchType.Eager, Batch and all of them doesn't work.
5 replies
JCHJava Community | Help. Code. Learn.
•Created by F4F on 4/6/2023 in #java-help
Interface
Hello! I have question about methods in interface.
I have got class like triangle, square this class implements my interface "Figures"
In interface I have got methods " double countArea();"
how I can create List method in interface?
Is this good idea -> "List<Figures> areasSmallerThan();" ?
Thanks for your answers.
10 replies