[英]org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist - Hibernate
[英]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 具有 oneToMany 關系的新實體。 我在stackoverflow上搜索了一些響應,但很多時候它“只是”模式名稱問題。 所以我為每個實體添加了架構名稱,但它仍然不起作用。
也許問題出在我的要求中?
有實體:
應用用戶:
@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>
謝謝
您的@Id
列名為internal_user_id
,關系為appUser
,轉換為app_user
。 所以 Hibernate 猜測Performance.appUser
JoinColumn 是app_user_internal_user_id
。
當物理列名不明確時,命名策略的工作就是猜測它們!
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
我找到解決方案。 不知道為什么,但它正在搜索一個不存在的 app_user_internal_user_id 列。 我將 @JoinColumn(name = "user_id") 添加到性能實體並且它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.