as3 URLLoader缓存移动应用的建议(as3 URLLoader Cache suggestions for mobile app)
我一直在寻找一个类来管理URLLoader调用中的数据缓存但是不成功。 URLLoader是否默认缓存?
我正在构建一个应用程序来获取用户的大量信息(个人资料详细信息,朋友列表,个人资料图像等),我不希望每次都调用URLLoader。 我在第一次加载时缓存他们的配置文件图像,并希望我可以对其余数据执行相同的操作,而无需在本地创建数据库的克隆。
干杯
I have been looking around for a class to manage the caching of the data from a URLLoader call but been unsuccessful. Does URLLoader cache by default?
I am building an app that fetches a bunch of information on the user (profile details, friend lists, profile image etc) and I would prefer not to call URLLoader each time. I am caching their profile image on first load and hope I can do the same with the rest of the data without having to create a clone of the DB locally.
Cheers
原文:https://stackoverflow.com/questions/17267015
最满意答案
使用
orphanRemoval
:@OneToMany(mappedBy="classroom", cascade={CascadeType.ALL}, orphanRemoval=true)
每当从持久集中删除条目时,它将被删除。 这意味着您需要使用持久集 。 即你不能更换套装,而应该做:
classroom.getRoster().clear(); classroom.getRoster().addAll(newRoster);
示例如何将持久集与用户所需的集同步:
/** * Assemble ClassroomUser relations. * @param classroom Classroom entity. Must be attached persistent or transient. Never null. * @param userIds Collection of user identifiers. Can be empty. Never null. */ private void assembleClassroomUsers(Classroom classroom, Collection<Integer> userIds) { // Make sure relation set exists (might be null for transient instance) if (classroom.getUsers() == null) { classroom.setUsers(new HashSet<ClassroomUser>()); } // Create working copy of the collection Collection<Integer> ids = new HashSet<Integer>(userIds); // Check existing relations and retain or remove them as required Iterator<ClassroomUser> it = classroom.getUsers().iterator(); while (it.hasNext()) { Integer userId = it.next().getUser().getId(); if (!ids.remove(userId)) { it.remove(); // This will be picked by the deleteOrphans=true } } // Create new relations from the remaining set of identifiers for (Integer userId : ids) { ClassroomUser classroomUser = new ClassroomUser(); classroomUser.setClassroom(classroom); // User must not have ClassroomUser relations initialized, otherwise Hibernate // will get conflicting instructions what to persist and what to drop => error. // It might be safer to use dummy transient instance... User dummyUser = new User(); dummyUser.setId(userId); classroomUser.setUser(dummyUser); classroom.getUsers().add(classroomUser); } }
这种方法看起来有点复杂。 您可以使用自定义
equals
/hashCode
和一些Set<E>
操作方法(例如来自Guava)创建更简单(但可能不太多)的东西。Use
orphanRemoval
:@OneToMany(mappedBy="classroom", cascade={CascadeType.ALL}, orphanRemoval=true)
Whenever an entry is removed from the persistent set, it will get deleted. And this means you need to work with the persistent set. I.e. you are not allowed to replace the set, instead you should do:
classroom.getRoster().clear(); classroom.getRoster().addAll(newRoster);
EXAMPLE how to synchronize persistent set with a user required set:
/** * Assemble ClassroomUser relations. * @param classroom Classroom entity. Must be attached persistent or transient. Never null. * @param userIds Collection of user identifiers. Can be empty. Never null. */ private void assembleClassroomUsers(Classroom classroom, Collection<Integer> userIds) { // Make sure relation set exists (might be null for transient instance) if (classroom.getUsers() == null) { classroom.setUsers(new HashSet<ClassroomUser>()); } // Create working copy of the collection Collection<Integer> ids = new HashSet<Integer>(userIds); // Check existing relations and retain or remove them as required Iterator<ClassroomUser> it = classroom.getUsers().iterator(); while (it.hasNext()) { Integer userId = it.next().getUser().getId(); if (!ids.remove(userId)) { it.remove(); // This will be picked by the deleteOrphans=true } } // Create new relations from the remaining set of identifiers for (Integer userId : ids) { ClassroomUser classroomUser = new ClassroomUser(); classroomUser.setClassroom(classroom); // User must not have ClassroomUser relations initialized, otherwise Hibernate // will get conflicting instructions what to persist and what to drop => error. // It might be safer to use dummy transient instance... User dummyUser = new User(); dummyUser.setId(userId); classroomUser.setUser(dummyUser); classroom.getUsers().add(classroomUser); } }
This approach might seem a little bit complex. You might be able to create something simpler (but probably not too much) with custom
equals
/hashCode
and someSet<E>
manipulation methods (e.g. from Guava).
相关问答
更多-
JPA 2 /休眠孤儿删除仍然不能与@OneToMany一起工作?(JPA 2 / Hibernate orphan removal still not working with @OneToMany?)[2022-06-30]
老实说,我不知道为什么,但如果您将CascadeType.PERSIST (或更好的CascadeType.ALL )添加到Provider实体中的@OneToMany关系,它将按预期工作。 可能Hibernate文档缺乏这个小细节。 用JPA2 更新 EclipseLink 2.5.1似乎没有这个问题 第二次更新 在第2.9节“实体关系”中,JPA 2.1规范说:“如果孤立的实体是分离的,新的或移除的实体,则orphanRemoval的语义不适用。” 我不知道你的相关实体是否被分离,但如果是的话,这不是一 ... -
JPA - @OneToMany更新(JPA - @OneToMany update)[2022-05-13]
明确的答案是:不,您的JPA提供商不可能自动处理您描述它的方式的双向关系。 但是,您可以实现在您的实体中设置双向关联的逻辑,或许可以沿着这些方向进行: class Department { public void addEmployee(Employee empl) { if (empl.getDepartment() != null && !this.equals(empl.getDepartment())) { empl.getDepartment().getEmployees ... -
如果您使用Hibernate,则必须明确定义CascadeType.DELETE_ORPHAN注释,可以与JPA CascadeType.ALL结合使用。 如果您不打算使用Hibernate,则必须明确地先删除子元素,然后删除主记录以避免任何孤立记录。 执行顺序 取出要删除的主行 获取子元素 删除所有子元素 删除主行 关闭会议 使用JPA 2.0,现在可以使用选项orphanRemoval = true @OneToMany(mappedBy="foo", orphanRemoval=true) If y ...
-
JPA的行为是正确的(意思是根据规范 ):对象不会被删除,因为您已经从OneToMany集合中删除它们。 有供应商特定的扩展,但本机JPA不适应它。 部分原因是因为JPA实际上并不知道是否应该删除从集合中删除的内容。 在对象建模术语中,这是组合与“聚合”之间的区别。 在组合中 ,没有父项的子实体没有存在。 房间和房间之间的一个典型的例子。 删除房子和房间也去。 汇总是一种较松散的协会,以课程和学生为代表。 删除课程,学生仍然存在(可能在其他课程)。 因此,您需要使用特定于供应商的扩展来强制此行为(如果可用) ...
-
JPA + Hibernate - 从@OneToMany关系重新连接实体(JPA+Hibernate - Reconnecting entities from a @OneToMany relationship)[2022-09-05]
你有没有尝试过: team2.setPlayers(team1.getPlayers(); team1.setPlayers(null); for (Player player : team2.getPlayers() { player.setTeam(team2); } Have you tried: team2.setPlayers(team1.getPlayers(); team1.setPlayers(null); for (Player player : team2.getPlayers( ... -
这非常简单: customer.addOrder(order); 这就是你所需要的一切。 这是ORM的原则。 您操纵对象,ORM使用您定义的映射将它们保存在数据库中。 It's extremely simple: customer.addOrder(order); That's all you need. That's the principle of an ORM. You manipulate objects, and the ORM saves them in the database using ...
-
是的,根据@jmvivo的回答你需要使用orphanRemoval = true是你的用例的解决方案,这里按照Oracle在这个链接 当从关系中移除一对一或一对多关系中的目标实体时,通常希望将移除操作级联到目标实体。 此类目标实体被视为“孤儿”,orphanRemoval属性可用于指定应删除孤立实体。 例如,如果订单包含多个订单项,并且其中一个订单项已从订单中删除,则已删除的订单项会被视为孤儿。 如果将orphanRemoval设置为true,则在从订单中删除订单项时,系统商品实体将被删除。 您可能还希望在 ...
-
您需要另一个OneToMany关系:确保db表中的两列代表Feedback : author和receivee :它们将指向employee表的主键,因此它们必须具有相同的类型。 添加外键约束:它们都应指向employee表的主键。 然后,按照zbigniew的回答中的解释,用Java映射关系: 反馈类: @ManyToOne @JoinColumn(name="author") private Employee author; @ManyToOne @JoinColumn(name="receivee" ...
-
JPA没有这样做,据我所知,没有JPA实现可以做到这一点。 JPA要求您管理关系的两个方面。 当只更新关系的一侧时,这有时被称为“对象损坏” JPA确实在双向关系中定义了一个“拥有”的一方(对于OneToMany,这是没有mappedBy注释的一方),它用于在持久化到数据库时解决冲突(只有一个表示数据库中的关系与内存中的两个相比,因此必须做出解决方案)。 这就是为什么要实现对ProductOrder类的更改,而不是对Client类的更改。 即使拥有“拥有”关系,您也应该始终更新双方。 这通常会导致人们只依赖 ...
-
使用orphanRemoval : @OneToMany(mappedBy="classroom", cascade={CascadeType.ALL}, orphanRemoval=true) 每当从持久集中删除条目时,它将被删除。 这意味着您需要使用持久集 。 即你不能更换套装,而应该做: classroom.getRoster().clear(); classroom.getRoster().addAll(newRoster); 示例如何将持久集与用户所需的集同步: /** * Assemble ...