anghelleonard / hibernate-springboot Goto Github PK
View Code? Open in Web Editor NEWCollection of 300+ best practices for Java persistence performance in Spring Boot applications
License: Apache License 2.0
Collection of 300+ best practices for Java persistence performance in Spring Boot applications
License: Apache License 2.0
I'm seeing some weird issue while working with findAll() method with the specification and pageable arguments. If I set the lower page i.e. 0 size i.e. 10 then I don't have any issues. but if I set the page i.e. 0 and size to 30 I'm getting the duplicate records.
e.g.
ID | NAME |
---|---|
1 | test |
2 | test |
3 | test |
4 | test |
5 | test |
6 | test |
7 | test |
8 | test |
9 | test |
1 | test |
2 | test |
3 | test |
4 | test |
11 | test |
6 | test |
7 | test |
18 | test |
I have the following in the database.
ID | NAME |
---|---|
1 | test |
2 | test |
3 | test |
4 | test |
5 | test |
6 | test |
7 | test |
8 | test |
9 | test |
10 | test |
11 | test |
12 | test |
13 | test |
14 | test |
15 | test |
16 | test |
17 | test |
18 | test |
@Entity
@Table(name = "table_name")
@JsonIgnoreProperties(ignoreUnknown = true)
@DynamicUpdate
public class Emp implements Persistable<String> {
@Id
@Column(name = "emp_id", nullable = false, columnDefinition = "BINARY(16)")
public String empId;
@Column(name = "name");
public String name;
// other fields
//setters and getters
}
@Repository
public interface EmpRepository extends JpaRepository<Emp, String> {
Page<Emp> findAll(Specification<Emp> specification, Pageable page);
}
public class EmpSpecification {
public static Specification<Emp> nameEquals(String name) {
return (root, query, builder) -> name == null ? null : builder.equal(root.get("name"), name);
}
}
in the service class we are trying to fetch data using the following code:
Specification<Emp> specifications = Specification.where(EmpSpecification.nameEquals(name)));
details = consolidatedTradeSummaryRepository.findAll(specifications, page);
http://localhost:8080/emp?name=test&page=0&size=10
http://localhost:8080/emp?name=test&page=0&size=20
Spring boot version : 2.2.2.RELEASE
Similar, we can fetch all Book
, including those with no registered Author
. This can be done via JOIN FETCH
or **JOIN**
.
JOIN
didn't return all Book
. The result excluding the record when the author_id is null.
Hi,
Thanks for awesome book.
HibernateSpringBootOneToManyBidirectional has helper method removeBooks.
@Transactional
public void deleteAllBooksOfAuthor() {
Author author = authorRepository.findByName("Joana Nimar");
author.removeBooks(); // use removeBooks() helper
}
But if you see carefully, it calls delete for each book entity. Can we optimize it somehow to call like IN queries instead?
Also purpose of these helper methods is to call on managed entities or detached ones only?
I am using findAll(Specification, Pageable)
with hints, but the result still contains duplicated records.
Hibernate 5.3.x
Postgres 9.6
I tried the example with the hibernate-enhance-maven-plugin
activated, and confirmed that direct @OnetoOne relations were being lazily loaded. I created a Book class with a @OnetoOne to Author, and every time I fetched a book, the author was being fetched lazily:
select book0_.id as id1_1_0_, book0_.author as author2_1_0_ from book book0_ where book0_.id=?
However, when I fetch an Author, which has an inverse @OnetoOne relation with Book, an extra query is being excecuted:
select author0_.id as id1_0_0_, author0_.age as age2_0_0_, author0_.avatar as avatar3_0_0_, author0_.genre as genre4_0_0_, author0_.name as name5_0_0_ from author author0_ where author0_.id=?
select book0_.id as id1_1_0_, book0_.author as author2_1_0_ from book book0_ where book0_.author=? // <-- This shouldn't be fetched
I read that this happens because hibernate does not know if it has to create a proxy or load it with a null reference, but I thought that the enhancement plugin fixed this.
This is the book class I created:
@Entity
public class Book implements Serializable {
private static final long serialVersionUID = 2L;
@Id
public Long id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author")
public Author author;
}
and the author class is the same as in the example, but I added the inverse relation:
@OneToOne(mappedBy = "author", fetch = FetchType.LAZY)
Book book;
Is there an error in my tests or it's still a limitation in the library? How are we suposed to solve this problem? Since if I fetch a list of objects with inverse relations, I will be making several extra queries and it will negatively impact on the performance
Forgot to mention: I also tried using @LazyToOne and @LazyGroup annotations in each field, but had the same results.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.