Hibernate + Postgres:关系不存在

时间:2019-12-27 16:11:24

标签: postgresql hibernate

我正在尝试使用带有Postgres的Hibernate执行简单的SQL查询。

insert into node_user (USER_EMAIL, USER_NAME, USER_PUBLIC_KEY_BASE64, USER_ID) values ($1, $2, $3, $4)

但是Hibernate说

Caused by: org.postgresql.util.PSQLException: ERROR: relation "node_user" does not exist

我不知道该怎么办。这是我的docker-compose.yml

node-postgres:
    image: postgres:9.5
    container_name: node-postgres
    ports:
      - 15432:5432
    environment:
      POSTGRES_PASSWORD: test
      POSTGRES_USER: test
    volumes:
      - ../init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - node-project

这是我的init.sql

CREATE TABLE node_user(
    USER_ID VARCHAR(256) PRIMARY KEY,
    USER_NAME VARCHAR(256) NOT NULL UNIQUE,
    USER_EMAIL VARCHAR(256) NOT NULL UNIQUE,
    USER_PUBLIC_KEY_BASE64 VARCHAR(2048) NOT NULL UNIQUE
)

这是我的SessionFactory

Properties properties = new Properties();
            properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
            properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
            properties.put("hibernate.connection.url", "jdbc:postgresql://" + masterServerConfig.getDbConfig().getHost() + ":" + masterServerConfig.getDbConfig().getPort() + "/postgres");
            properties.put("hibernate.connection.username", "test");
            properties.put("hibernate.connection.password", "test");
            properties.put("show_sql", "true");
            properties.put("format_sql", "true");
            properties.put(Environment.C3P0_MIN_SIZE, 5);
            properties.put(Environment.C3P0_MAX_SIZE, 20);
            properties.put(Environment.C3P0_TIMEOUT, 1800);
            Configuration configuration = new Configuration();
            configuration.addAnnotatedClass(User.class);
            configuration.setProperties(properties);

这是我的Entity班:

@Entity
@Table(name = "node_user")
public class User implements Serializable {
    private static final long serialVersionUID = 6569577055168857214L;
    @Id
    @Column(name = "USER_ID")
    private String id;
    @Column(name = "USER_NAME")
    private String name;
    @Column(name = "USER_PUBLIC_KEY_BASE64")
    private String publicKeyBase64;
    @Column(name = "USER_EMAIL")
    private String email;

如果我连接到数据库,则可以清楚地在日志中看到该表:

root@e784895a1f3c:/# psql -U test
psql (9.5.20)
Type "help" for help.

test=# \dt
       List of relations
 Schema | Name | Type  | Owner
--------+------+-------+-------
 public | node_user | table | test

我没有任何想法。怎么了?

1 个答案:

答案 0 :(得分:0)

根据您的问题尚不清楚您是否在使用本机查询。如果是,则需要在表名前面添加模式名。您可以对其进行硬编码,但是更好的方法是使用{h-schema}