【问题标题】:Hibernate postgres - relation "userdetails" does not existHibernate postgres - 关系“userdetails”不存在
【发布时间】:2015-03-02 18:28:50
【问题描述】:

我刚开始使用 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();
    }

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

【问题讨论】:

  • 我认为这是一个这样的案例问题:stackoverflow.com/questions/11595599/…
  • 相对于您的示例,我看不到我应该在代码中更改什么,我没有带有名称的注释...
  • 我的意思是“Postgres(过去用于,不确定是否在新版本)将表名转换为小写。”
  • @pL4Gu33:我明白了,我只是不明白我应该怎么做:(

标签: java hibernate postgresql


【解决方案1】:

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

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

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

而不是

hdm2ddl.auto

您的最后似乎是错字。

【讨论】:

  • 这似乎是 mysql 与 postgres 的基本配置之间的区别,以及工作和不工作之间的区别。 Mysql 似乎不需要这样的条目。谢谢。
猜你喜欢
  • 2019-08-02
  • 2020-04-09
  • 2018-12-02
  • 1970-01-01
  • 2018-05-15
  • 2015-08-13
  • 1970-01-01
  • 2021-01-13
  • 2016-04-11
相关资源
最近更新 更多