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です。
Column | Type | Collation | Nullable |
---|---|---|---|
source_id | bigint | not null | |
application_id | bigint | not null | |
number_4 | character(4) | not null | |
number_6 | character(6) | not null | |
publication_at | date | not null |
以下はpatent_jp_utility_a_productsの構成の表です。
primary key はproduct_idです。
source_idは外部キー制約等はしてません。
Column | Type | Collation | Nullable |
---|---|---|---|
product_id | bigint | not null | |
source_id | bigint | not null | |
copyright | bigint | ||
upload_account_id | bigint | not null | |
created_at | timestamp without time zone | not null | |
updated_at | timestamp without time zone | ||
deleted_at | timestamp without time zone | ||
upload_user_id | bigint | not 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_id | application_id | product_id | upload_account_id |
---|---|---|---|
1994080950 | 1993149958 | 1000000017 | 1010000002 |
1994240150 | 1993203292 | 1000000012 | 1010000002 |
1994240150 | 1993203292 | 1000000004 | 1010000002 |
1995130250 | 1993299032 | 1000000011 | 1010000002 |
以上のイメージをすべく、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
以上です、長くなりましたがよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。