Java Development Cheatsheet
Comparison for BigDecimal
In Java, comparing BigDecimal values requires care because equals and compareTo behave differently.
equals vs compareTo
BigDecimal#equals checks both value and scale, while BigDecimal#compareTo only checks value (ignoring scale). This means:
Why this matters
The scale mismatch often appears when values come from different sources — e.g., parsing user input, reading from a database (DECIMAL(10,2) columns), or receiving JSON payloads. You might think two values are equal when equals says they aren't.
Rule of thumb
-
Use
compareTofor numeric equality checks:a.compareTo(b) == 0 -
Use
equalsonly when you mean "identical representation" (same value and same scale) -
Use
stripTrailingZeros()if you need to normalise scale beforeequals:
Comparing with zero
Avoid == or .equals(BigDecimal.ZERO) to check for zero — prefer compareTo:
How to Retrieve Data from a BlockingQueue
take()— retrieves and removes the head of the queue, waiting if necessary until an element becomes available.poll()— retrieves and removes the head of the queue, or returnsnullif the queue is empty.poll(long timeout, TimeUnit unit)— retrieves and removes the head of the queue, waiting up to the specified wait time if necessary for an element to become available. Returnsnullif the timeout expires.peek()— retrieves but does not remove the head of the queue. Returnsnullif the queue is empty.
Spring Cloud Alibaba FAQs
How to prevent Nacos from creating a nacos folder in the user's home directory?
Add the following two configuration properties to specify the Nacos storage path:
JM.LOG.PATHJM.SNAPSHOT.PATH
How to deal with Sentinel's scattered log files?
Add the configuration property csp.sentinel.log.dir to change Sentinel's log directory.
How to add Configuration Properties in JetBrains IntelliJ IDEA?
In JetBrains IntelliJ IDEA, click Edit Configurations… in the run configuration dropdown at the top right.
Click the Modify options button on the page, then add the properties you need to reset in the Override configuration properties table that appears below.
Spring Data JPA FAQs
How to fix the "Serializing PageImpl instances as-is not supported" warning?
Spring Data JPA warns about unstable JSON serialization of PageImpl. To resolve this, enable VIA_DTO serialization mode on your application's main class:
Why does the first page return no results?
JPA pagination is zero-indexed. Page 0 is the first page. If your frontend sends page=1, you need to pass page - 1 to Spring Data:
How to avoid the N+1 query problem?
The N+1 problem occurs when JPA executes one query for the parent entity, then N additional queries for each child association.
Detection — look for repetitive SQL queries in the logs, or configure spring.jpa.properties.hibernate.generate_statistics=true to spot it.
Fixes:
findById vs getReferenceById — which one to use?
findById— hits the database immediately, returns the entity orOptional.empty(). Use this when you need the data.getReferenceById— returns a lazy proxy without hitting the database. ThrowsEntityNotFoundExceptiononly when you access a non-existent proxy's properties. Use this when you only need the ID to set a foreign key relationship.
How to fix LazyInitializationException?
This happens when you access a lazily-loaded association outside the persistence context (e.g., in a controller or serializer after the transaction has closed).
Solutions:
-
Use
JOIN FETCHor@EntityGraphto eagerly load needed associations. -
Use DTO projections — return only the fields you need instead of whole entities:
-
@Transactional(readOnly = true)on the service method — keep the session open for the entire method scope.
When should I use @Transactional(readOnly = true)?
Use @Transactional(readOnly = true) on read-only service methods for three benefits:
- Hibernate skips dirty checking (no snapshots, less memory).
- The JDBC driver may route to read replicas.
- It documents the intent clearly.
save() vs saveAll() — which is faster for batch inserts?
saveAll() uses a single transaction and can benefit from JDBC batching. Configure the batch size:
For large bulk inserts (thousands of rows), consider JdbcTemplate batch operations instead — Hibernate's entity management overhead is significant at that scale.
How to use dynamic queries with Specification?
For complex search forms with optional filters, use JpaSpecificationExecutor: