简体   繁体   English

我可以使用什么代替实体bean?

[英]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实体对我不起作用的原因有三个:

  1. Performance. 性能。 I will need to push the whole entity and all of its data through the net. 我将需要通过网络推送整个实体及其所有数据。 There is quite a lot of that data, and it could take an unacceptably long time to get it all through. 这些数据很多,而且要花费很长时间才能使它们全部通过。
  2. Security. 安全。 If all the data in the entity is transferred over the net, then all of the data is accessible to the program that downloaded it. 如果实体中的所有数据都是通过网络传输的,则下载该数据的程序可以访问所有数据。 However, I want certain data to only be accessible if the user has sufficient permissions. 但是,我希望某些数据仅在用户具有足够权限的情况下才可访问。
  3. Write Access. 写访问。 Once the copy of the data has been downloaded, the client should be able to make changes to it. 一旦下载了数据副本,客户端就应该能够对其进行更改。 However, if the changes are made, they won't be persisted to the server. 但是,如果进行了更改,它们将不会持久保存到服务器。 Of course, I could always send the entity back to the server for persisting, but I would have to send all the data through an even slower upstream connection. 当然,我总是可以将实体发送回服务器进行持久化,但是我必须通过一个甚至更慢的上游连接发送所有数据。

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: 因此,关于您面临的三个设计问题,这是我的看法:

  1. 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,我猜想客户端可以随时延迟加载数据(值得仔细检查)。

  2. 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。

  3. 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.

相关问题 我可以在bean验证中使用休眠实体注释吗 - Can I use hibernate entity annotations with bean validation 我可以使用会话范围的bean代替httpsession来维护会话吗 - Can i use session scoped bean instead of httpsession for maintaining sessions 我可以使用什么来代替 Java 中的 Set()? - What can I use instead of Set() in Java? 我可以用什么代替滑动抽屉 - What can i use instead of a Sliding drawer 我可以使用什么而不是“this”作为传递参数? - What can I use instead of “this” as passing argument? 我可以在java中使用addActionListener(this)代替“ this”吗? - What can I use in java for addActionListener(this) instead of 'this'? 除了HashMap,我还能使用什么? - What else can I use instead of a HashMap? 当实体中存在关系时,我可以只使用实体的 id 而不是从数据库中获取实体吗? - Can I use only Entity's id instead of fetching an entity from DB when there is a relationship in an Entity? 如果我们可以使用@component,bean 工厂有什么用? - What are bean factories for, if we can use @component? 什么是最佳实践:使用原型bean而不是new()运算符 - What is the best practice: Use prototype bean instead of new () operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM