Saltar al contenido

Cree una aplicación simple usando Hibernate en SAP HANA

Ahora probemos nuestras entidades creadas con lo siguiente:

  1. crea dos desarrolladores, John Doe y Jane Doe.
  2. crea un proyecto con John y agrega a Jane como colaboradora.
  3. Se crea la versión 1.0 del proyecto.
  4. buscar el proyecto probado y probar su contenido

Crea una clase Java llamada TestSimple en un paquete llamado com.sap.hana.hibernate.tutorial.simple (haga clic con el botón derecho en el proyecto y seleccione Nuevo -> Clase o usa el Archivo -> Nuevo -> Clase barra de menú), luego pegue el siguiente contenido:

package com.sap.hana.hibernate.tutorial.simple;

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

public class TestSimple {

  private static Developer createDeveloper(String name, String eMail) {
    Developer developer = new Developer(eMail);
    developer.setName(name);
    return developer;
  }

  private static Project createProject(String name, Developer owner) {
    Project project = new Project(name);
    project.setProjectOwner(owner);
    return project;
  }

  private static Version createVersion(String versionNumber, Project project) {
    Version version = new Version(versionNumber, project);
    return version;
  }

  public static void main(String[] args) {
    try {
      EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Tutorial");
      EntityManager entityManager = entityManagerFactory.createEntityManager();

      entityManager.getTransaction().begin();

      System.out.println("************************************************************************");
      System.out.println(" >> Creating entities");
      System.out.println("************************************************************************");

      Developer john = createDeveloper("John Doe", "john@doe.com");
      entityManager.persist(john);

      Developer jane = createDeveloper("Jane Doe", "jane@doe.com");
      entityManager.persist(jane);

      Project project = createProject("John's big project", john);
      project.addContributor(jane);

      Version version = createVersion("1.0", project);
      project.addVersion(version);

      entityManager.persist(project);

      project.addVersion(version);

      entityManager.persist(version);

      entityManager.getTransaction().commit();
      entityManager.clear();

      System.out.println("************************************************************************");
      System.out.println(" >> Creating entities completed");
      System.out.println("************************************************************************");

      System.out.println("************************************************************************");
      System.out.println(" >> Finding project by name");
      System.out.println("************************************************************************");

      Project johnsBigProject = entityManager.find(Project.class, "John's big project");

      System.out.println("************************************************************************");
      System.out.println(" >> Getting project name : " + johnsBigProject.getName());
      System.out.println(" >> Getting project owner: " + johnsBigProject.getProjectOwner().getName());
      System.out.println("************************************************************************");

      assert johnsBigProject.getName().equals("John's big project");
      assert johnsBigProject.getProjectOwner().getName().equals("John Doe");
      System.out.println("************************************************************************");
      entityManager.clear();
      entityManager.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.exit(0);
  }
}

Guarde el archivo de la clase.

Ejecute la aplicación haciendo clic derecho en el archivo de clase y seleccionando Ejecutar como -> Aplicación Java o haga clic en el icono.

Debería ver el siguiente registro de salida en su consola (solo el stdout):

Hibernate:

    drop table Developer cascade
Hibernate:

    drop table Project cascade
Hibernate:

    drop table Project_Developer cascade
Hibernate:

    drop table Version cascade
Hibernate: create column table Developer (eMailAddress varchar(255) not null, name varchar(255), primary key (eMailAddress))
Hibernate:

    alter table Project
       add constraint FK3oeqfkq5jn0kjy2mdaft7rp5u
       foreign key (projectOwner_eMailAddress)
       references Developer
Hibernate:

    alter table Project_Developer
       add constraint FKfjmrxrx78c50vodgyffyfu1ni
       foreign key (contributors_eMailAddress)
       references Developer
Hibernate:

    alter table Project_Developer
       add constraint FKs6slwia2sper8m6sqjma71g3d
       foreign key (Project_name)
       references Project
Hibernate:

    alter table Version
       add constraint FK9igf86no0jfeogkcj9q46l290
       foreign key (project_name)
       references Project
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@3b2c0e88'
************************************************************************
 >> Creating entities
************************************************************************
Hibernate:
    insert
    into
        Developer
        (name, eMailAddress)
    values
        (?, ?)
Hibernate:
    insert
    into
        Developer
        (name, eMailAddress)
    values
        (?, ?)
Hibernate:
    insert
    into
        Project
        (projectOwner_eMailAddress, name)
    values
        (?, ?)
Hibernate:
    insert
    into
        Version
        (releaseDate, project_name, versionNumber)
    values
        (?, ?, ?)
Hibernate:
    insert
    into
        Project_Developer
        (Project_name, contributors_eMailAddress)
    values
        (?, ?)
************************************************************************
 >> Creating entities completed
************************************************************************
************************************************************************
 >> Finding project by name
************************************************************************
************************************************************************
 >> Getting project name : John's big project
 >> Getting project owner: John Doe
************************************************************************
************************************************************************

Esta salida muestra las declaraciones SQL generadas para ingresar los valores en la base de datos y volver a leer el proyecto desde la base de datos.