简体   繁体   中英

How to change the name and value of default ID field of an entity in jhipster

During Entity creation in jhipster, ID field gets created automatically, with auto-increment feature.

As per my requirement, i want the ID field name as TK_ID, instead of ID. And also, value of the ID field starts with 1 and gets incremented automatically. In my case , i want the value to be started with TK001, and should be auto incremented.

During Entity creation , i want these changes to happen. Is there any way to do this is jhipster application ?

Requirement: Instead of field name- ID, want as TK_ID..... And Value of ID field should start with TK001, instead of 1.

Note: I am using mysql for DB and Gateway application

You're entering the world of Hibernate/JPA generators. If you look at the Jhipster entity generator you'll see that the ID is a @GeneratedValue that uses the Identity generation type, not the Increment type that you say.

There are many different types of ID generators that are explained over on this SO answer .

I would avoid changing the ID attribute itself (the one that is automatically generated by JHipster) to a different name or generation type as it could have a few unintended consequences, and there could be other JHipster dependencies. Simply too messy to edit and manage.

A better way is to create another attribute with the name TK_ID and create your own @GenericGenerator that takes the ID attribute and adds the prefix you want. eg if a new record will have the ID field value of 3 , take it and add the prefix TK00 .

Have a look at this SO answer for a code example.

Alternatively, you could transform any of your queries to TK_ID and point it to ID instead, and transform the result back to the format you want. That way you're not adding an extra attribute and changing the way the entity is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM