简体   繁体   English

JPA IdClass混淆-这个想法可行吗?

[英]JPA IdClass confusion - is this idea practical?

If you have a user entity, you could store his/her address in two ways. 如果您有用户实体,则可以通过两种方式存储其地址。 You could store all of the fields in the user object directly -or- you could create an address object which is persisted in a separate table and joined to the user (or cascaded, or whatever) and put into an address object inside the user entity. 您可以将所有字段直接存储在用户对象中-或-您可以创建一个地址对象,该地址对象保存在单独的表中,并与用户连接(或级联,等等),然后放入用户实体内的地址对象中。

What is the advantage of doing the latter? 后者的优势是什么? If you can fit more stuff into a single table, doesn't that make the most sense? 如果您可以在一个表中放入更多内容,那不是最有意义吗? I would presume the latter would end up incurring more resource usage. 我认为后者最终将导致更多的资源使用。 I get that it can be more practical for other things, but the example specifically mentions a situation where you could do either. 我知道这对于其他事情可能更实用,但是该示例专门提到了您可以执行任何一种操作的情况。

Is a user something you would generally use an IdClass for, if you did something similar to what I've described above? 如果您执行了与上述操作类似的操作,那么用户通常会使用IdClass吗?

For an IdClass, must EVERY field simply be a key to another entity, or can you have concrete or transient fields there in addition? 对于IdClass,是否每个字段都必须仅仅是另一个实体的键,还是您还可以在其中添加具体字段或临时字段? Is usage of @IdClass simply saying to expect multiple Id annotations, and the rest is up to you? @IdClass的用法是否只是在说要使用多个Id注释,其余的取决于您?

Thanks! 谢谢!

You User to address relationship should be many to one. 您与用户之间的地址关系应该是多对一的。 Because one user may have two addresses. 因为一个用户可能有两个地址。 If you going to put address object inside the user object you can have only one address for a user. 如果要将地址对象放在用户对象内,则每个用户只能有一个地址。 Having the address fields in user object is also having that issue. 在用户对象中具有地址字段也存在该问题。 So both your suggestions are not the best practices. 因此,您的建议都不是最佳做法。

No need to use a IdClass for User entity you can simply use database native id genoration by annotations. 无需为用户实体使用IdClass,您只需使用注释即可使用数据库本机ID生成。 `@Id @GeneratedValue(strategy=GenerationType.IDENTITY). @Id @GeneratedValue(strategy = GenerationType.IDENTITY)。

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

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