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?

Main.java

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();
    main.run();
}

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

private void persistPerson(EntityManager entityManager) {
    EntityTransaction transaction = entityManager.getTransaction();
    try {
        transaction.begin();
        User person = new User();
        person.setName("Homer");
        person.setPassword("Simpson");
        entityManager.persist(person);
        transaction.commit();
    } catch (Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
    }
}

}

User.java

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

@Entity @Table public class User {

@Id
@GeneratedValue
private int id;
private String name;
private String password;
private Boolean isActive = false;


public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.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;
}

}


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

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).