Doronz
Doronz

Reputation: 704

CrudRepository not returning database elements from API

I'm building a Spring boot app which populates data into a hibernate database. I have written DAOs which pull data from an external API and load them into my database (this works fine). However, I'd like to generate the REST API for my data using CrudRepository. So I created a NewsItemRepository which extends it.

public interface NewsItemRepository extends CrudRepository<NewsItem, Long>

My NewsItemDao is what does the persisting and fetching to/from the database:

@Override
public List<NewsItem> fetchAllNewsItems() {

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<NewsItem> criteria = builder.createQuery(NewsItem.class);
    Root<NewsItem> newsItemRoot = criteria.from(NewsItem.class);
    criteria.orderBy(builder.desc(newsItemRoot.get("date")));

    List<NewsItem> newsItemList = entityManager.createQuery(criteria).getResultList();

    entityManager.close();

    return newsItemList;
}

    @Override
public void save(NewsItem newsItem) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(newsItem);
    entityManager.getTransaction().commit();
    entityManager.close();
}

The API functionality appears to be running (localhost:8080/api/v1):

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

However, newsItems are not being populated at localhost:8080/api/v1/newsItems:

{
  "_embedded" : {
    "newsItems" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/v1/newsItems"
    },
    "profile" : {
      "href" : "http://localhost:8080/api/v1/profile/newsItems"
    }
  }
}

I am able to access the newsItems from the database and display them. But I would like to get the API functionality working and I do not know what I am missing to get it to work. I did notice that two additional blank tables were added to my database and nowhere did I indicate for that to happen / specify their names. news_item and news_source (I also have a news source entity). The populated tables are newsitem and newssource.

Please let me know if any further information is needed to provide an answer.

Upvotes: 0

Views: 393

Answers (1)

Ahsan Iqbal
Ahsan Iqbal

Reputation: 316

The reason you are getting new tables in DB is that hibernate by default maps your POJO entity to its unqualified class name equivalent i.e NewsItem to news_item and you might have specified in your persistence.xml

hibernate.hbm2ddl.auto = create-drop | update

There are two solutions to this 1. You use the convention that hibernate uses by default and rename your tables to news_item and news_source 2. You can specify table name at the top of the POJO class

@Entity
@Table(name="newsitem")
public class NewsItem implements Serializable {}

Upvotes: 1

Related Questions