简体   繁体   English

我们如何在 JPA/Hibernate 中使用 Integer 或 Long 类型的对象作为外键映射?

[英]How do we use Integer or Long type of objects as Foreign key mapping in JPA/Hibernate?

I tried below sample code which does not seem to work, while trying to fetch the object from table, hibernate tries to set the User Object on top of Long objects and fails when tried to load the Parent entity which has this createdBy field我尝试了以下似乎不起作用的示例代码,同时尝试从表中获取 object,hibernate 尝试将用户 Object 设置在 Long 对象之上,并在尝试加载具有此 createdBy 字段的父实体时失败

any help?有帮助吗?

@JoinColumn(name = "CREATED_BY", referencedColumnName = "USER_ID", updatable = false, nullable = false)
@OneToOne(targetEntity = User.class)
private Long createdBy;

I want to use Long/Integer as type of the field and want to make sure its a valid Foreign key to User table which has Primary key USER_ID我想使用 Long/Integer 作为字段类型,并希望确保它是具有主键 USER_ID 的用户表的有效外键

Please note that i DO NOT want to use User as type of my object, for example, i do not want below declaration in my class请注意,我不想使用用户作为我的 object 的类型,例如,我不想在我的 class 中使用以下声明

private User createdBy;私人用户创建者;

Edit: Reason for such requirement:编辑:这种要求的原因:

Ahh well,.. i'll try to keep it short which is basically one of the problem with ORM's like Hibernate. I have a Super Class AuditLogModel which is extended by each and every entity class in my software (100+ entities), This AuditLogModel class has CreatedBy & ModifiedBy field, If i keep the types of this fields as User, then every entity in my software tries to create a join with user table twice on operations like getResultsList/Merge/refresh.嗯,好吧,..我会尽量保持简短,这基本上是 ORM 的问题之一,例如 Hibernate。我有一个超级 Class AuditLogModel,它由我软件中的每个实体 class(100 多个实体)扩展,这AuditLogModel class 具有 CreatedBy 和 ModifiedBy 字段,如果我将此字段的类型保留为用户,那么我软件中的每个实体都会尝试在 getResultsList/Merge/refresh 等操作中两次创建与用户表的连接。 where Merge and Refresh calls on entity manger cannot be controlled by us, its all eager loading in one select query.我们无法控制对实体管理器的 Merge 和 Refresh 调用,它在一个 select 查询中全部预加载。 Since my entities have child entities and they have further childs and so on, this creates more than 61 joins and sometime 100+ joins and causes query performance issues.由于我的实体有子实体并且它们有更多的子实体等等,这会创建超过 61 个连接,有时会创建 100 多个连接并导致查询性能问题。 These createdBy and modified by columns are updated with every insert/update but not required with any query when any entity is loaded, and hence I want to avoid the unneccessari joins here.这些 createdBy 和 modified by 列在每次插入/更新时都会更新,但在加载任何实体时不需要任何查询,因此我想在这里避免不必要的连接。 Hope its understood希望它被理解

Ah OneToOne mapping is used for Entities like this:啊 OneToOne 映射用于这样的实体:

@JoinColumn(name = "CREATED_BY", referencedColumnName = "USER_ID", updatable = false, nullable = false)
@OneToOne
private User createdBy;

If you want to use just the database value then you must remove the OneToOne mapping:如果只想使用数据库值,则必须删除 OneToOne 映射:

@Column(updatable = false, nullable = false)
private Long createdBy;

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

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