Hibernate不会创建表

时间:2018-05-07 16:34:29

标签: java postgresql hibernate

我正在使用Hibernate和PostgresSQL,并尝试使用.xml创建一个表,但没有成功。

启动Main.class时没有看到错误。也许我应该使用另一个版本的Hibernate?

我正在使用Gradle并添加了最新版本的hibernate-core,hibernate-entitymanager和hibernate-validator的依赖项。

我一直在观看2016年发布的视频:可能存在问题。

Hibernate cfg:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
        <property name="connection.username">pavel</property>
        <property name="connection.password">31228900</property>
        <property name="connection.pool_size">10</property>
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="planetofUsers.cfg.xml"/>

    </session-factory>

</hibernate-configuration>


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="hibernateLesson.Planet" table="planet">
        <id name="id" >
            <generator class="native" />
        </id>
        <property name="age" column="age" type="int" />
        <property name="firstName" column="first_name" type="string" />
        <property name="lastName" column="last_name" type="string" />

    </class>
</hibernate-mapping>

Java类:

import lombok.Data;
import lombok.experimental.FieldDefaults;

import static lombok.AccessLevel.PRIVATE;

@Data
@FieldDefaults(level = PRIVATE)
public class Planet {
    long id;
    int age;
    String firstName;
    String

import lombok.experimental.FieldDefaults;
import org.hibernate.SessionFactory;

import static lombok.AccessLevel.PRIVATE;


public class Main {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    }
}

package hibernateLesson;

import lombok.experimental.FieldDefaults;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import static lombok.AccessLevel.PRIVATE;

@FieldDefaults(level = PRIVATE)
public class HibernateUtil {
    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;
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下设置:

hibernate.hbm2ddl.auto = create