Caused by: org.postgresql.util.PSQLException: ERROR: column performanc3_.app_user_internal_user_id does not exist

Caused by: org.postgresql.util.PSQLException: ERROR: column performanc3_.app_user_internal_user_id does not exist

我有这个问题=>

2020-12-08 10:10:31.472 ERROR 7184 --- [  XNIO-1 task-4] c.f.timesheet.service.AppUserService     : Exception in findOne() with cause = 'org.hibernate.exception.SQLGrammarException: could not extract ResultSet' and exception = 'could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet'

Caused by: org.postgresql.util.PSQLException: ERROR: column performanc3_.app_user_internal_user_id does not exist

我想添加一个与 appUser 具有一对多关系的新实体。 我在 Whosebug 上搜索了一些响应,但很多时候它“只是”模式名称问题。 所以我将架构名称添加到每个实体,但它仍然不起作用。

也许问题出在我的要求上?

有实体:

应用用户:

@Entity
@Table(name = "app_user", schema = "public")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class AppUser implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "internal_user_id", unique = true, nullable = false)
    private Long id;

    @Column(name = "phone")
    private String phone;

    @OneToOne(cascade = CascadeType.ALL)
    @MapsId
    private User internalUser;

    @ManyToMany
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @JoinTable(
        name = "app_user_job",
        joinColumns = @JoinColumn(name = "internal_user_id"),
        inverseJoinColumns = @JoinColumn(name = "job_id", referencedColumnName = "id")
    )
    private Set<Job> jobs = new HashSet<>();

    @ManyToOne
    @JsonIgnoreProperties(value = "appUsers", allowSetters = true)
    private Company company;

    @OneToMany(mappedBy = "appUser",cascade = CascadeType.REMOVE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private Set<Performance> performances = new HashSet<>();

性能:

@Entity
@Table(name = "performance", schema = "public")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Performance implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Min(value = 0)
    @Max(value = 16)
    @Column(name = "hours", nullable = false)
    private Integer hours;

    @NotNull
    @Column(name = "date", nullable = false)
    private LocalDate date;

    @ManyToOne
    @JsonIgnoreProperties(value = "performances_user", allowSetters = true)
    private AppUser appUser;

    @ManyToOne
    @JsonIgnoreProperties(value = "performances_job", allowSetters = true)
    private Job job;

请求:

 @Query(
        "select appUser from AppUser appUser " +
        "left join fetch appUser.jobs " +
        "left join appUser.performances " +
        " where appUser.id =:id"
    )
    Optional<AppUser> findOneWithEagerRelationships(@Param("id") Long id);

还有 liquibase xml:

应用用户:

<changeSet id="20201111182357-1" author="jhipster">
    <createTable tableName="app_user">
        <column name="internal_user_id" type="bigint">
            <constraints primaryKey="true" nullable="false"/>
        </column>

        <column name="phone" type="varchar(255)">

            <constraints nullable="true" />
        </column>
        <column name="company_id" type="bigint">
            <constraints nullable="true" />
        </column>
        <!-- jhipster-needle-liquibase-add-column - JHipster will add columns here -->
    </createTable>
</changeSet>
<changeSet id="20201111182357-1-relations" author="jhipster">

    <createTable tableName="app_user_job">
        <column name="job_id" type="bigint">
            <constraints nullable="false"/>
        </column>
        <column name="internal_user_id" type="bigint">
            <constraints nullable="false"/>
        </column>
    </createTable>
    <addPrimaryKey columnNames="internal_user_id, job_id" tableName="app_user_job"/>
    
</changeSet>

性能:

    <changeSet id="20201202093141-1" author="jhipster">
    <createTable tableName="performance">
        <column name="id" type="bigint">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="hours" type="integer">
            <constraints nullable="false" />
        </column>
        <column name="date" type="date">
            <constraints nullable="false" />
        </column>
        <column name="user_id" type="bigint">
            <constraints nullable="true" />
        </column>
        <column name="job_id" type="bigint">
            <constraints nullable="true" />
        </column>
        <!-- jhipster-needle-liquibase-add-column - JHipster will add columns here -->
    </createTable>
</changeSet>

和他的约束

<changeSet id="20201202093141-2" author="jhipster">
    
    <addForeignKeyConstraint baseColumnNames="user_id"
                             baseTableName="performance"
                             constraintName="fk_performance_user_id"
                             referencedColumnNames="internal_user_id"
                             referencedTableName="app_user"/>

    <addForeignKeyConstraint baseColumnNames="job_id"
                             baseTableName="performance"
                             constraintName="fk_performance_job_id"
                             referencedColumnNames="id"
                             referencedTableName="job"/>

</changeSet>

谢谢

我找到了解决办法。 不知道为什么,但它正在搜索不存在的 app_user_internal_user_id 列。我将 @JoinColumn(name = "user_id") 添加到性能实体并且它有效。

您的 @Id 列名为 internal_user_id,关系为 appUser,转换为 app_user。 所以 Hibernate 猜测 Performance.appUser JoinColumn 是 app_user_internal_user_id.

命名策略的工作是在物理列名称不明确时猜测它们!

我猜你正在使用实现 org.hibernate.boot.model.naming.ImplicitNamingStrategySpringImplicitNamingStrategy,值得阅读文档或这篇不错的文章:https://www.baeldung.com/hibernate-naming-strategy