找不到Hibernate @OneToMany / @ManyToOne列

时间:2017-04-30 18:30:40

标签: java spring hibernate jpa spring-boot

尝试使用Spring-BootHibernateCrudRepository界面尝试为我的网络应用调用REST API时,收到“找不到列”错误。

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

@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]

Sample row from my NewsItem table

Sample row from my NewsSource table

NewsApi.org Api

2 个答案:

答案 0 :(得分:1)

框架似乎试图根据camelcase添加额外的下划线。这似乎违反了默认的JPA行为,它应该只在关系名称和外来ID之间添加下划线。

无论如何..尝试明确定义连接列:

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

答案 1 :(得分:0)

NEWSITEM0_.NEWS_SOURCE_ID和给定表的最后一项NEWSSOURCE_ID是不同的。使它一样。

希望它能解决您的问题。