質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

5719閲覧

ManyToOneでSQLのエラー

adkinchan55

総合スコア11

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2018/11/14 12:06

編集2018/11/14 21:03

JavaでRDBの内容を表示するwebアプリケーションを作っておりまして、現在2つのテーブルをJPAのOneToManyとManyToOneを使って結合をしたいのですが、実行後にブラウザ上でエラーが出ます。

以下2つの対象テーブルです。
patent_jp_utility_as
patent_jp_utility_a_products

patent_jp_utility_a_productsが多となります。

以下はpatent_jp_utility_asの構成の表です。
primary keyはsource_idです。

ColumnTypeCollationNullable
source_idbigintnot null
application_idbigintnot null
number_4character(4)not null
number_6character(6)not null
publication_atdatenot null

以下はpatent_jp_utility_a_productsの構成の表です。
primary key はproduct_idです。
source_idは外部キー制約等はしてません。

ColumnTypeCollationNullable
product_idbigintnot null
source_idbigintnot null
copyrightbigint
upload_account_idbigintnot null
created_attimestamp without time zonenot null
updated_attimestamp without time zone
deleted_attimestamp without time zone
upload_user_idbigintnot null

やりたいSQLは以下の通りです。

select s.source_id ,application_id ,product_id ,upload_account_id from patent_jp_utility_a_products as p inner join patent_jp_utility_as as s on p.source_id = s.source_id where p.upload_account_id = '1010000002';

表示したい表は以下の通りです。

やりたいのはブラウザ上で以下のように出すことです。

source_idapplication_idproduct_idupload_account_id
1994080950199314995810000000171010000002
1994240150199320329210000000121010000002
1994240150199320329210000000041010000002
1995130250199329903210000000111010000002

以上のイメージをすべく、javaでそれぞれのエンティティとrepositoryを作成しました。

該当のソースコード

以下Sourceクラスです。

@Entity @Table(name="patent_jp_utility_as") public class Source { @Id @Column(name = "source_id") @NotNull private long sourceId; public long getSourceId() {return sourceId;} public void setSourceId(long sourceId) {this.sourceId = sourceId;} @Column(name = "application_id") @NotNull private long applicationId; public long getApplicationId() {return applicationId;} public void setApplicationId(long applicationId) {this.applicationId = applicationId;} @Column(name = "number_4") @NotNull private String number4; public String getNumber4() {return number4;} public void setNumber4(String number4) {this.number4 = number4;} @Column(name = "number_6") @NotNull private String number6; public String getNumber6() {return number6;} public void setNumber6(String number6) {this.number6 = number6;} @Column(name="publication_at") @NotNull private Timestamp publicationAt; public Timestamp getPublicationAt() {return publicationAt;} public void setPublicationAt(Timestamp publicationAt) {this.publicationAt = publicationAt;} @OneToMany(mappedBy = "source", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @Column(nullable = true) private List<Product> products; public List<Product> getProducts(){return products;} public void setProducts(List<Product> products) {this.products = products;} }

以下Productクラスです。

@Entity @Table(name="patent_jp_utility_a_products") public class Product { public Product() { super(); source = new Source(); } @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @NotNull @Column(name="product_id") private long productId; public long getProductId() {return productId;} public void setProductId(long productId) {this.productId = productId;} @Column(name="source_id") @NotNull private long sourceId; public long getSourceId() {return sourceId;} public void setSourceId(long sourceId) {this.sourceId = sourceId;} @ManyToOne private Source source; public Source getSource() {return source;} public void setSource(Source source) {this.source = source;} @Column(name="copyright") private long copyright; public long getCopyright() {return copyright;} public void setCopyright(long copyright) {this.copyright = copyright;} @Column(name="upload_account_id") @NotNull private long uploadAccountId; public long getUploadAccountId() {return uploadAccountId;} public void setUploadAccountId(long uploadAccountId) {this.uploadAccountId = uploadAccountId;} @Column(name="upload_user_id") @NotNull private long uploadUserId; public long getUploadUserId() {return uploadUserId;} public void setUploadUserId(long uploadUserId) {this.uploadUserId = uploadUserId;} @Column(name="created_at") @NotNull private Timestamp createdAt; public Timestamp getCreatedAt() {return createdAt;} public void setCreatedAt(Timestamp createdAt) {this.createdAt = createdAt;} @Column(name="updated_at") private Timestamp updatedAt; public Timestamp getUpdatedAt() {return updatedAt;} public void setUpdatedAt(Timestamp updatedAt) {this.updatedAt = updatedAt;} @Column(name="deleted_at") private Timestamp deletedAt; public Timestamp getDeletedAt() {return deletedAt;} public void setDeletedAt(Timestamp deletedAt) {this.deletedAt = deletedAt;} }

以下プロダクトリポジトリのインターフェースです。

public interface ProductRepository extends JpaRepository<Product, Long> { public List<Product> findByUploadAccountId(long accountId); }

以下コントローラーです。
※肥大化してますのでポイントだけ載せます。

@Controller public class HeloController { @Autowired UserRepository repository; @Autowired UserDetailsServiceImpl service; @Autowired PatentRepository patentrepository; @Autowired PatentService patentService; @Autowired ProductRepository productRepository; @Autowired EditionRepository editionRepository; @Autowired FileRepository fileRepository; @Autowired SourceRepository sourceRepository; //問題のメソッド @GetMapping(value="/mypage") public ModelAndView mypage( ModelAndView mav ) { //ログインユーザーの情報取得 String username; Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { username = ((UserDetails)principal).getUsername(); }else { username = principal.toString(); } User user = repository.findByUsername(username); long accountId = user.getAccountId(); Product product = new Product(); Source source = new Source(); List<Product> products = new ArrayList<>(); //問題のレポジトリ //このレポジトリを実行したときにエラーとなります。 products = productRepository.findByUploadAccountId(accountId); //以下省略 mav.setViewName("mypage"); return mav; } }

発生している問題・エラーメッセージ

springbootでSTSから起動すると問題なく通ります。
しかし、ブラウザでhttp://localhost:8080/mypageにアクセスすると

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Nov 14 20:14:39 JST 2018 There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

となります。

そのときのコンソールは以下のようになっています。

2018-11-14 20:14:39.186 WARN 15180 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42703 2018-11-14 20:14:39.186 ERROR 15180 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: column product0_.source_source_id does not exist ポジション: 159 2018-11-14 20:14:39.217 ERROR 15180 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause org.postgresql.util.PSQLException: ERROR: column product0_.source_source_id does not exist ポジション: 159 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) ~[postgresql-42.2.2.jar:42.2.2] at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178) ~[postgresql-42.2.2.jar:42.2.2] at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306) ~[postgresql-42.2.2.jar:42.2.2] at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441) ~[postgresql-42.2.2.jar:42.2.2] at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365) ~[postgresql-42.2.2.jar:42.2.2] at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155) ~[postgresql-42.2.2.jar:42.2.2] at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:118) ~[postgresql-42.2.2.jar:42.2.2] at 以下略

となります。

試したこと

こちらの内容をためしましたが、JoinColumnを入れるとコンパイルエラーとなりました。
リンク内容

補足情報(FW/ツールのバージョンなど)

SpringBoot2.0.3
postgresql10
windows10
sts3.9.4

以上です、長くなりましたがよろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

すいません、自己解決しました。
Productクラスのほうを以下のようにJoinColumnを追加したらできました。

@ManyToOne @JoinColumn(name="source_id",insertable=false, updatable=false) private Source source; public Source getSource() {return source;} public void setSource(Source source) {this.source = source;}

投稿2018/11/15 03:01

編集2018/11/15 03:04
adkinchan55

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問