简体   繁体   English

如何更改Jhipster中实体的默认ID字段的名称和值

[英]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. 在jhipster中创建实体期间,将使用自动递增功能自动创建ID字段。

As per my requirement, i want the ID field name as TK_ID, instead of ID. 根据我的要求,我希望ID字段名称为TK_ID,而不是ID。 And also, value of the ID field starts with 1 and gets incremented automatically. 而且,ID字段的值以1开头,并自动递增。 In my case , i want the value to be started with TK001, and should be auto incremented. 在我的情况下,我希望该值以TK001开头,并且应自动递增。

During Entity creation , i want these changes to happen. 在实体创建期间,我希望这些更改能够发生。 Is there any way to do this is jhipster application ? jhipster应用程序有什么办法做到这一点?

Requirement: Instead of field name- ID, want as TK_ID..... And Value of ID field should start with TK001, instead of 1. 要求:而不是字段名称-ID,要作为TK_ID .....并且ID字段的值应以TK001开头,而不是1。

Note: I am using mysql for DB and Gateway application 注意:我正在使用mysql作为数据库和网关应用程序

You're entering the world of Hibernate/JPA generators. 您正在进入Hibernate / JPA生成器的世界。 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. 如果查看Jhipster实体生成器,您会发现ID是使用Identity生成类型而不是您说的Increment类型的@GeneratedValue

There are many different types of ID generators that are explained over on this SO answer . 有许多不同类型的ID生成器在此SO答案中进行了说明。

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. 我会避免将ID属性本身(由JHipster自动生成的属性)更改为其他名称或生成类型,因为它可能会带来一些意想不到的后果,并且可能还会存在其他JHipster依赖项。 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. 更好的方法是创建另一个名称为TK_ID属性,并创建自己的@GenericGenerator ,该属性@GenericGenerator ID属性并添加所需的前缀。 eg if a new record will have the ID field value of 3 , take it and add the prefix TK00 . 例如,如果一条新记录的ID字段值为3 ,则取其并添加前缀TK00

Have a look at this SO answer for a code example. 看看这个SO答案的代码示例。

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. 或者,您可以将任何查询转换为TK_ID ,然后将其指向ID ,然后将结果转换回所需的格式。 That way you're not adding an extra attribute and changing the way the entity is created. 这样,您无需添加额外的属性并更改实体的创建方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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