[英]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.我想添加一个与 appUser 具有 oneToMany 关系的新实体。 I search on stackoverflow some response but many time it's "just" the schema name the problem.
我在stackoverflow上搜索了一些响应,但很多时候它“只是”模式名称问题。 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:还有 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
.您的
@Id
列名为internal_user_id
,关系为appUser
,转换为app_user
。 So Hibernate guesses that the Performance.appUser
JoinColumn is app_user_internal_user_id
.所以 Hibernate 猜测
Performance.appUser
JoinColumn 是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 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.
不知道为什么,但它正在搜索一个不存在的 app_user_internal_user_id 列。 I added @JoinColumn(name = "user_id") to performance entity and it works.
我将 @JoinColumn(name = "user_id") 添加到性能实体并且它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.