【问题标题】:JPA: Delete orphans, invalid columnJPA:删除孤儿,无效列
【发布时间】:2018-03-31 05:57:20
【问题描述】:

最近我在 Stack Overflow 上提出了一个非常相似的问题,结果证明是另一个问题的重复。在另一个问题中,有一个解决方法,我应用并解决了我的问题。现在,这一次解决方法不起作用,并且所有其他提到的解决方案都不起作用。此外,链接到第一个线程的其他线程的所有解决方案都不起作用。

这是我最初的问题:

SQLServerException: Invalid column name

这是重复的:

hibernate column name issues

我已经检查了链接和相关部分右侧的主题,但找不到解决问题的方法。我也无法理解我的问题发生的原因。

我有 2 个表:DeclarationFile(这里我不会提及我的其他表,因为它们与问题无关)

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/

JPA JoinColumn vs mappedBy

我想要的:删除声明,自动删除声明相关的文件

我得到了什么:列名“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


【解决方案1】:

就我而言,我不需要在 File 类中使用 @ManyToOne。我的声明类中只需要@OneToMany。删除此注释后不再出现错误。

我认为这行不通。如果去掉@ManyToOne注解,持久化提供者会默认创建一个连接表来维护关系。你的意思可能是你没有得到任何例外。但是看看数据库架构:

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])
);
  • declaration_id 被声明为 NOT NULL,这意味着您无法在此表中保存任何内容,除非您在 Declaration 表中为其分配一个条目。
  • 您已经定义了一个外键约束,这意味着您的数据库将在您保存文件记录时对其进行检查。

这意味着你有两个选择:

  • 您需要一个 @ManyToOne 注释,以便 JPA 可以正确、自动地映射与您的数据库架构相对应的实体,或者

  • 您从File 表中删除了外键字段declaration_id 和相应的参照完整性约束。在这种情况下,持久化提供程序会默认为您创建一个连接表,除非您自定义它。

所以如果你想使用第一个选项,即@ManyToOne注解,你必须将实体映射如下:

@Entity
@Table(name = "[file]")
public class File {

    @Id
    private int number;
    private String path;

    @ManyToOne
    @JoinColumn(name = "declaration_id")
    private Declaration declaration;

    public int getDeclaration_id() {
        return declaration_id;
    }

    // ... getters and setters
}

和一个稍作修改的Declaration实体:

@Entity
public class Declaration {

     @Id
     private int number;
     // ... other fields

    @OneToMany(mappedBy = "declaration",orphanRemoval = true)
    private List<File> files = new ArrayList<>();

   // ... Rest of the code
}

注意事项:

  • 我从注释中删除了targetEntity = File.class 属性,因为您不需要它,因为您的集合已经暗示了该类型。
  • 为什么要将表/列名放在方括号中?它们使代码不可读,我看不到使用它的好处。

【讨论】:

    猜你喜欢
    • 2010-09-23
    • 2013-06-05
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2011-01-02
    相关资源
    最近更新 更多