0

I am trying to set up a simple Hibernate + Spring JPA repository example in my project. I am using an oracle database, for reference.

I have a simple entity declares as follows

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "item")
public class Item {
    
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private long Id;
    
    private String name;
    private String detail;
    
}

When running the project, hibernate creates a table named hte_item in oracle.

I have set up a controller which tries to fetch all the items in the table. I have created the following repository

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface ItemRepository extends CrudRepository<Item, Long> {
    List<Item> findAll();
}

When running itemRepository.findAll, instead, Spring JPA runs the following query to the database.

select i1_0.id,i1_0.detail,i1_0.name from item i1_0

Which throws an Oracle error ORA-00942: Table or view does not exist

If you notice, the query does not include the prefix generated by Hibernate, which generates the Oracle error. Am I missing something related to configuration here?

Mikel Urkia
  • 2,087
  • 1
  • 23
  • 40

2 Answers2

1

The 'hte' prefix means that it's a temporary table generated by hibernate. It's not the table for your entity.

Basically, you need to create your table, either manually or through some automated process. See, for example, How to create database schema in hibernate first time and further update it in case of schema modification?

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
1

When running the project, hibernate creates a table named hte_item in oracle.

The hte_item table is a temporary table used by Hibernate for certain bulk update and delete queries. It is not the name of the table where Items are made persistent.

Since you've specified @Table(name = "item") you must have a table named item in your database. If you want Hibernate to export that table for you, then you need to explicitly enable schema export, by setting a config property:

jakarta.persistence.schema-generation.database.action=create

or whatever.

Gavin King
  • 3,182
  • 1
  • 13
  • 11