> For the complete documentation index, see [llms.txt](https://bernardo.gitbook.io/development-docs-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bernardo.gitbook.io/development-docs-java/jpa/jpa-entities.md).

# JPA Entities

Java beans can be mapped to the database tables.

This can be done with XML or annotations.

## Constraints

* Usage of JPA annotations or XML mapping
* Non final classes
* Non final getters and setters

## Additional Constraints

These are recommended, but not compulsory:

* Extend the equals method
* Extend the hashCode method
* Extend the toString method

## Comparison Methods

For the equals and hashCode methods do not use the id for this, make use of the unique fields.

## Annotations

```java
@Entity(name = "SimpleEntity")
@Table(name = "simple_entities")
public class SimpleEntity {

    /**
     * Entity's ID.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    private Integer           id               = null;

    /**
     * Name of the entity.
     */
    @Column(name = "name", nullable = false, unique = true)
    private String            name             = "";

    public SimpleEntity () {
        super();
    }

    @Override
    public final boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        final SimpleEntity other = (SimpleEntity ) obj;
        return Objects.equals(name, other.name);
    }

    @Override
    public final Integer getId() {
        return id;
    }

    @Override
    public final String getName() {
        return name;
    }

    @Override
    public final int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public final void setId(final Integer identifier) {
        id = checkNotNull(identifier, "Received a null pointer as identifier");
    }

    @Override
    public final void setName(final String value) {
        name = checkNotNull(value, "Received a null pointer as name");
    }

    @Override
    public final String toString() {
        return MoreObjects.toStringHelper(this).add("name", name).toString();
    }

}
```

### Ids

Id columns are annotated with @Id:

```java
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true)
private Integer id = null;
```

When using composite ids these can be defined into a class id:

```java
@Entity(name = "CompositeKeyIdClassEntity")
@Table(name = "composite_key_idclass_entities")
@IdClass(CompositeKey.class)
public class CompositeKeyIdClassEntity implements Serializable {

    private static final long serialVersionUID = -4338241927428303803L;

    @Id
    @Column(name = "id1", nullable = false, unique = true)
    private Integer           id;

    @Id
    @Column(name = "id2", nullable = false, unique = true)
    private Long              supportId;

   // Constructor, getters, setters, etc...

}
```

This way they can be handled as a single object.

### Defining the Entity Name

By default JPA implementations will take the name of the class as the entity name. In this case the entity will be named SomeEntity.

```java
@Entity
@Table(name = "table")
public class SomeEntity
```

This can be changed by defining a name in the annotation. This will set the entity name as NamedEntity:

```java
@Entity(name = "NamedEntity")
@Table(name = "table")
public class SomeEntity
```

### Transient Fields

Any field containing data which should not be persisted has to be marked with the @Transient field. It is not recommended using this annotation, and it may be a hint telling that the entity is doing something else apart from storing persistent data.

## XML

## Additional Constraints

Requirementes may vary between implementations, but there are some recommended thinks to take care of:

* Avoid final classes
* Avoid final getters and setters for lazy fields


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bernardo.gitbook.io/development-docs-java/jpa/jpa-entities.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
