<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Deborah Arku</title>
    <description>The latest articles on Forem by Deborah Arku (@amamre).</description>
    <link>https://forem.com/amamre</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3707137%2F290a7b16-db35-49da-9770-f45f17c3a774.jpg</url>
      <title>Forem: Deborah Arku</title>
      <link>https://forem.com/amamre</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/amamre"/>
    <language>en</language>
    <item>
      <title>Creating and Mapping Entities in JPA: Essential Annotations Explained</title>
      <dc:creator>Deborah Arku</dc:creator>
      <pubDate>Mon, 16 Feb 2026 20:45:23 +0000</pubDate>
      <link>https://forem.com/amamre/creating-and-mapping-entities-in-jpa-essential-annotations-explained-1862</link>
      <guid>https://forem.com/amamre/creating-and-mapping-entities-in-jpa-essential-annotations-explained-1862</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is JPA?&lt;/strong&gt;&lt;br&gt;
JPA (Java Persistence API) is a Java standard that makes it easier to work with relational databases. Instead of writing complex SQL queries, JPA lets you map Java classes to database tables and manage data using simple Java code. In short, JPA acts as a bridge between your Java objects and database records.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Entities&lt;/strong&gt;&lt;br&gt;
Before we look further into JPA annotations, let's clarify what an entity is.&lt;/p&gt;

&lt;p&gt;An **entity **represents a real-world object or concept that can be uniquely identified and stored in a database. If you remember your early lessons in Object-Oriented Programming (OOP), you learned that classes represent real-world objects. In JPA, we use classes to represent entities in the same way.&lt;/p&gt;

&lt;p&gt;Here's how the mapping works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;*&lt;em&gt;Entity *&lt;/em&gt;→ Java class&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;Table *&lt;/em&gt;→ Physical storage of the entity in the database&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;Column *&lt;/em&gt;→ Field (attribute/property) in the class&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;Row *&lt;/em&gt;→ Individual instance of the entity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, Student can be an entity. To capture the characteristics of each student, the table will have several columns like name, gender, department, and email address. Each row in this table represents a different student.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Entity Mapping?&lt;/strong&gt;&lt;br&gt;
Entity mapping is the process of defining how your Java classes correspond to database tables. Using JPA annotations, we specify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which class represents a database table&lt;/li&gt;
&lt;li&gt;Which field is the primary key&lt;/li&gt;
&lt;li&gt;How fields map to columns&lt;/li&gt;
&lt;li&gt;How entities relate to each other&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These annotations tell JPA exactly how to translate between your Java objects and database records. Let's explore the most common JPA annotations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core JPA Annotations&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/entity"&gt;@entity&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
This annotation declares that a class is not just an ordinary Java class, but an entity that should be mapped to a database table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
public class Student {
    // fields, constructors, methods
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
Every database table needs a primary key. A primary key is a unique value that distinguishes one record from another. The &lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt; annotation marks a field as the primary key of the entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
public class Student {
    @Id
    private Long id;
    private String name;
    private String email;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/column"&gt;@column&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
This annotation specifies the details of the column that a field maps to. While JPA automatically maps fields to columns with the same name, &lt;a class="mentioned-user" href="https://dev.to/column"&gt;@column&lt;/a&gt; allows you to customize the column name, length, whether it can be null, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
public class Student {
    @Id
    private Long id;

    @Column(name = "student_name", nullable = false, length = 100)
    private String name;

    @Column(name = "email_address", unique = true)
    private String email;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;@Table&lt;/strong&gt;&lt;br&gt;
By default, JPA uses the class name as the table name. The @Table annotation lets you customize this mapping by explicitly specifying the table name, schema, or unique constraints. This is especially useful when your class name conflicts with database reserved words or when you need to follow specific database naming conventions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
@Table(name = "students", schema = "university_db")
public class Student {
    @Id
    private Long id;
    private String name;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Relationship Annotations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In real applications, entities often have relationships with each other. JPA provides annotations to map these relationships.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@OneToOne&lt;/strong&gt;&lt;br&gt;
This annotation defines a one-to-one relationship between two entities. One instance of an entity is associated with exactly one instance of another entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Each student has exactly one student profile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
public class Student {
    @Id
    private Long id;
    private String name;

    @OneToOne
    @JoinColumn(name = "profile_id")
    private StudentProfile profile;
}

@Entity
public class StudentProfile {
    @Id
    private Long id;
    private String bio;
    private String photoUrl;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;@OneToMany&lt;/strong&gt;&lt;br&gt;
This annotation defines a one-to-many relationship. One instance of an entity is associated with multiple instances of another entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; One department has many students.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
public class Department {
    @Id
    private Long id;
    private String name;

    @OneToMany(mappedBy = "department")
    private List&amp;lt;Student&amp;gt; students;
}

@Entity
public class Student {
    @Id
    private Long id;
    private String name;

    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;@ManyToOne&lt;/strong&gt;&lt;br&gt;
This is the inverse of @OneToMany. Multiple instances of an entity are associated with one instance of another entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Many students belong to one department (shown in the example above).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@ManyToMany&lt;/strong&gt;&lt;br&gt;
This annotation defines a many-to-many relationship where multiple instances of one entity are associated with multiple instances of another entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Students can enroll in multiple courses, and each course can have multiple students.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
public class Student {
    @Id
    private Long id;
    private String name;

    @ManyToMany
    @JoinTable(
        name = "student_course",
        joinColumns = @JoinColumn(name = "student_id"),
        inverseJoinColumns = @JoinColumn(name = "course_id")
    )
    private List&amp;lt;Course&amp;gt; courses;
}

@Entity
public class Course {
    @Id
    private Long id;
    private String title;

    @ManyToMany(mappedBy = "courses")
    private List&amp;lt;Student&amp;gt; students;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In this article, we explored the essentials of entity mapping in JPA, including key annotations like &lt;a class="mentioned-user" href="https://dev.to/entity"&gt;@entity&lt;/a&gt;, &lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;, &lt;a class="mentioned-user" href="https://dev.to/column"&gt;@column&lt;/a&gt;, and @Table, as well as relationship annotations such as @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany. Understanding these concepts is crucial for effectively working with JPA and managing how your Java objects are stored in a relational database.&lt;/p&gt;

&lt;p&gt;As you continue building your Java applications, these annotations will become second nature, allowing you to focus on your business logic rather than complex SQL queries.&lt;/p&gt;

</description>
      <category>java</category>
      <category>jpa</category>
      <category>database</category>
      <category>backenddevelopment</category>
    </item>
  </channel>
</rss>
