Imagine, an Event entity references a Status Entity:
想象一下,一个事件实体引用一个状态实体:
@Entity
@Table(name = "event")
public class Event()
{
@Id
@Column(name = "id", nullable = false)
private long id;
...
@ManyToOne
@JoinColumn(name = "status_code", nullable = false)
private Status status;
}
@Entity
@Table(name = "status")
public class Status()
{
@Id
@Column(name = "code", nullable = false)
private String code;
@Column(name = "label", nullable = false, updatable = false)
private String label;
}
Status is mapped to a small table 'status
'. Status is a typical reference data / lookup Entity.
状态映射到一个小表“Status”。状态是一个典型的引用数据/查找实体。
code label
----- --------------
CRD Created
ITD Initiated
PSD Paused
CCD Cancelled
ABD Aborted
I'm not sure if it is a good idea to model Status as an Entity. It feels more like an enumeration of constants...
我不确定将状态建模为实体是否是一个好主意。它更像是对常量的枚举……
By mapping Status as an Entity, I can use Status objects in Java code, and the Status values are equally present in the database. This is good for reporting.
通过将状态映射为实体,我可以在Java代码中使用状态对象,并且状态值在数据库中同样存在。这有利于报道。
On the other hand, if I want to set a particular Status to an Event, I can't simply assign the constant status I have in mind. I have to lookup the right entity first:
另一方面,如果我想为一个事件设置一个特定的状态,我不能简单地分配我心中的常量状态。我必须先查找正确的实体:
event.setStatus(entityManager.find(Status.class, "CRD"))
Can I avoid the above code fragment? I'm affraid for a performance penalty and it looks very heavy...
我可以避免上面的代码片段吗?我很害怕被罚点球,看起来很重……
- Do I have to tweak things with read-only attributes?
- 我需要对具有只读属性的东西进行调整吗?
- Can I prefetch these lookup entities and use them as constants?
- 我可以预取这些查找实体并将它们作为常量使用吗?
- Did I miss a crucial JPA feature?
- 我是否错过了一个关键的JPA特性?
- ...?
- ……?
All opinions / suggestions / recommendations are welcome!
欢迎提出意见/建议!
Thank you! J.
谢谢你!J。
2 个解决方案
#1
3
Can I avoid the above code fragment? I'm affraid for a performance penalty and it looks very heavy?
我可以避免上面的代码片段吗?我被判罚款,而且看起来很重?
Well, you could use an enum
instead. I don't really see why you don't actually.
你可以用enum代替。我不明白你为什么不。
But if you really want to use an entity, then it would be a perfect candidate for 2nd level caching and this would solve your performance concern.
但是如果您真的想要使用实体,那么它将是二级缓存的理想候选,这将解决您的性能问题。
#2
9
You could use entityManager.getReference(Status.class, "CRD")
, which might not fetch the entity from the database if it is only used to set a foreign key.
您可以使用entityManager.getReference(状态。类“CRD”),如果实体只用于设置外键,则它可能不会从数据库中获取实体。
#1
3
Can I avoid the above code fragment? I'm affraid for a performance penalty and it looks very heavy?
我可以避免上面的代码片段吗?我被判罚款,而且看起来很重?
Well, you could use an enum
instead. I don't really see why you don't actually.
你可以用enum代替。我不明白你为什么不。
But if you really want to use an entity, then it would be a perfect candidate for 2nd level caching and this would solve your performance concern.
但是如果您真的想要使用实体,那么它将是二级缓存的理想候选,这将解决您的性能问题。
#2
9
You could use entityManager.getReference(Status.class, "CRD")
, which might not fetch the entity from the database if it is only used to set a foreign key.
您可以使用entityManager.getReference(状态。类“CRD”),如果实体只用于设置外键,则它可能不会从数据库中获取实体。