4

Here , I want to hardcode some things into db at the action of server starts with Spring commandline runner

Problem :- In this I have checked that if 1L isn't present then do entry for that with Id 1L but still it stores to incremental Id when 1L or 2L or 3L is not present there.

My Entity In which I'm doing entry :-

Product Type

@Data
@Entity
@Table(name = "tbl_product_type_chemical")
public class ProductType implements IBaseData<Long> {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;
    private String name;
    @Column(columnDefinition = "text")
    private String description;
    }

This is method of Runner :-

private void loadProductTypeNew() throws Exception {
    String SUBMODULE = " [Init Data] " + " [loadProductTypeNew()] ";
    try {
        ProductType fp = productTypeRepository.getOne(1L);
        if (null == fp) {
            fp = new ProductType();
            fp.setId(1L);
            fp.setName("FINISH PRODUCT");
            productTypeRepository.save(fp);
        }
        ProductType rm = productTypeRepository.getOne(2L);
        if (null != rm) {
            rm = new ProductType();
            rm.setId(2L);
            rm.setName("RAW MATERIAL");
            productTypeRepository.save(rm);
        }
        ProductType sm = productTypeRepository.getOne(3L);
        if (null != sm) {
            sm = new ProductType();
            sm.setId(3L);
            sm.setName("SUPPORTING MATERIAL");
            productTypeRepository.save(sm);
        }
    } catch (Exception ex) {
        ApplicationLogger.logger.error(SUBMODULE + ex.getMessage(), ex);
        throw ex;
    }
}


@Override
public void run(String... args) throws Exception {
    loadProductTypeNew();`
}

Output :-

9           SUPPORTING MATERIAL
8           RAW MATERIAL
7           FINISH PRODUCT
10          FINISH PRODUCT
11          RAW MATERIAL
12          SUPPORTING MATERIAL
13          RAW MATERIAL
14          SUPPORTING MATERIAL

And I am calling it in run method.If anyone can solve thanks in advance

Thakkar Darshan
  • 350
  • 1
  • 15
  • Please show us your entity definition. Also for such use case maybe it will be easier to use flyway/liquibase so it enters your data in first db migration and you will not need to care about this manually? – Nadir May 25 '20 at 06:17
  • I have edited the question Please look at that @Nadir – Thakkar Darshan May 25 '20 at 07:39
  • Once a primary key is assigned to a given row it is not assigned to another row even if that row is deleted. Otherwise your DROP the table and create again. – Mukesh Keshu May 25 '20 at 07:51
  • I know @MukeshKeshu But how to overcome to that situation – Thakkar Darshan May 25 '20 at 08:01
  • What i did on my spring boot project was to first delete the tables - Add a data.sql to the class path and used @GeneratedValue(strategy=GenerationType.AUTO) to my ids. So when the application starts the data in the sql file will be loaded to the database. See this - https://stackoverflow.com/questions/38040572/spring-boot-loading-initial-data – Mukesh Keshu May 25 '20 at 08:38

1 Answers1

0

Nothing in your application should depend on specific values of the id field since it is generated by the database.

I suggest therefore to identify a business key (in your case the name), ensure it is unique with a primary key and use that to determine if a a row needs to be inserted in the database.

Your code would look similar to this

if (productTypeRepository.existsByName(""FINISH PRODUCT")) {
    ProductType fp = new ProductType();
    fp.setName("FINISH PRODUCT");
    productTypeRepository.save(fp);
}
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348