[英]When should I use enum instead of domain and vice versa?
My context: User will add one of numerous type of company into himself in the form. 我的上下文:用户将以表格的形式将众多类型的公司之一添加到他自己中。 For that form, I created enum
to fill the listbox instead of pulling the database. 对于该表单,我创建了enum
来填充列表框,而不是提取数据库。
I will try my best to explain what my question is: In my context, whats the difference between using enum
and domain
for showing and save into database? 我将尽力解释我的问题:在我的上下文中,使用enum
和domain
显示并保存到数据库之间有什么区别? Performance, logic, bestpractice? 性能,逻辑,最佳实践?
Because when user saves a companyType
, it will save an int
into database, but I can see what description
it is. 因为当用户保存一个companyType
,它会将一个int
保存到数据库中,但是我可以看到它的description
。 using a method @Overide toString()
. 使用方法@Overide toString()
。
Instead of using enum
as code below, I could have created a domain with id
and description
moreover adding two registers into my db pulling into to form. 除了使用下面的enum
作为代码外,我还可以创建一个具有id
和description
的域,并且在我的数据库中添加两个寄存器以形成表单。
class Company{
CompanyType companyType
}
Use this as enum 将此用作枚举
enum CompanyType {
MARKET('Market', 1),
SHOP('Shop', 2)
// ... etc of code
}
or use this as domain 或将此用作域
class CompanyType {
Long id
String description
}
sorry for my bad english. 对不起,我的英语不好。
您应该考虑的主要因素是公司类型数据可能发生的变化,如果您能够列出将要使用的所有可能的公司类型,并且确定不会频繁更新,那么枚举是合理的选择,没有另一方面,如果需要更改表或POJO,则要更改,添加,更新和删除公司类型,则表和POJO类是正确的选择。
two points: 两点:
performance: enum values are saved as a String (usually as enum.toString()
) in a db, and don't require any FK-relations and have less overhead during ORM transformations 性能:枚举值在数据库中保存为字符串(通常保存为enum.toString()
),并且不需要任何FK关系,并且在ORM转换期间开销较小
flexibility: enum values can be updated only with new deployment. 灵活性:枚举值只能使用新的部署进行更新。 If you need to change the value of the existing enum constant, you would have to migrate your data 如果需要更改现有枚举常量的值,则必须迁移数据
I would use enums instead of a domain class, as the performance is usually more important 我会用枚举代替域类,因为性能通常更重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.