java - Hibernate @OneToMany/@ManyToOne 列未找到

标签 java spring hibernate jpa spring-boot

尝试使用 Spring-Boot 为我的 Web 应用程序调用 REST API 时,出现“未找到列”错误, HibernateCrudRepository界面。

我有两个实体,一个新闻源和一个新闻项。新闻来源(例如: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

最佳答案

框架似乎试图在驼峰式的基础上添加额外的下划线。这似乎违反了默认的 JPA 行为,默认的 JPA 行为应该只在关系名称和外部 id 之间添加下划线。

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

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

关于java - Hibernate @OneToMany/@ManyToOne 列未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709898/

相关文章:

java - 如何在JAVA中保持(2^(n-1) mod 1 =1)的更大值

java - 在另一个包的接口(interface)文件中使用类

java - 使用 Spring @Profile 加载 log4j2 xml 配置而不是 maven <profile>

java - 从 IntelliJ 使用 Hibernate 连接到 PostgreSQL 服务器

java - JxMaps 可以禁用 Google 的默认 POI

java - AlertDialog OnClose 事件 Android

java - 多个监听器的 KafkaListener ConsumerConfig AUTO_OFFSET_RESET_DOC 最早

spring - 跳过 Spring Batch 文件中的页眉、正文和页脚行

java - 如何通过 Maven2 pom.xml 获取 Hibernate + javax.persistence

java - Hibernate关系一对多映射问题