Hibernate postgres - 关系 "userdetails" 不存在。

4

我刚开始学习Hibernate,我的简单配置让我尝试保存唯一的类时出现了错误:“关系‘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();
    }

有什么想法吗?我找到了类似的问题,但没有帮助。谢谢!

我认为这是一个类似于这样的问题:https://dev59.com/E2bWa4cB1Zd3GeqPWGOe - pL4Gu33
我不知道在我的代码中应该改变什么,与你的例子相比,我没有带名称的注释... - lou
我的意思是“Postgres(以前使用,不确定新版本)将表名转换为小写。” - pL4Gu33
@pL4Gu33:我明白了,只是不知道该怎么办才好:( - lou
1个回答

2
创建属性不正确:请使用hbm2ddl而不是hdm2ddl。
    <property name="hibernate.hbm2ddl.auto">create</property>

或者

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

而不是

hdm2ddl.auto

你的拼写似乎有误。


这似乎是mysql和postgres的基本配置之间的区别,也是工作和不工作之间的区别。Mysql似乎不需要像这样的条目。谢谢。 - Doomed Mind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接