jeudi 27 août 2015

Spring's EmbeddedDatabaseBuilder not generating primary key collumns

I am trying to setup in-memory database using Spring's EmbeddedDatabaseBuilder, however i am facing the error where id is always null, which is probably related to sequence generator, or lack thereof.

How can i have the EmbeddedDatabaseBuilder to generate primary keys or let me insert them manually, neither seems to work.

I am using HibernateJpaVendorAdapter property to build database from entities:

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

Here is error log when i don't specify id manually, from what i can tell id column is not even generated:

.12:14:16.263 INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update 
.12:14:16.263 INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000102: Fetching database metadata 
.12:14:16.275 INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000396: Updating schema 
.12:14:16.319 INFO  o.h.tool.hbm2ddl.TableMetadata - HHH000261: Table found: public.voucher 
.12:14:16.319 INFO  o.h.tool.hbm2ddl.TableMetadata - HHH000037: Columns: [consumption_user, code, consumption_date, serial, expiry_date, creation_date, state, type, class_name] 
.12:14:16.319 INFO  o.h.tool.hbm2ddl.TableMetadata - HHH000108: Foreign keys: [] 
.12:14:16.320 INFO  o.h.tool.hbm2ddl.TableMetadata - HHH000126: Indexes: [voucher_pkey] 
.12:14:16.325 ERROR o.h.tool.hbm2ddl.SchemaUpdate - HHH000388: Unsuccessful: alter table voucher add column id int4 not null 
.12:14:16.325 ERROR o.h.tool.hbm2ddl.SchemaUpdate - ERROR: column "id" contains null values 
.12:14:16.326 INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000232: Schema update complete 
org.springframework.web.context.support.GenericWebApplicationContext@5bfbf16f: startup date [Thu Aug 27 12:14:12 CEST 2015]; root of context hierarchy 
    .12:14:19.026 INFO  o.h.e.j.b.internal.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements 
    .12:14:19.026 WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42703 
    .12:14:19.026 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Batch entry 0 insert into voucher (class_name, code, consumption_date, consumption_user, creation_date, expiry_date, serial, state, type, id) values (NULL, '123', NULL, NULL, '2015-08-27 12:14:18.922000 +02:00:00', NULL, NULL, 'T', '1', 1) was aborted.  Call getNextException to see the cause. 
    .12:14:19.026 WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42703 
    .12:14:19.027 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ERROR: column "id" of relation "voucher" does not exist

And here when i remove manual id assignment :

.12:17:32.094 WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42703 .12:17:32.095 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ERROR: column voucherent0_.id does not exist

Configuration:

@Bean
public EntityManagerFactory entityManagerFactory(DataSource dataSource) throws ClassNotFoundException {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    // doesn't seem to generate pkey collumn
    vendorAdapter.setGenerateDdl(true);
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);     
    Properties props = new Properties();
    props.setProperty(HIBERNATE_BATCH_SIZE_PROPERTY, "50");
    factory.setJpaProperties(props);

    factory.setPackagesToScan(DATA_ACCESS_LAYER);
    factory.setDataSource(dataSource);
    factory.afterPropertiesSet();
    return factory.getObject();
}

@Bean
@Primary
public DataSource dataSource() {

    return new EmbeddedDatabaseBuilder().addDefaultScripts()
                                        .setType(EmbeddedDatabaseType.HSQL)
                                        .build();
}

Entity id definition:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "voucher_id_seq")
@SequenceGenerator(name = "voucher_id_seq", sequenceName = "voucher_id_seq", allocationSize = 1)
@Column(name = "id")
public Integer id;

I am using Spring Data JPa to persist entities:

    VoucherEntity v = new VoucherEntity();
    // doesn't work with or without this
    v.id = 2;
    v.code = "123";
    v.type = "1";
    v.state = "T";
    // delegate to save
    writeVoucher.add(v)

Aucun commentaire:

Enregistrer un commentaire