JAVA JPA - Regular solution to persist all objects in project

JAVA JPA - Regular solution to persist all objects in project

I'm working on a backend which creates new user, news, various data etc.

I'm working on a backend which creates new user, news, various data etc.

So far I could create and persist a new User in the database. I think there will be many further entities also with relations. At this point I only see two way to persist the data:

  • create CreateXY, UpdateXY and DeleteXY classes
  • or code the persist method in each entity object

I think that is quiet redundant to write the persists again and again. Is there a nice way to go to persist objects in a nice way?

package database;

import java.util.logging.Level; import java.util.logging.Logger;

import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence;

import backend.User;

public class Main { private static final Logger LOGGER = Logger.getLogger("JPA");

public static void main(String[] args) {
    Main main = new Main();;

public void run() {
    EntityManagerFactory factory = null;
    EntityManager entityManager = null;
    try {
        factory = Persistence.createEntityManagerFactory("shareiffUnit");
        entityManager = factory.createEntityManager();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        if (entityManager != null) {
        if (factory != null) {

private void persistPerson(EntityManager entityManager) {
    EntityTransaction transaction = entityManager.getTransaction();
    try {
        User person = new User();
    } catch (Exception e) {
        if (transaction.isActive()) {


package backend;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity @Table public class User {

private int id;
private String name;
private String password;
private Boolean isActive = false;

public int getId() {
    return id;
public void setId(int id) { = id;
public String getName() {
    return name;
public void setName(String name) { = name;
public String getPassword() {
    return password;
public void setPassword(String password) {
    this.password = password;
public Boolean isActive() {
    return isActive;
public void setActive(boolean isActive) {
    this.isActive = isActive;


java hibernate jpa

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

A Guide to JPA with Hibernate - Relationship Mapping

In this article, we'll dive into Relationship Mapping with JPA and Hibernate in Java. JPA is the persistence standard of the Java ecosystem. It allows us to map our domain model directly to the database structure and then gives us the flexibility of manipulating objects in our code - instead of messing with cumbersome JDBC components like Connection, ResultSet, etc.

Understand to Spring Boot: MySQL and JPA, Hibernate

Spring Boot uses Hibernate as the default JPA implementation. The property spring.jpa.hibernate.ddl-auto is used for database initialization. I've used the value “update” for this property.

Spring Boot + JPA + Hibernate + Oracle

In this tutorial, we will learn how to create a Spring Boot application that communicates with an Oracle data source through Hibernate.

Multitenant configuration: StaleObjectStateException on Transaction (hibernate + spring-data-jpa)

i'm trying to setup a configuration for manage a multi-tenant enviroment with spring-boot, spring-data-jpa, hibernate and mysql (same schema, every tenant table has a tenant_code column). For dependencies the parent maven project is&nbsp;<strong>spring-boot-starter-parent</strong>&nbsp;(2.1.2.RELEASE).