【发布时间】:2018-03-31 05:57:20
【问题描述】:
最近我在 Stack Overflow 上提出了一个非常相似的问题,结果证明是另一个问题的重复。在另一个问题中,有一个解决方法,我应用并解决了我的问题。现在,这一次解决方法不起作用,并且所有其他提到的解决方案都不起作用。此外,链接到第一个线程的其他线程的所有解决方案都不起作用。
这是我最初的问题:
SQLServerException: Invalid column name
这是重复的:
我已经检查了链接和相关部分右侧的主题,但找不到解决问题的方法。我也无法理解我的问题发生的原因。
我有 2 个表:Declaration 和 File(这里我不会提及我的其他表,因为它们与问题无关)
CREATE TABLE [dbo].[Declaration] (
[number] INT NOT NULL,
[status] VARCHAR (50) NOT NULL,
[name] VARCHAR (50) NOT NULL,
[description] VARCHAR (250) NOT NULL,
[amount] FLOAT (53) NOT NULL,
[date] DATE NOT NULL,
[period_id] INT NOT NULL,
[client_project_id] INT NOT NULL,
PRIMARY KEY CLUSTERED ([number] ASC),
CONSTRAINT [fk_client_period] FOREIGN KEY ([client_project_id]) REFERENCES [dbo].[ClientProject] ([number]),
CONSTRAINT [fk_period] FOREIGN KEY ([period_id]) REFERENCES [dbo].[Period] ([number])
);
CREATE TABLE [dbo].[File] (
[number] INT NOT NULL,
[path] VARCHAR (50) NOT NULL,
[declaration_id] INT NOT NULL,
PRIMARY KEY CLUSTERED ([number] ASC),
CONSTRAINT [fk_file] FOREIGN KEY ([declaration_id]) REFERENCES [dbo].[Declaration] ([number])
);
对应的类:
@Entity
@Table(name = "[file]")
public class File {
@Id
private int number;
private String path;
@ManyToOne(targetEntity = Declaration.class)
private int declaration_id;
public int getDeclaration_id() {
return declaration_id;
}
public void setDeclaration_id(int declaration_id) {
this.declaration_id = declaration_id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
和
@Entity
public class Declaration {
@Id
private int number;
private String status;
private String name;
private String description;
private double amount;
private Date date;
private int period_id;
private int client_project_id;
@OneToMany(targetEntity = File.class,mappedBy = "declaration_id",orphanRemoval = true)
private List<File> files = new ArrayList<>();
public List<File> getFiles() {
return files;
}
public void setFiles(List<File> files) {
this.files = files;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number= number;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getPeriod_id() {
return period_id;
}
public void setPeriod_id(int period_id) {
this.period_id = period_id;
}
public int getClient_project_id() {
return client_project_id;
}
public void setClient_project_id(int client_project_id) {
this.client_project_id = client_project_id;
}
}
我已经根据这些主题和教程定义了我的 @ManyToOne 和 @OneToMany 关系:
https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
我想要的:删除声明,自动删除声明相关的文件
我得到了什么:列名“declaration_id_number”无效。
我尝试过的:
- renaming fields in database to declaration_id_number (results in declaration_id_number_number)
- using @Column(name="declaration_id") on declaration_id field
- using @Colum(name="declaration_id") on the getter field
- using @JoinColumn(name="fk_file") on the declaration_id field
- Using different kinds of naming stategies (in application.properties), including the default one
spring.jpa.hibernate.naming.strategy: org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
实际的 SQL 查询:
select files0_.declaration_id_number as declarat3_3_0_, files0_.number as number1_3_0_, files0_.number as number1_3_1_, files0_.declaration_id_number as declarat3_3_1_, files0_.path as path2_3_1_ from [file] files0_ where files0_.declaration_id_number=?
select declaratio0_.number as number1_2_0_, declaratio0_.amount as amount2_2_0_, declaratio0_.client_project_id as client_p3_2_0_, declaratio0_.date as date4_2_0_, declaratio0_.description as descript5_2_0_, declaratio0_.name as name6_2_0_, declaratio0_.period_id as period_i7_2_0_, declaratio0_.status as status8_2_0_ from declaration declaratio0_ where declaratio0_.number=?
我正在使用 JPA Hibernate 5.2.10 运行 Spring Boot
有没有人知道为什么会发生这种情况,如果我知道为什么会发生这种情况,我也许可以自己解决问题。现在我完全被卡住了。
提前致谢。
编辑:
好的,所以我偶然解决了自己的问题,我仍然不知道为什么会出现问题。根据本主题的这个答案:
JPA JoinColumn vs mappedBy
你使用@ManyToOne & @OnyToMany
就我而言,我不需要在 File 类中使用 @ManyToOne。我的声明类中只需要@OneToMany。删除此注释后不再出现错误。
如果有人知道此问题的原因,请提供答案,以便将来对我或其他人有用。
【问题讨论】:
-
你的问题比较混乱。目前尚不清楚您运行的是什么代码。告诉我们您究竟做了什么来“获取:无效的列名 'declaration_id_number'”。为什么您的查询是两个查询?此外,它们不是查询,它们是模板。此外,尚不清楚我们应该如何理解您的“我尝试过的”列表,即您将为每个代码运行什么代码,以及为什么最后三行存在。请阅读minimal reproducible example并采取行动。
标签: java mysql spring hibernate jpa