简体   繁体   中英

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

I have this issue =>

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

I want to add a new entity with a oneToMany relationship with the appUser. I search on stackoverflow some response but many time it's "just" the schema name the problem. So I added the schema name to each entities but it still doesn't work.

Maybe the issue is in my request?

There's the entities:

AppUser:

@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<>();

Performance:

@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;

And the request:

 @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);

And there's the liquibase xml:

appUser:

<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>

Performance:

    <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>

and his constraints

<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>

Thank you

Your @Id column is named internal_user_id and the relation is appUser , which translates to app_user . So Hibernate guesses that the Performance.appUser JoinColumn is app_user_internal_user_id .

It's the job of Naming Strategies to guess physical column names when their are not explicit!

I guess you're using SpringImplicitNamingStrategy which implements org.hibernate.boot.model.naming.ImplicitNamingStrategy , it's worth reading the doc or this nice article: https://www.baeldung.com/hibernate-naming-strategy

I Find the solution. Don't know why but it was searching a app_user_internal_user_id column wich doesn't exist. I added @JoinColumn(name = "user_id") to performance entity and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM