【问题标题】:Relation doesn't exist even with schema specified即使指定了架构,关系也不存在
【发布时间】:2022-01-23 14:33:12
【问题描述】:

尝试使用 JPA 发出请求时出现错误。

我已经在我的类实体中指定了表所在的架构:

@Data 
@NoArgsConstructor 
@AllArgsConstructor 
@Entity
@Table(schema = "dwp_schema")
public class Corridor {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id_corridor;
    private Integer id_floor;
    private String orientation;

}

当我在我的存储库中执行特定请求时:

public interface CorridorRepository extends JpaRepository<Corridor, Integer> {
    @Query(value = "select * from corridor c inner join floor f on f.id_floor=c.id_floor INNER JOIN building b on f.id_building = b.id_building WHERE b.building_name=?1 AND f.floor_number=?2" ,nativeQuery = true)
    List<Corridor> getCorridorsByFloor(String building_name, int floor);
}

我在 Postgres 中有以下错误:

org.postgresql.util.PSQLException: ERROR: relation "corridor" does not exist

有人有想法吗?

谢谢。

【问题讨论】:

    标签: java spring postgresql spring-data-jpa


    【解决方案1】:

    您需要在连接字符串中指定您的 DB 和 Schema:

    jdbc:postgresql://localhost:5432/dbname?currentSchema=dwp_schema
    

    无论如何,将架构名称放在实体声明中是个坏主意 - 您的 DBA 应该能够决定架构名称。

    【讨论】:

      【解决方案2】:

      尝试在表名前写模式名:

          @Query(value = "select * from dwp_schema.corridor c inner join floor f on f.id_floor=c.id_floor INNER JOIN building b on f.id_building = b.id_building WHERE b.building_name=?1 AND f.floor_number=?2" ,nativeQuery = true)

      【讨论】:

      • 是的,谢谢,我做到了,即使对我拥有的每张桌子都这样做很烦人,它也能正常工作。因为我们在同一个项目中处理两个模式。
      猜你喜欢
      • 2021-06-02
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      相关资源
      最近更新 更多