Hibernate postgres - relation" userdetails"不存在

时间:2015-03-02 18:28:50

标签: java hibernate postgresql

我刚开始使用hibernate并且我有一个简单的配置,它给了我这个错误:当我尝试首次保存我唯一的类时,“relation”userdetails“不存在。”


我的hibernate.gfg.xml文件:

<!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.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">XXXX</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.PostgreSQLDialect
        </property>

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="hdm2ddl.auto">create</property>

        <mapping class="hibernate.project.UserDetails"/>

    </session-factory>

</hibernate-configuration>

我唯一的模特课:

@Entity
public class UserDetails {

    @Id
    private int userId;
    private String userName;

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }

我的主要:

public static void main(String[] args) {
        UserDetails user = new UserDetails();
        user.setUserId(1);
        user.setUserName("First User");

        Configuration config = new Configuration().configure();


        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);


        Session session=sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }

任何想法?我发现了类似的问题,但没有帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

create属性不正确:使用hbm2ddl而不是hdm2ddl

    <property name="hibernate.hbm2ddl.auto">create</property>

OR

    <property name="hbm2ddl.auto">create</property>

而不是

hdm2ddl.auto

似乎是你最后的错字。