0

I keep table schema in this file tableaddress.orm.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1"
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <entity class="Address">
        <attributes>
            <basic name="city" attribute-type="String" />
            <basic name="country" attribute-type="int" />
            <basic name="province" attribute-type="double" />
            <basic name="postalCode" attribute-type="boolean">
            </basic>
            <basic name="street" attribute-type="String" />
        </attributes>
    </entity>
</entity-mappings>

Here is how I tried to create table using Hibernate

public class App 
{
    public static void main( String[] args )
    {

    Properties prop= new Properties();
    prop.setProperty("hibernate.connection.url", "jdbc:mariadb:......");
    prop.setProperty("dialect", "org.hibernate.dialect.MariaDB53Dialect");
    prop.setProperty("hibernate.connection.username", "user");
    prop.setProperty("hibernate.connection.password", "password");
    prop.setProperty("hibernate.connection.driver_class", "org.mariadb.jdbc.Driver");

     SessionFactory sessionFactory = new Configuration()
             .addResource("tableaddress.orm.xml").addProperties(prop).buildSessionFactory();
     Session session = sessionFactory.openSession();
     session.beginTransaction();
     session.getTransaction().commit();
     session.close(); 
    }
} 

It should work, there is no compile errors, but for some reason the table is not created

Error java.lang.NoClassDefFoundError: javax/transaction/SystemException

john
  • 647
  • 5
  • 23
  • 53
  • Wouldn’t you need to set the hibernate.hbm2ddl.auto property in the hibernate.cfg.xml ? See https://stackoverflow.com/questions/4507142/does-hibernate-create-tables-in-the-database-automatically – racraman Jul 10 '19 at 03:30
  • Getting the same error java.lang.NoClassDefFoundError: javax/transaction/SystemException – john Jul 10 '19 at 14:19

1 Answers1

1

This is probably due to bug/feature in Hibernate.

The javax.transaction was removed from Hibernate (well, marked as "provided") in 5.0.4, but it was brought back in 5.0.7 (see https://hibernate.atlassian.net/browse/HHH-10307 :

"JTA no longer transitively provided (HHH-10178) causes problems for apps not using JTA"

)

So assuming you are using 5.04, 5.0.5 or 5.0.6, your choices are to either upgrade Hibernate, or to add the following dependency :

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
    <version>1.1</version>
</dependency>
racraman
  • 4,988
  • 1
  • 16
  • 16
  • Than you. But I'm using the latest version 5 – john Jul 10 '19 at 21:26
  • Version 6 is beta – john Jul 10 '19 at 21:26
  • Ok, upgrading would have been good if you were in an older version. Still, adding the hat dependency will probably fix the issue, but shouldn’t be needed. Could you edit your question to include the hibernate and other database-related dependencies from your Pom ? – racraman Jul 11 '19 at 00:22
  • Thank you. That was the solution to NoClass error. However, it got replaced with a new error `org.hibernate.AnnotationException: Unable to load class defined in XML: Address` . I don't have a class `Address` . I only want to create a database table from xml file that's it. No classes. Should I open a new question and accept this one. Please let me know either way – john Jul 11 '19 at 19:19
  • I see that https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html section 3.1.2 says “An entity has to have a class attribute referring to the java class”, so unfortunately might not be possible - but that’s going outside my knowledge, so yes, new question. – racraman Jul 12 '19 at 00:26