Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring的具有不同视图的JPA EntityGraph_Java_Spring_Jpa_Spring Data - Fatal编程技术网

Java 使用Spring的具有不同视图的JPA EntityGraph

Java 使用Spring的具有不同视图的JPA EntityGraph,java,spring,jpa,spring-data,Java,Spring,Jpa,Spring Data,我有一个Spring应用程序。登录后,我将调用getUserByEmail()方法 我只需要用户和角色数据。根据角色,我将显示不同的视图,每个视图都有不同的数据,并且需要不同的用户子实体 似乎我必须使用不同的子实体调用getUserByEmail() 这是我涉及实体的部分代码: EntityGraph(value = "withAddresses", type = EntityGraphType.FETCH) public class User{ public firstName; pu

我有一个Spring应用程序。登录后,我将调用getUserByEmail()方法

我只需要用户和角色数据。根据角色,我将显示不同的视图,每个视图都有不同的数据,并且需要不同的用户子实体

似乎我必须使用不同的子实体调用getUserByEmail()

这是我涉及实体的部分代码:

EntityGraph(value = "withAddresses", type = EntityGraphType.FETCH)
public class User{
  public firstName;
  public lastName;

  @OneToMany(fetch = FetchType.LAZY)
  public List<Address> addresses

  @OneToMany(fetch = FetchType.LAZY)
  public List<Order> orders;             
 }

 public userRepository extend jPaRepository(User.class,Long){
  @EntityGraph(name="withAdresses")
  getUserByEmail(String email)

  /* if possible */
  @EntityGraph(name="withOrder")
  getUserByEmail(String email)
 }
EntityGraph(value=“withAddresses”,type=EntityGraphType.FETCH) 公共类用户{ 公众名字; 公众姓氏; @OneToMany(fetch=FetchType.LAZY) 公开名单地址 @OneToMany(fetch=FetchType.LAZY) 公开名单命令; } 公共用户存储库扩展jPaRepository(User.class,Long){ @EntityGraph(name=“WithAddresses”) getUserByEmail(字符串电子邮件) /*如果可能的话*/ @EntityGraph(name=“withOrder”) getUserByEmail(字符串电子邮件) }
  • 是否可能有两个具有相同查询名称的用户对象图? 因为不同的视图需要不同的数据

  • 另外,当切换到一个新视图(spring controller中的新调用)时,来自上一个视图的事务将被关闭,我必须进行新调用,以便与用户有不同的数据。我不明白若你们不使用相同的事务服务方法,那个么FetchLazy有什么帮助,除非我并没有遗漏什么


  • 例如,如果我需要“orderWiew.html”中的订单数据,订单的延迟加载将不会有任何帮助,我必须再次完整调用相同的用户数据和其他订单数据

    只是一个关于使用多实体图的建议:在我的工作中,我们使用了事实Spring数据可以使用多个前缀作为查询方法。我们设置了一个约定,即具有不同前缀的方法具有不同的实体图。因此,例如,FindUserByMail(字符串)可以使用比readUserByEmail(字符串)更惰性的图形

    不幸的是,我认为Spring数据不支持以动态方式传递实体图。不过,您可以实现它并将其添加到存储库中。为此,您应该:

    创建声明新方法的接口(但不扩展JpaRepository或其他存储库接口)

    使您的存储库接口扩展该接口

    public interface UserRepository extends JPaRepository<User,Long>, UserCustomOperations{
        // Not much to do here anymore
    }
    

    谢谢@Apokralipsa,我认为您的解决方案非常有效,并将提供为查询提供任何名称的选项。在我的实现中注入EntityManager是我试图阻止的唯一一件事。但这似乎是唯一的选择。我想知道,任何时候对同一个对象调用不同的查询(在这种情况下是用户),用户对象将从DB中新加载,其子或用户将从缓存加载,但未加载的子将从DB加载?如果不想注入实体管理器,则考虑用F取Cuin()创建一个名为JPQL的查询。然后,您可以在存储库中用@Query注释该方法,并让它执行该JPQL
    public interface UserRepository extends JPaRepository<User,Long>, UserCustomOperations{
        // Not much to do here anymore
    }
    
    public class  UserRepositoryImpl implements UserCustomOperations{
        public User findUserByEmail(String email, String entityGraph){
            // Inject the EntityManager and execute standard Jpa query with the entity graph set
        }
    }