Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple H2 and Hibernate/JPA

Tags:

hibernate

jpa

h2

Simple test with H2 as the database, JPA and Hibernate. Gives no discerning error, but it does not persist the entity. For sure I am missing something extremely simple

persistence.xml in META-INF/:

<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd" version="1.0">

  <persistence-unit name="thePersistenceUnit" transaction-type="RESOURCE_LOCAL">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>entities.Person</class>

    <properties>
        <property name="connection.driver_class" value="org.h2.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:h2:./db/repository"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.show_sql" value="true" />

    </properties>
</persistence-unit>

the simple entity :

@Entity
public class Person {

    @Id
    @GeneratedValue
    private Integer id;
    private String firstName;

    public Integer getId() {
        return id;
    }

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


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

the test:

public class Testing {
    @Test
    public void test2(){

        EntityManagerFactory factory = Persistence.createEntityManagerFactory("thePersistenceUnit");
        EntityManager theManager = factory.createEntityManager();
        assertNotNull(theManager);

        Person person = new Person();
        person.setFirstName("ana");
        theManager.persist(person);
        System.out.println(person.getId());

        Person p = (Person)theManager.find(Person.class, 1);
        System.out.println(person.getId());

        assertNotNull(p);
    }
}

the result :

Aug 16, 2013 1:48:20 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Aug 16, 2013 1:48:20 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.0.1.Final}
Aug 16, 2013 1:48:20 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 16, 2013 1:48:20 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 16, 2013 1:48:21 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 16, 2013 1:48:21 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000148: No JDBC Driver class was specified by property hibernate.connection.driver_class
Aug 16, 2013 1:48:21 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Aug 16, 2013 1:48:21 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
Aug 16, 2013 1:48:21 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [null] at URL [jdbc:h2:./db/repository]
Aug 16, 2013 1:48:21 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {autocommit=true, release_mode=auto}
Aug 16, 2013 1:48:21 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Aug 16, 2013 1:48:21 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Aug 16, 2013 1:48:21 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Aug 16, 2013 1:48:21 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Aug 16, 2013 1:48:21 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Aug 16, 2013 1:48:21 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: drop table Person if exists
Hibernate: create table Person (id integer generated by default as identity, firstName varchar(255), lastName varchar(255), primary key (id))
null
Hibernate: select person0_.id as id0_0_, person0_.firstName as firstName0_0_, person0_.lastName as lastName0_0_ from Person person0_ where person0_.id=?
null

junit.framework.AssertionFailedError
    at test.Testing.test2(Testing.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

The question: why doesn't it persist the instance/why doesn't it throw any error, etc ?

like image 528
vlad dd Avatar asked Aug 16 '13 10:08

vlad dd


People also ask

Can we use hibernate with H2 database?

In this Hibernate H2 database tutorial, you will learn how to create a Hibernate Application to connect the H2 in-memory database. Hibernate is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

What is the difference between JPA and Hibernate?

Conclusion: The major difference between Hibernate and JPA is that Hibernate is a framework while JPA is API specifications. Hibernate is the implementation of all the JPA guidelines.

What is JPA H2?

H2 is an open-source in-memory SQL database written in Java. It can be embedded in Java applications or used as a standalone database in client-server mode. An in-memory database is created when the application starts up and is destroyed when the application shuts down.

Is Hsqldb same as H2?

They summarize the history pretty wonderfully. First came HSQLDB (Hypersonic SQL) then came H2, but HSQLDB and H2 do not share any of the same code and are separate projects as of today. @specialk1st: Right, the heritage in this case is a common author, not common code.


1 Answers

You are trying to persist a record in the database without opening a transaction. This is not possible. What you should do is:

    EntityManager theManager = factory.createEntityManager();
    theManager .getTransaction().begin();
    Person person = new Person();
    person.setFirstName("ana");
    theManager.persist(person);
    theManager.getTransaction().commit();
like image 94
George Papatheodorou Avatar answered Sep 19 '22 16:09

George Papatheodorou