我有这个问题=>
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>
谢谢您
2条答案
按热度按时间r7knjye21#
我找到了解决办法。不知道为什么,但它正在搜索一个不存在的app\u user\u internal\u user\u id列。我将@joincolumn(name=“user\u id”)添加到performance实体中,它可以正常工作。
628mspwn2#
你的
@Id
列已命名internal_user_id
关系是appUser
,也就是app_user
. 所以hibernate猜测Performance.appUser
joincolumn是app_user_internal_user_id
.它的命名策略的工作猜测物理列名时,他们是不明确的!
我猜你在用
SpringImplicitNamingStrategy
哪个实现了org.hibernate.boot.model.naming.ImplicitNamingStrategy
,值得一读博士或这篇好文章:https://www.baeldung.com/hibernate-naming-strategy