繁体   English   中英

找不到Hibernate @OneToMany / @ManyToOne列

[英]Hibernate @OneToMany / @ManyToOne Column not found

尝试使用Spring-BootHibernateCrudRepository接口为我的Web应用程序调用REST API时,出现“找不到列”错误。

我有两个实体,一个新闻来源和一个新闻项目。 新闻来源(例如:Abc新闻)有许多新闻项目。

@Entity
@Table(name="newsitem")
public class NewsItem

@ManyToOne
private NewsSource newsSource;

@JsonIgnoreProperties(ignoreUnknown=true)
@Entity
@Table(name="newssource")
public class NewsSource

@OneToMany(mappedBy = "newsSource", cascade = CascadeType.ALL)
private List<NewsItem> newsItems;

我的两个表都正确填充,可以在我的H2控制台中进行验证。

关系注释在我的NewsItem表中添加了一个名为Newssource_id的列。 但是,它不是自动填充的。 因此,当从外部API提取项目时,我添加了代码来分配它。

 public Future<List<NewsItem>> fetchAllNewsItemsFromApi(){
    List<NewsSource> sources = newsSourceDao.fetchAllNewsSources();
    List<NewsItem> newsItems = new ArrayList<>();
    for (NewsSource source : sources) {
        String request = "https://newsapi.org/v1/articles?apikey=" + NEWSAPI_API_KEY + "&source=" + source.getId();
        ResponseEntity<NewsItemResponse> newsItemResponse =
                restTemplate.exchange(request,
                        HttpMethod.GET, null, NewsItemResponse.class);
        NewsItemResponse response = newsItemResponse.getBody();
        List<NewsItem> itemsFromSource = response.getNewsItemList();
        for (NewsItem item : itemsFromSource){
            item.setNewsSource(source);
        }
        newsItems.addAll(itemsFromSource);
    }
    return new AsyncResult<>(newsItems);
}

我必须创建两个附加的响应类,因为NewsApi JSON除了返回文章/资源之外,还返回元数据。

public class NewsItemResponse {
    @JsonProperty("articles")
    private List<NewsItem> newsItemList;

public class NewsSourceResponse {
    @JsonProperty("sources")
    private List<NewsSource> sourceList;

我已经像这样设置了一个NewsItemRepository:

public interface NewsItemRepository extends CrudRepository<NewsItem, Long>{}

我的http://localhost:8080/api/v1看起来像这样:

{
  "_links" : {
    "newsItems" : {
      "href" : "http://localhost:8080/api/v1/newsItems"
    },
    "profile" : {
      "href" : "http://localhost:8080/api/v1/profile"
    }
  }
}

当我导航到http://localhost:8080/api/v1/newsItems出现500错误。

在浏览器中:

could not prepare statement; SQL [select newsitem0_.id as id1_0_, newsitem0_.version as version2_0_, newsitem0_.author as author3_0_, newsitem0_.date as date4_0_, newsitem0_.news_source_id as news_sou9_0_, newsitem0_.summary as summary5_0_, newsitem0_.title as title6_0_, newsitem0_.url as url7_0_, newsitem0_.url_to_image as url_to_i8_0_ from newsitem newsitem0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

在IntelliJ中:

org.h2.jdbc.JdbcSQLException: Column "NEWSITEM0_.NEWS_SOURCE_ID" not found; SQL statement:
select newsitem0_.id as id1_0_, newsitem0_.version as version2_0_, newsitem0_.author as author3_0_, newsitem0_.date as date4_0_, newsitem0_.news_source_id as news_sou9_0_, newsitem0_.summary as summary5_0_, newsitem0_.title as title6_0_, newsitem0_.url as url7_0_, newsitem0_.url_to_image as url_to_i8_0_ from newsitem newsitem0_ [42122-191]

我的NewsItem表中的示例行

我的NewsSource表中的示例行

NewsApi.org Api

似乎该框架试图基于驼峰添加其他下划线。 这似乎违背了默认的JPA行为,该行为仅应在关系名称和外部ID之间添加下划线。

无论如何..尝试显式定义join列:

@ManyToOne
@JoinColumn(name="newssource_id")
private NewsSource newsSource;

NEWSITEM0_.NEWS_SOURCE_ID和Given表的最后一项NEWSSOURCE_ID是不同的。 一样

希望它能解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM