Skip to content

Instantly share code, notes, and snippets.

@alejandro-du
Last active June 1, 2018 09:54
Show Gist options
  • Save alejandro-du/7bd5a2926c2c6389f737a5a133a1cd30 to your computer and use it in GitHub Desktop.
Save alejandro-du/7bd5a2926c2c6389f737a5a133a1cd30 to your computer and use it in GitHub Desktop.

pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.10.Final</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.196</version>
</dependency>

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="demo-pu">
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/temp/demo;DB_CLOSE_ON_EXIT=FALSE" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

JPAService.java

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.function.Function;

/**
 * @author Alejandro Duarte
 */
public class JPAService {

    private static EntityManagerFactory factory;

    static {
        init();
    }

    public static void init() {
        if (factory == null) {
            factory = Persistence.createEntityManagerFactory("demo-pu");
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        factory.close();
    }

    public static EntityManagerFactory getFactory() {
        return factory;
    }

    public static <T> T runInTransaction(Function<EntityManager, T> function) {
        EntityManager entityManager = null;

        try {
            entityManager = JPAService.getFactory().createEntityManager();
            entityManager.getTransaction().begin();

            T result = function.apply(entityManager);

            entityManager.getTransaction().commit();
            return result;

        } finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
    }

}

Comentario.java

import javax.persistence.*;
import java.util.Date;

/**
 * @author Alejandro Duarte
 */
@Entity
@Table(name = "comentarios")
public class Comentario {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String contenido;

    private Date fecha;

    public Comentario() {
        fecha = new Date();
    }

    public Comentario(String contenido) {
        this();
        setContenido(contenido);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContenido() {
        return contenido;
    }

    public void setContenido(String contenido) {
        this.contenido = contenido;
    }

    public Date getFecha() {
        return fecha;
    }

    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

}

ComentarioRepository.java

import java.util.List;

/**
 * @author Alejandro Duarte
 */
public class ComentarioRepository {

    public static List<Comentario> findAll() {
        return JPAService.runInTransaction(em ->
                em.createQuery("select c from Comentario c").getResultList()
        );
    }

    public static void save(Comentario comentario) {
        JPAService.runInTransaction(em -> {
            em.persist(comentario);
            return null;
        });
    }

    public static void delete(Comentario comentario) {
        JPAService.runInTransaction(em -> {
            Comentario c = em.find(Comentario.class, comentario.getId());
            em.remove(c);
            return null;
        });
    }

}

shared-styles.html

<custom-style>
    <style>
        html {
            --lumo-font-family: Courier, monospace;
            --lumo-border-radius: 2em;
            --lumo-base-color: #fafbfc;
            --lumo-primary-color: #00b4f0;
        }

        .main-view {
            box-shadow: .5em .5em .5em rgba(0,0,0, .3);
            margin: 2em;
            paddin: 2em;
            background-color: #def;
            max-width: 600px;
        }
    </style>
</custom-style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment