N
N
Nikolai Gerasimov2015-10-02 18:15:13
Java
Nikolai Gerasimov, 2015-10-02 18:15:13

Why doesn't Hibermate create tables?

In general, I study Hibernate. I'm trying to create tables in the database, I seem to have done everything as in the tutorial, but there are no records about creating tables. Here is what is output in the console.

"C:\Program Files\Java\jdk1.8.0_20\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\bin" -Dfile.encoding=windows-1251 -classpath "C:\Program Files\Java\jdk1.8.0_20\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\zipfs.jar;C:\Users\Николай\IdeaProjects\myDb\target\classes;C:\Users\Николай\.m2\repository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-core\5.0.1.Final\hibernate-core-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\org\jboss\logging\jboss-logging\3.3.0.Final\jboss-logging-3.3.0.Final.jar;C:\Users\Николай\.m2\repository\org\apache\geronimo\specs\geronimo-jta_1.1_spec\1.1.1\geronimo-jta_1.1_spec-1.1.1.jar;C:\Users\Николай\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;C:\Users\Николай\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;C:\Users\Николай\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\Николай\.m2\repository\org\jboss\jandex\1.2.2.Final\jandex-1.2.2.Final.jar;C:\Users\Николай\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\Николай\.m2\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;C:\Users\Николай\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.0.0.Final\hibernate-commons-annotations-5.0.0.Final.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-entitymanager\5.0.1.Final\hibernate-entitymanager-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-osgi\5.0.1.Final\hibernate-osgi-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\org\osgi\org.osgi.core\4.3.1\org.osgi.core-4.3.1.jar;C:\Users\Николай\.m2\repository\org\osgi\org.osgi.compendium\4.3.1\org.osgi.compendium-4.3.1.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-envers\5.0.1.Final\hibernate-envers-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-c3p0\5.0.1.Final\hibernate-c3p0-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\com\mchange\c3p0\0.9.2.1\c3p0-0.9.2.1.jar;C:\Users\Николай\.m2\repository\com\mchange\mchange-commons-java\0.2.3.4\mchange-commons-java-0.2.3.4.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-proxool\5.0.1.Final\hibernate-proxool-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\proxool\proxool\0.8.3\proxool-0.8.3.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-infinispan\5.0.1.Final\hibernate-infinispan-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\org\infinispan\infinispan-core\7.2.1.Final\infinispan-core-7.2.1.Final.jar;C:\Users\Николай\.m2\repository\org\infinispan\infinispan-commons\7.2.1.Final\infinispan-commons-7.2.1.Final.jar;C:\Users\Николай\.m2\repository\org\jgroups\jgroups\3.6.2.Final\jgroups-3.6.2.Final.jar;C:\Users\Николай\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.1_spec\1.0.1.Final\jboss-transaction-api_1.1_spec-1.0.1.Final.jar;C:\Users\Николай\.m2\repository\org\jboss\marshalling\jboss-marshalling-osgi\1.4.10.Final\jboss-marshalling-osgi-1.4.10.Final.jar;C:\Users\Николай\.m2\repository\org\rhq\helpers\rhq-pluginAnnotations\3.0.4\rhq-pluginAnnotations-3.0.4.jar;C:\Users\Николай\.m2\repository\org\hibernate\hibernate-ehcache\5.0.1.Final\hibernate-ehcache-5.0.1.Final.jar;C:\Users\Николай\.m2\repository\net\sf\ehcache\ehcache-core\2.4.3\ehcache-core-2.4.3.jar;C:\Users\Николай\.m2\repository\org\slf4j\slf4j-api\1.6.1\slf4j-api-1.6.1.jar;C:\Users\Николай\.m2\repository\org\springframework\spring-core\4.2.1.RELEASE\spring-core-4.2.1.RELEASE.jar;C:\Users\Николай\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
окт 02, 2015 6:08:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.1.Final}
окт 02, 2015 6:08:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
окт 02, 2015 6:08:58 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
окт 02, 2015 6:08:58 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
окт 02, 2015 6:08:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
окт 02, 2015 6:08:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/mydbtest]
окт 02, 2015 6:08:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
окт 02, 2015 6:08:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
окт 02, 2015 6:08:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
окт 02, 2015 6:08:59 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
окт 02, 2015 6:08:59 PM org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
окт 02, 2015 6:09:00 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
окт 02, 2015 6:09:00 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete

hibernate config file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydbtest</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.show_sql">true</property>

        <mapping class="models.User"/>
        <mapping class="models.Order"/>
        <mapping class="models.Product"/>
        <mapping class="models.ProductCategory"/>
        <mapping class="models.Role"/>
    </session-factory>
</hibernate-configuration>

HibernateUtil class code
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static SessionFactory sessionFactory = null;

    static {
        Configuration cfg = new Configuration().configure();
        StandardServiceRegistryBuilder builder =  new StandardServiceRegistryBuilder()
                .applySettings(cfg.getProperties());

        sessionFactory = cfg.buildSessionFactory(builder.build());
    }

    public static SessionFactory getSessionFactory(){
        return  sessionFactory;
    }
}

User class code
package models;

import javax.persistence.*;

/**
 * Created by Николай on 30.09.2015.
 */

@Entity
@Table (name = "user")
public class User extends Model{

    @Column (name = "age")
    private int age;

    public int getAge() {
        return age;
    }

    public String getFirstName() {

        return firstName;
    }

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

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column (name = "firstName")
    private String  firstName;

    @Column (name = "lastName")
    private String lastName;

    @OneToOne
    @PrimaryKeyJoinColumn
    private Role role;

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public User(long id){
        super(id);
    }

    public User(){
        super();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2016-03-04
@IvanCher

Maybe someone else will come in handy, because the behavior is rather non-obvious and is not explained anywhere.
I'm just getting started with java and decided to use Java SE8, Hibernate 5.1, JPA for a small desktop application.
Generate a database schema from annotated entity classes using SchemaExport from the command line.
SchemaExport does not yet support annotated entity classes out of the box, it waits for old *.hbm.xml mapping files.
The command has a simple syntax
All options options can be found in the org.hibernate.tool.hbm2ddl.SchemaExport class, lines 499 to 551. Everything is clear there.
If you are using hibernate.cfg.xml, you can specify this file in the --config option.
This HibernateUtil class is just a small example. It only generates SQL commands and inserts them into the init.sql file.
You can make a wrapper class for SchemaExport to have all its features, just come up with some option to add annotated entity classes. Although, if they ask me, then I can write such a class, it's not difficult at all.
The core-team says that they don't have time to deal with tools for hibernate now, so it's not known how long to wait for this change from them.
Just a whole article turned out, I hope it will be useful to someone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question