<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Denis Yevseyenko on Medium]]></title>
        <description><![CDATA[Stories by Denis Yevseyenko on Medium]]></description>
        <link>https://medium.com/@yevseyenkod27?source=rss-40458c7984ea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ECWgXbmGiaFVLOXy9PXKvg.jpeg</url>
            <title>Stories by Denis Yevseyenko on Medium</title>
            <link>https://medium.com/@yevseyenkod27?source=rss-40458c7984ea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 17:01:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yevseyenkod27/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Lombok: Because your IDE deserves a break too.]]></title>
            <link>https://medium.com/@yevseyenkod27/lombok-because-your-ide-deserves-a-break-too-14f7b98ba15d?source=rss-40458c7984ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/14f7b98ba15d</guid>
            <category><![CDATA[lombok]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Denis Yevseyenko]]></dc:creator>
            <pubDate>Sun, 04 May 2025 10:28:52 GMT</pubDate>
            <atom:updated>2025-05-04T10:28:52.075Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we’ll explore how we use <strong>Lombok</strong> in our Java application to reduce boilerplate code and improve readability. While Lombok can streamline development, it’s important to understand its impact on maintainability, tooling, and collaboration before fully adopting it.</p><p><strong>What is Lombok?</strong><br>Lombok is a Java library that helps reduce boilerplate code by automatically generating commonly used code like getters, setters, constructors, equals(), hashCode(), and toString() methods using simple annotations. It integrates with your IDE and build tools, making your code cleaner, more concise, and easier to maintain.</p><p><strong>Before Lombok</strong></p><pre>public class AccountDto {<br>    private Long id;<br>    private String accountNumber;<br>    private Double balance;<br>    private Client client;<br><br>    public Long getId() {<br>        return id;<br>    }<br><br>    public void setId(Long id) {<br>        this.id = id;<br>    }<br><br>    public String getAccountNumber() {<br>        return accountNumber;<br>    }<br><br>    public void setAccountNumber(String accountNumber) {<br>        this.accountNumber = accountNumber;<br>    }<br><br>    public Double getBalance() {<br>        return balance;<br>    }<br><br>    public void setBalance(Double balance) {<br>        this.balance = balance;<br>    }<br><br>    public Client getClient() {<br>        return client;<br>    }<br><br>    public void setClient(Client client) {<br>        this.client = client;<br>    }<br>}</pre><p><strong>What’s not good before using Lombok:</strong></p><p><strong>Too Much Boilerplate Code:</strong></p><ul><li>The class contains <strong>manual getters and setters</strong> for each field.</li><li>While they are necessary for encapsulation, they don’t add any business logic — just repetitive, cluttered code.</li></ul><p><strong>Harder to Read and Maintain:</strong></p><ul><li>The essence of the class — its structure and relationships — is hidden behind a wall of trivial code.</li><li>It becomes harder to focus on what’s important (like the JPA annotations or entity design).</li></ul><p><strong>Increased Risk of Human Error:</strong></p><ul><li>Manually writing getters/setters can lead to mistakes (e.g., copy-paste errors, mismatched names).</li><li>If a field changes, you need to manually update the relevant methods, increasing maintenance costs.</li></ul><p><strong>More Lines of Code:</strong></p><ul><li>This class is already ~50 lines with only 4 fields.</li><li>Larger models can quickly become hundreds of lines long just because of repeated method definitions.</li></ul><p><strong>Main Lombok Annotations</strong></p><pre>@Getter<br><br>@Setter<br><br>@ToString<br><br>@NoArgsConstructor<br><br>@AllArgsConstructor<br><br>@RequiredArgsConstructor<br><br>@Data<br><br>@Builder</pre><p><strong>@Getter and @Setter annotation</strong></p><p>Automatically generates standard getter and setter methods for fields.</p><pre>@Setter<br>@Getter<br>public class AccountDto{<br>    private Long id;<br>    private String accountNumber;<br>    private Double balance;<br>    private Client client;<br>}</pre><p><strong>Where to use:</strong></p><ul><li>On classes (to generate for all fields).</li><li>In specific fields (to generate selectively).</li></ul><p><strong>When to be careful:</strong></p><ul><li>Don’t expose setters for immutable or sensitive fields unnecessarily.</li><li>In JPA entities, sometimes you may only want getters (e.g., for id).</li></ul><p><strong>@ToString</strong></p><p>Generates a toString() method that includes the class name and field values.</p><pre>@ToString<br>public class AccountDto {<br>    private Long id;<br>    private String accountNumber;<br>    private Double balance;<br>    private Client client;<br>}</pre><p><strong>When to be careful:</strong></p><ul><li><strong>Bidirectional JPA relations</strong> (like @OneToOne, @ManyToOne) can cause <strong>infinite recursion</strong> — always consider excluding those.</li></ul><pre>@ToString(exclude = &quot;client&quot;) </pre><p><strong>@NoArgsConstructor and @AllArgsConstructor</strong></p><p>@NoArgsConstructorgenerates a constructor with no parameters.</p><p>@AllArgsConstructorgenerates a constructor with parameters for <strong>all fields</strong>.</p><p>@RequiredArgsConstructorconstructor for <strong>final</strong> or <strong>@NonNull</strong> fields only.</p><pre>@Setter<br>@Getter<br>@NoArgsConstructor<br>@AllArgsConstructor<br>public class AccountDto {<br>    private Long id;<br>    private String accountNumber;<br>    private Double balance;<br>    private Client client;<br>}</pre><p><strong>When to be careful:</strong></p><ul><li>JPA requires a <strong>no-arg constructor</strong> (can be protected).</li><li>@RequiredArgsConstructor is useful for immutable classes or service classes.</li></ul><p><strong>@Data</strong></p><p>A shortcut that includes:<br><a href="http://twitter.com/Getter">@Getter</a>, <a href="http://twitter.com/Setter">@Setter</a>, <a href="http://twitter.com/ToString">@ToString</a>, <a href="http://twitter.com/EqualsAndHashCode">@EqualsAndHashCode</a>, <a href="http://twitter.com/RequiredArgsConstruc">@RequiredArgsConstruc</a>tor</p><pre>@Data<br>public class AccountDto {<br>    private Long id;<br>    private String accountNumber;<br>    private Double balance;<br>    private Client client;<br>}</pre><p><strong>When to be careful:</strong></p><ul><li><strong>Avoid on JPA entities</strong> — generates equals(), hashCode(), and toString() automatically, which may cause issues with <strong>Hibernate proxies</strong> or <strong>lazy-loaded relations</strong>.</li></ul><p><strong>@Builder</strong></p><p>Implements the <strong>builder pattern</strong>, which allows constructing objects step-by-step in a readable way.</p><pre>@Builder<br>public class Account {<br>    private String accountNumber;<br>    private Double balance;<br>}</pre><pre>Account account = Account.builder()<br>    .accountNumber(UUID.toString())<br>    .balance(100.0)<br>    .build();</pre><p><strong>When to be careful:</strong></p><ul><li><strong>Bidirectional JPA relations</strong> can lead to problems (e.g., infinite loops, nulls).</li><li>Lombok generates a builder for <strong>all fields</strong>, so be cautious with mandatory vs. optional fields.</li><li>Lombok’s builder <strong>doesn’t work well with default constructors</strong> in some JPA cases.</li></ul><p>Using <strong>Lombok</strong> in a Java application can significantly simplify your code by removing boilerplate and improving readability. With just a few annotations, you can eliminate repetitive code like getters, setters, constructors, and even equals, hashCode, or toString implementations.</p><p>However, as with any tool, <strong>understanding when and where to use Lombok is key</strong>. In JPA-based applications, extra caution is needed when using annotations like @ToString, @EqualsAndHashCode, and @Builder, especially in the presence of bidirectional relationships or lazy loading.</p><p>Lombok is not just about reducing lines of code — it’s about writing <strong>clearer, cleaner, and more maintainable</strong> software. If used wisely and consistently within your team, it becomes a powerful asset in your Java development toolkit.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14f7b98ba15d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[@MappedSuperclass: Share the roots, and let your entities grow!]]></title>
            <link>https://medium.com/@yevseyenkod27/mappedsuperclass-share-the-roots-and-let-your-entities-grow-002682fc3a26?source=rss-40458c7984ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/002682fc3a26</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[jpa]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Denis Yevseyenko]]></dc:creator>
            <pubDate>Sat, 28 Dec 2024 08:33:21 GMT</pubDate>
            <atom:updated>2024-12-28T09:47:55.139Z</atom:updated>
            <content:encoded><![CDATA[<h3>@MappedSuperclass: Share the roots, and let your entities grow!</h3><p>In object-relational mapping with JPA, reusability, and maintainability are key to building robust and scalable applications. The @MappedSuperclass annotation in JPA (Java Persistence API) is an excellent way to encapsulate shared fields and behaviors in a single, reusable base class. By placing common attributes—like identifiers and audit fields—in one class, you &quot;plant the roots&quot; of your data model, allowing each entity to &quot;grow&quot; its unique attributes without duplicating boilerplate code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V5Zsg_KM6leH_6Hmi6k3eA.jpeg" /></figure><p><strong>Entities with Common and Unique Fields</strong></p><p>Consider an example with four entities: EntityA, EntityB, EntityC, and EntityD. Each entity has its unique fields and some shared common fields. Below are the code snippets for these entities:</p><p>EntityA</p><pre>@Getter<br>@Setter<br>@Entity<br>public class EntityA {<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.AUTO)<br>    private Long id;<br><br>    //unique fields for EntityA<br>    private BigDecimal priceA;<br>    private String currency;<br>    private Timestamp transactionDate;<br>    private boolean isActive;<br>    private String description;<br><br><br>    //common fields in all entities<br>    private String source;<br>    private int maxFlag;<br>    private int minFlag;<br>    private int parentId;<br>    private String parentType;<br>}</pre><p>EntityB</p><pre>@Getter<br>@Setter<br>@Entity<br>public class EntityB extends BaseEntity{<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.AUTO)<br>    private Long id;<br><br>    //unique fields for EntityB<br>    private BigDecimal priceB;<br>    private String category;<br>    private Timestamp effectiveDate;<br>    private String status;<br>    private int quantity;<br>    private String remarks;<br><br>    //common fields in all entities<br>    private String source;<br>    private int maxFlag;<br>    private int minFlag;<br>    private int parentId;<br>    private String parentType;<br>}</pre><p>EntityC</p><pre>@Getter<br>@Setter<br>@Entity<br>public class EntityC {<br><br>    @Id<br>    @GeneratedValue(strategy = GenerationType.AUTO)<br>    private Long id;<br><br>    //unique fields for EntityC<br>    private BigDecimal priceC;<br>    private String feeType;<br>    private Timestamp expiryDate;<br>    private boolean isActive;<br><br>    //common fields in all entities<br>    private String source;<br>    private int maxFlag;<br>    private int minFlag;<br>    private int parentId;<br>    private String parentType;<br>}</pre><p>EntityD</p><pre>@Getter<br>@Setter<br>@Entity<br>public class EntityD {<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.AUTO)<br>    private Long id;<br><br>    //unique fields for EntityD<br>    private BigDecimal priceC;<br>    private LocalDate effectiveFrom;<br>    private LocalDate effectiveTo;<br>    private boolean isTaxable;<br>    private String notes;<br><br>    //common fields in all entities<br>    private String source;<br>    private int maxFlag;<br>    private int minFlag;<br>    private int parentId;<br>    private String parentType;<br>}</pre><p><strong>The Problem with Duplication</strong></p><p>As shown, each entity contains both unique fields and five common fields. While this is manageable with a few fields, in real-world applications, the number of common fields can grow significantly, leading to duplicated code and increased maintenance complexity.</p><p>Can we eliminate duplication while ensuring each entity retains access to these common fields? The answer lies in @MappedSuperclass.</p><h3>The Purpose of @MappedSuperclass</h3><ul><li>A @MappedSuperclass is designed to allow common fields to be shared among multiple entities without duplicating code.</li><li>Fields in the @MappedSuperclass are treated as if they are part of the subclass&#39;s persistent state, regardless of their visibility.</li></ul><h3>How to use @MappedSuperclass in code</h3><p>Create a BaseEntity class to encapsulate the common fields:</p><pre>@Getter<br>@Setter<br>@MappedSuperclass<br>public class BaseEntity {<br>    private String source;<br>    private int maxFlag;<br>    private int minFlag;<br>    private int parentId;<br>    private String parentType;<br>}</pre><p>2. Extend BaseEntity in your entities, removing the duplicated common fields:</p><pre>@Getter<br>@Setter<br>@Entity<br>public class EntityA extends BaseEntity</pre><p>The code snippet for EntityA looks like this:</p><pre>@Getter<br>@Setter<br>@Entity<br>public class EntityA extends BaseEntity{<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.AUTO)<br>    private Long id;<br><br>    //unique fields for EntityA<br>    private BigDecimal priceA;<br>    private String currency;<br>    private Timestamp transactionDate;<br>    private boolean isActive;<br>    private String description;<br>}</pre><h3>Advantages of @MappedSuperclass</h3><ul><li><strong>Cleaner Code:</strong> By moving common fields to BaseEntity, entities focus only on their unique attributes.</li><li><strong>Reusability:</strong> Shared fields and behaviors are defined once, reducing duplication.</li><li><strong>Maintainability:</strong> Changes to common fields are centralized in BaseEntity, simplifying updates.</li></ul><p><strong>Example using a Repository</strong></p><p>We can easily create a <strong>JPA Repository </strong>using common fields from BaseEntity</p><pre>@Repository<br>public interface EntityARepository extends JpaRepository&lt;EntityA,Long&gt; {<br>    EntityA findAllByMaxFlag(Integer maxFlag);<br>}</pre><p>Entities have all fields</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*AmsZWzKPoLbRlX4Iq0_ISg.png" /></figure><p>We have access to common fields via setters and getters</p><pre>EntityA entityA = new EntityA();<br>entityA.setMaxFlag(1);<br>entityA.setParentId(1);</pre><h3>Key notes on @MappedSuperclass</h3><ol><li><strong>Persistence via Reflection:</strong> Private fields in a @MappedSuperclass are accessible for persistence because JPA relies on reflection, bypassing normal Java visibility rules.</li><li><strong>Programmatic Access:</strong> While JPA can manage private fields internally, you should provide getters and setters for programmatic access.</li></ol><h3>Conclusion</h3><p>The @MappedSuperclass annotation is a powerful tool in JPA for achieving cleaner, more maintainable code by centralizing common fields and behaviors in a shared base class. It eliminates code duplication, promotes reusability, and simplifies the management of entities in large-scale applications. By separating shared attributes into a @MappedSuperclass like BaseEntity, you can focus on defining unique attributes in each entity while retaining access to common fields effortlessly.</p><p>This approach not only improves the structure and readability of your code but also ensures easier updates and scalability. Whether you are building small applications or complex systems, leveraging @MappedSuperclass allows you to &quot;share the roots and let your entities grow&quot; into a robust and organized data model.</p><p>Feel free to connect with me on <a href="https://www.linkedin.com/in/denis-yevseyenko-6a4108199/">LinkedIn</a> for more insights, discussions, and tips on software development and best practices. Let’s grow and learn together!</p><p>Thank you, and happy coding! 😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=002682fc3a26" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>