<?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 Nageswarareddy Kandikonda on Medium]]></title>
        <description><![CDATA[Stories by Nageswarareddy Kandikonda on Medium]]></description>
        <link>https://medium.com/@nageswarareddykandikonda?source=rss-5537a0669621------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*jA7b3dMFXOE4aXJV.</url>
            <title>Stories by Nageswarareddy Kandikonda on Medium</title>
            <link>https://medium.com/@nageswarareddykandikonda?source=rss-5537a0669621------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 19:49:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nageswarareddykandikonda/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[ Custom ID Generation in Spring Data JPA with Oracle — Generating Primary Keys like OD1, OD2, OD3.]]></title>
            <link>https://medium.com/@nageswarareddykandikonda/custom-id-generation-in-spring-data-jpa-with-oracle-generating-primary-keys-like-od1-od2-od3-caff44a09e9a?source=rss-5537a0669621------2</link>
            <guid isPermaLink="false">https://medium.com/p/caff44a09e9a</guid>
            <category><![CDATA[oracle]]></category>
            <category><![CDATA[spring-data-jpa]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[jpa]]></category>
            <dc:creator><![CDATA[Nageswarareddy Kandikonda]]></dc:creator>
            <pubDate>Wed, 09 Apr 2025 08:17:44 GMT</pubDate>
            <atom:updated>2025-04-09T08:17:44.256Z</atom:updated>
            <content:encoded><![CDATA[<h3>🔧 Custom ID Generation in Spring Data JPA with Oracle — Generating Primary Keys like OD1, OD2, OD3...</h3><p>In many applications, we use simple numeric auto-increment IDs for primary keys. But what if your business requirements demand more <strong>readable, structured, and meaningful</strong> IDs — like OD1, OD2, OD3, and so on?</p><p>In this article, I’ll show you how I implemented a <strong>custom primary key generator</strong> using Oracle Database and Spring Data JPA to generate order IDs prefixed with &quot;OD&quot;.</p><h3>🎯 The Goal</h3><p>The objective was to generate custom primary key values for an OrderDetails table with this format:</p><pre>OD1, OD2, OD3, ...</pre><p>We wanted readable and traceable order IDs instead of default numeric ones.</p><h3>🛠️ Tech Stack</h3><ul><li>Spring Boot</li><li>Spring Data JPA</li><li>Oracle Database</li><li>Lombok</li></ul><h3>📦 Maven Dependencies</h3><p>Add these to your pom.xml:</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br> xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;<br> &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;<br> &lt;parent&gt;<br>  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>  &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;<br>  &lt;version&gt;3.4.4&lt;/version&gt;<br>  &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;<br> &lt;/parent&gt;<br> &lt;groupId&gt;com.nreddy&lt;/groupId&gt;<br> &lt;artifactId&gt;SpringDataJPA-Custom-Generator&lt;/artifactId&gt;<br> &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;<br> &lt;name&gt;SpringDataJPA-Custom-Generator&lt;/name&gt;<br> &lt;description&gt;Developing custom generator with oracle Database&lt;/description&gt;<br> &lt;url/&gt;<br> &lt;licenses&gt;<br>  &lt;license/&gt;<br> &lt;/licenses&gt;<br> &lt;developers&gt;<br>  &lt;developer/&gt;<br> &lt;/developers&gt;<br> &lt;scm&gt;<br>  &lt;connection/&gt;<br>  &lt;developerConnection/&gt;<br>  &lt;tag/&gt;<br>  &lt;url/&gt;<br> &lt;/scm&gt;<br> &lt;properties&gt;<br>  &lt;java.version&gt;17&lt;/java.version&gt;<br> &lt;/properties&gt;<br> &lt;dependencies&gt;<br>  &lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;<br>  &lt;/dependency&gt;<br><br>  &lt;dependency&gt;<br>   &lt;groupId&gt;com.oracle.database.jdbc&lt;/groupId&gt;<br>   &lt;artifactId&gt;ojdbc11&lt;/artifactId&gt;<br>   &lt;scope&gt;runtime&lt;/scope&gt;<br>  &lt;/dependency&gt;<br>  &lt;!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --&gt;<br> &lt;dependency&gt;<br>    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;<br>    &lt;artifactId&gt;lombok&lt;/artifactId&gt;<br>    &lt;version&gt;1.18.38&lt;/version&gt;<br>    &lt;scope&gt;provided&lt;/scope&gt;<br>&lt;/dependency&gt;<br>  &lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;<br>   &lt;scope&gt;test&lt;/scope&gt;<br>  &lt;/dependency&gt;<br> &lt;/dependencies&gt;<br><br> &lt;build&gt;<br>  &lt;plugins&gt;<br>   &lt;plugin&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>   &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br> &lt;/build&gt;<br><br>&lt;/project&gt;</pre><h3>📐 Step-by-Step Implementation</h3><h3>Step 1: Create the sequence in oracle database</h3><pre>create sequence order_id_seq start with 1 increment by 1;</pre><h3>Step 2: Create the custom generator class</h3><pre>public class OrderIDGenerator implements IdentifierGenerator {<br><br> @Override<br> public Object generate(SharedSessionContractImplementor session, Object object) {<br>  <br>    String prefix=&quot;OD&quot;;<br>    String suffix=&quot;&quot;;<br>    String primaryKeyVal=&quot;&quot;;<br>    <br>  <br>   try {<br>    Connection connection = session.getJdbcConnectionAccess().obtainConnection();<br>    Statement statement = connection.createStatement();<br>    PreparedStatement ps=connection.prepareStatement(&quot;select order_id_seq.nextval from dual&quot;);<br>    ResultSet resultSet= ps.executeQuery();<br>          <br>   <br>      if(resultSet.next()) {<br>       int orderID = resultSet.getInt(1);<br>        suffix = String.valueOf(orderID);<br>      }<br>      <br>     return primaryKeyVal=prefix+suffix;<br>     <br>   } catch (SQLException e) {<br>       throw new HibernateException(&quot;Unable to generate Order ID&quot;, e);<br>   }<br> }<br>}</pre><h4>Step 3: Create custom annotation @OrderID</h4><p>Created a custom annotation @OrderId and integrated it with Hibernate using @IdGeneratorType, configured with the OrderIDGenerator class.</p><pre>@Documented<br>@Retention(RUNTIME)<br>@Target({ FIELD, METHOD })<br>@IdGeneratorType(OrderIDGenerator.class)<br>public @interface OrderId {<br>}</pre><h4>Step 4: Define the entity class</h4><pre>@Entity<br>@Table(name = &quot;Order_Details_tbl&quot;)<br>@Data<br>public class OrderDetailsEntity {<br> @Id<br> @GeneratedValue<br> @OrderId //Custom annotation <br> @Column(name = &quot;order_id&quot;)<br> private String orderId;<br> @Column(name = &quot;order_by&quot;)<br> private String orderBy;<br> @Column(name=&quot;ordered_date&quot;)<br> @Temporal(TemporalType.DATE)<br> private Date orderedDate;<br>}</pre><h4>Step 4: Create the Repository</h4><pre>public interface OrderDetailsRepository extends JpaRepository&lt;OrderDetailsEntity, Serializable&gt; {<br>}</pre><h4>Step 5: Main Application class</h4><pre>@SpringBootApplication<br>public class SpringDataJpaCustomGeneratorApplication {<br><br>    public static void main(String[] args) {<br>        ConfigurableApplicationContext context = SpringApplication.run(SpringDataJpaCustomGeneratorApplication.class, args);<br><br>        OrderDetailsRepository repository = context.getBean(OrderDetailsRepository.class);<br><br>        OrderDetailsEntity entity = new OrderDetailsEntity();<br>        entity.setOrderBy(&quot;Nageswarareddy&quot;);<br>        entity.setOrderedDate(new Date());<br><br>        OrderDetailsEntity savedEntity = repository.save(entity);<br>        System.out.println(savedEntity);<br>    }<br>}</pre><h4>Output Example:</h4><pre>OrderDetailsEntity(orderId=OD1, orderBy=Nageswarareddy, orderedDate=2025-04-09)</pre><h4>✅ Benefits of This Approach</h4><ul><li><strong>Readable IDs</strong>: Business-friendly, human-readable order identifiers.</li><li><strong>Centralized logic</strong>: Generator logic is cleanly encapsulated.</li><li><strong>Hibernate-integrated</strong>: Leverages native Hibernate lifecycle without manual intervention.</li></ul><h3>🧠 Final Thoughts</h3><p>Custom ID generation can be a powerful tool when building enterprise applications, especially when tracking or auditing business records is essential. By using Hibernate’s extensibility and Oracle’s sequence features, this solution creates consistent, readable primary keys without sacrificing performance or maintainability.</p><p>Have you used custom generators in your project? I’d love to hear how you’ve implemented them — drop a comment or reach out!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=caff44a09e9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>