Search code examples
javahibernateintellij-ideajunithsqldb

Hibernate can't generate id


I've created a new project on my IntelliJ IDEA, using hibernate (+ hibernate entitymanager) 5, hsqldb 1.8 (for testing only), as well as junit 4.

I've got my test class

UserRoundRelationsTest

EntityManagerFactory emf;
EntityManager em;

Date currentDate;
User user;
Round round1;
Round round2;

And some @After and @Before methods:

@Before
public void saveDummyUserAndTwoDummyRounds() {

    emf = Persistence.createEntityManagerFactory("testPersistence");
    em = emf.createEntityManager(); // Retrieve an application managed entity manager

    currentDate = new Date();
    user = new User.UserBuilder("alias").fullName("Full Name").phoneNumber("Phone Number").height(100).weight(100).birthDate(currentDate).build();

    round1 = new Round(new GameMode(1, ButtonTargetStrategy.RANDOM, ButtonSkipStrategy.SKIP_ON_MISTAKE));
    round2 = new Round(new GameMode(2, ButtonTargetStrategy.RANDOM, ButtonSkipStrategy.SKIP_ON_MISTAKE));

    round1.setUser(user);
    round2.setUser(user);

    HashSet<Round> rounds = new HashSet<Round>(2);
    rounds.add(round1);
    rounds.add(round2);

    em.getTransaction().begin();

    user.setRounds(rounds);

    em.persist(user);
    em.persist(round1);
    em.persist(round2);

    em.flush();
    em.getTransaction().commit();
}

and

@After
public void deleteData() {

    em.remove(user);
    em.remove(round2);
    em.remove(round2);

    em.getTransaction().commit();

    em.close();
    emf.close();
}

Firstly, when I try to run the test, hibernate can't find the tables for User and Round classes, as well as everything they are dependent on (a class called ButtonPress for example). I assume this happens due to the automatic create-drop I have configured at my persistence.xml

<persistence-unit name="testPersistence">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>com.carloworks.model.User</class>
    <class>com.carloworks.model.Round</class>
    <class>com.carloworks.model.ButtonPress</class>
    <properties>
        <!--<property name="hibernate.archive.autodetection" value="class,hbm"></property>-->

        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"></property>
        <property name="hibernate.show_sql" value="true"></property>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"></property>
        <property name="hibernate.connection.username" value="sa"></property>
        <property name="hibernate.connection.password" value=""></property>
        <property name="hibernate.connection.url"
                  value="jdbc:hsqldb:mem:buttonMasherModelsTestDb;MV_STORE=FALSE;MVCC=FALSE"></property>
        <!--<property name="hibernate.default_schema" value="buttonMasherModelsTestDb"/>-->
        <property name="hibernate.hbm2ddl.auto" value="create-drop"></property>
    </properties>
</persistence-unit>

Scrolling further down the error logs, I also get an error at this line:

em.persist(user);

null identifier (plus 'This function is not supported'/'GenericJDBCException: could not prepare statement').

What did I do wrong?

The project is also available on Github

Full Error Log


Solution

  • Changing the version to the latest one fixed the problem *Some may experience a broken path error (caused by maven). If you are using intelliJ navigate to Project Settings -> Modules and Fix any problems under the "Problems" tab.