[英]What can I use instead of an entity bean?
I would like to write a Java EE app for online learning. 我想编写一个用于在线学习的Java EE应用程序。 I'm thinking of this data representation: 我正在考虑这种数据表示形式:
@Entity
public class Course {
private String name;
private String[] instructors;
private String[] teachingAssistants;
private GradeBook gradeBook;
// plenty of methods
}
@Entity
public class GradeBook {
private GradeBookColumn[];
// some methods
}
@Entity
public abstract class GradeBookColumn {
private String name;
private GradeBookColumnType type;
// more methods
}
I would have quite a lot more than just that, but you get the point. 我不仅仅可以做到这一点,但是您明白了。
Now, in the EJB 3.2 spec, entity beans were removed and replaced with JPA entities. 现在,在EJB 3.2规范中,实体Bean被删除并替换为JPA实体。 My question is how to cope with this tragic loss. 我的问题是如何应对这一悲惨的损失。 There are three reasons why serialized JPA entities won't work for me: 序列化JPA实体对我不起作用的原因有三个:
So, how do I design this system to meet these requirements without entity beans? 那么,如何在没有实体bean的情况下设计该系统来满足这些要求?
I'm not sure that the loss of entity beans is really tragic, but that's a matter of opinion :) 我不确定实体bean的损失是否真的很悲惨,但这是一个见解:)
You seem that have a rich client on the desktop that connects to a remote server. 您似乎在桌面上拥有一个连接到远程服务器的富客户端。 You have two options: 您有两种选择:
A. You exchange " detached " object graphs between the client and server. 答:您在客户端和服务器之间交换“ 分离的 ”对象图。 The client receives some data, modifies it, then sends it back. 客户端接收一些数据,对其进行修改,然后将其发送回去。 The server then " merges " the data it receives. 然后,服务器“ 合并 ”它接收到的数据。 There is one transaction on the server when you load the data, and one when you merge back. 加载数据时,服务器上有一个事务,合并后又有一个事务。 To ensure you don't have conflict, you can version the data. 为了确保没有冲突,您可以对数据进行版本控制。
B. You use an " extended persistence context ". B.您使用“ 扩展的持久性上下文 ”。 In that case, the client receives entites that are still "attached" to a session. 在这种情况下,客户端会收到仍“附加”到会话的实体。 Modification to the entities on the client side are cached, and will be synchronized when you call a method on the server. 客户端上对实体的修改将被缓存,并且在您在服务器上调用方法时将被同步。
So, regarding the three design issue you face, here is my take on it: 因此,关于您面临的三个设计问题,这是我的看法:
Performance. 性能。 JPA and other modern ORM rely on laziness to avoid unnecessary data transfer: data is loaded on demand. JPA和其他现代ORM依靠惰性来避免不必要的数据传输:按需加载数据。 You can choose which part of the graph can be loaded eagerly or lazily. 您可以选择可以急于或懒惰地加载图形的哪一部分。 With option A, you need to make sure that you load all the necessary data before you send them to the client; 使用选项A,您需要确保在将所有必需的数据发送到客户端之前先加载它们。 if the client attempts to access data that aren't loaded, it gets an exception since it's outside of a transaction. 如果客户端尝试访问未加载的数据,则它将获得异常,因为它不在事务之内。 With option B, I guess the client can lazy load data anytime (it would be worth double checking that). 使用选项B,我猜想客户端可以随时延迟加载数据(值得仔细检查)。
Security. 安全。 The JPA entities should be business objects, not data object. JPA实体应该是业务对象,而不是数据对象。 They can encapsulate business methods that do the necessary checks and preserve the desired invariants. 他们可以封装进行必要检查并保留所需不变式的业务方法。 In other words: security is not handled at the data level but at the business logic level. 换句话说:安全性不是在数据级别处理,而是在业务逻辑级别处理。 This applies for both options A and B. 这适用于选项A和B。
Write Access. 写访问。 With option A, you need to send back the whole graph and merge it. 使用选项A,您需要发送回整个图形并将其合并。 With option B, the framework should merge the changes that have been cached in a more optimized way. 使用选项B,框架应以更优化的方式合并已缓存的更改。
Conclusions: 结论:
Extended persistence contexts have been designed for GUI applications with long units of work. 扩展的持久性上下文已为工作量较长的GUI应用程序设计。 They should in theory solve your problems. 从理论上讲,他们应该解决您的问题。 In practice, extended persistence contexts have their share of complexitiy though (eg needs to use stateful session beans). 实际上,扩展的持久性上下文有其复杂性(例如,需要使用有状态会话Bean)。
The approach to detach and merge the graph is simpler, but raises the issues that you mention in terms of performance. 分离和合并图形的方法比较简单,但是会引起您提到的性能问题。
The third options is to go back to traditional data transfer object (DTO) to optimize performance. 第三种选择是回到传统的数据传输对象(DTO)以优化性能。 In that case the JPA entites stay exclusively on the server side. 在这种情况下,JPA实体仅停留在服务器端。 Instead of transferring JPA entites, you transfer only the subset of the data really needed into DTOs. 无需传输JPA实体,而仅将实际需要的数据子集传输到DTO中。 The drawback is that DTOs will proliferate, and you will have boilerplate code to create DTOs from JPA entites, and update the JPA enties from DTOs. 缺点是DTO会大量增加,您将具有从JPA实体创建DTO并更新DTO的JPA实体的样板代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.