2017-10-19 16 views
0

最近、私は別の質問の重複であることが判明したスタックオーバーフローに関する非常によく似た質問をしました。その他の質問には、私が問題を適用して解決した回避策がありました。今、今回は、回避策が機能せず、他のすべての解決策が機能しません。また、最初のスレッドにリンクされた他のスレッドのすべてのソリューションは機能しません。JPA:孤児の削除、無効な列

SQLServerException: Invalid column name

そして、これが重複した:

これが最初で、私の質問だった私がリンクされ、関連のセクションで右のトピックをチェックした

hibernate column name issues

が、私の問題の解決策を見つけることができません。私はまた私の問題が起こる理由を理解することができません。 宣言ファイル(彼らは問題とは無関係ですので、私はここに私の他のテーブルには言及しています)、対応するクラスと

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; 
    } 
} 

は、私は2つのテーブルを持っています

および

@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

を春のブートを実行しています。そこに誰もがありますなぜこれが起こったのか、私はそれが起こっている理由を知っていれば、私は自分の問題を解決できるかもしれない。今、私は完全に立ち往生しています。

ありがとうございます。

EDIT:

[OK]を、私は私自身の問題を解決し、事故によってそのような問題は最初の場所で発生した理由を、私はまだ知りません。このトピックのこの回答(複数可)によると:

mappedBy

JPA JoinColumnあなたは私の場合は@ManyToOne & @OnyToMany

を使用する私は、Fileクラスに@ManyToOneを使用する必要はありません。私はDeclarationクラスで@OneToManyだけ必要です。この注釈を削除してもエラーは発生しません。

誰かがこの問題の原因を知っている場合は、将来私または他の人のために使用できるように回答を提供してください。

+0

あなたの質問はかなり混乱しています。実行したコードは明らかではありません。あなたが行ったことを正確に教えてください:無効な列名 'declaration_id_number' "。クエリが2つのクエリを使用する理由また、それらはクエリではなく、テンプレートです。また、「私が試したこと」、つまりそれぞれのコードを実行するコードと最後の3行がなぜ存在するのかを理解するために、どのように訴訟を起こしているのかははっきりしない。 [mcve]を読んで行動してください。 – philipxy

答えて

0

私の場合、Fileクラスで@ManyToOneを使用する必要はありません。私はDeclarationクラスで@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は、あなたがそれをDeclarationテーブルのエントリを割り当てない限り、あなたは、この表には何も保存することができないことを意味するNOT NULLするdeclaraedされます。
  • 外部キー制約を定義しました。これは、ファイルレコードを保存するときにデータベースがこれをチェックすることを意味します。 JPAが正しくエンティティをマッピングすることができますし、自動的にそれがあなたのデータベーススキーマに対応するように、あなたが@ManyToOne注釈が必要

    • 、または

    この

  • は、次の2つのオプションを持っていることを意味し外部キーフィールド declaration_idおよび対応する参照整合性制約を Fileテーブルから削除します。この場合、永続性プロバイダは、カスタマイズしない限り、デフォルトで結合テーブルを作成します。

あなたはすなわち@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属性を削除しました。
  • 大括弧の中にテーブル/カラム名を入れているのはなぜですか?彼らはコードを読むことができないし、私はそれを使用する利点が表示されません。
関連する問題