Marco
Marco

Reputation: 15929

Unable to see generated tables generated by hibernate schema export (H2 database)

I am trying to get a small app going using Spring Boot (v1.1.1.RELEASE), and H2 database. In the logging i see that the ddl is correctly generated but it i just cannot find the tables inside the H2 database.

I manually copied the ddl into a db visualizer and the sql is ok. I have no clue what i am missing here. When executing code the JPA persistence layer seems to store the data correctly as i get generated ID's back etc.. I was thinking that i made a mistake in the jdbc url, but they all point to the same file based H2 database. But this database just seems to hold no data.

The JPA object

@Entity
@Table(name = "rawdata", schema = "PUBLIC")
public class RawData {

    @Id
    @GeneratedValue
    private Long id;

    @Lob
    @Column(name = "payload", nullable = false)
    private String payload;

    // getters and setters omitted
}

The JPARepository

@Repository
public interface RawDataRepository extends JpaRepository<RawData, Long> {

}

Application properties

spring.datasource.url=jdbc:h2:file:/home/username/dev-db
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true

Logging info

org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: drop table PUBLIC.rawdata if exists
Hibernate: create table PUBLIC.rawdata (id bigint generated by default as identity, payload clob not null, primary key (id))
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

Test code

@Autowired
private RawDataRepository repository;

repository.saveAndFlush(new RawData("test"));
System.out.println(repository.count());

So saving a JPA object actually seems to persist the object (the count increases etc) but the data and table structure do not appear in the database. I see that the modified date changes of the database when persisting an object but i seem unable to view the data with for example squirrel/dbvisualizer etc.. Any hints or tips?

Upvotes: 1

Views: 3971

Answers (1)

geoand
geoand

Reputation: 64011

The problem is that when the application is shutdown, Hibernate will drop the entire schema, because you have configured spring.jpa.hibernate.ddl-auto=create-drop.

If you change your configuration to spring.jpa.hibernate.ddl-auto=create the schema will not be dropped at the end of the session and you will be able to see the tables that where created along with any data you inserted

Upvotes: 1

Related Questions