春天开机2.杰克逊JSON序列化或Spring数据JPA FetchType.Lazy

问题描述 投票:0回答:1

我的情况是超级怪异,很坦率地说,我并不完全相信如果杰克逊系列化问题或东西与Spring数据JPA做如何获取懒对象

春天2.0.2

  • 弹簧引导起动数据JPA
  • 弹簧引导启动的Web
  • 弹簧引导devtools
  • MySQL的连接器的Java

当您搜索大多数人的问题,杰克逊是它的序列化会话之外的懒惰造成取下面的例外。但是,我没有得到这个例外,在所有....

org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话

问题

  1. 它实际上获取的数据和我的控制器(以下Hibernate查询)返回JSON对象。它是一个问题,以获取急切,因为它得到的是不使用不必要的大的数据。

结构(省略getter和setter,以使线程更短)

       User
        | <---[ OneToMany ]        
     Employee { hierarchies entity }
        |
  +-----+-----+
  |           |
PartTime   FullTime

用户

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

@OneToMany(mappedBy="user")
@JsonIgnoreProperties("user")
private List<Employee> employee = new ArrayList<>();

雇员

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JsonIgnoreProperties("employee")
private User user;

全职/兼职(场工资变化小时工资在兼职时)

@Entity
public class FullTime extends Employee  {

private BigDecimal salary;

UserRepository延伸CrudRepository

UserService

public User getUserById(Long id) {

    Optional<User> optionalUser = userRepo.findById(id);
    User user = optionalUser.get();

    logger.info("\n User -> {}", user);
    return user;
}

调节器

@RestController
@RequestMapping(value="/api/user")
public class UserController {

  @GetMapping(value="/{userId}" )
  public User getUser(@PathVariable("userId") Long id) {
    return userService.getUserById(id);
  }   

}

在我的应用程序扩展CommandLineRunner,让我在应用程序的启动运行命令

@Override
public void run(String... args) throws Exception {
  logger.info("users 1 -> {}" , userService.getUserById(1L));
}


Hibernate: 
select
    user0_.id as id1_3_0_,
    user0_.name as name2_3_0_ 
from
    user user0_ 
where
    user0_.id=?

INFO 14434 --- [  restartedMain] ication$$EnhancerBySpringCGLIB$$6cf0457c :     
users 1 -> 
User [id=1, name=Jack]

但是,当我通过我的控制器http://localhost:8080/api/user/1我得到2个独立的休眠调用都似乎是我的服务层内。请记住,我的服务层,我没有交易,所以它很奇怪....

Hibernate:  << 1st
select
    user0_.id as id1_3_0_,
    user0_.name as name2_3_0_ 
from
    user user0_ 
where
    user0_.id=?

com.example.app.service.UserService      : << -- In service 
User -> 
User [id=1, name=Jack]

Hibernate: 
select
    employee0_.user_id as user_id2_0_0_,
    employee0_.id as id1_0_0_,
    employee0_.id as id1_0_1_,
    employee0_.user_id as user_id2_0_1_,
    employee0_1_.salary as salary1_1_1_,
    employee0_2_.hourly_wage as hourly_w1_2_1_,
    case 
        when employee0_1_.id is not null then 1 
        when employee0_2_.id is not null then 2 
        when employee0_.id is not null then 0 
    end as clazz_1_ 
from
    employee employee0_ 
left outer join
    full_time employee0_1_ 
        on employee0_.id=employee0_1_.id 
left outer join
    part_time employee0_2_ 
        on employee0_.id=employee0_2_.id 
where
    employee0_.user_id=?

现在,如果杰克逊序列化问题,我曾参观过Avoid Jackson serialization on non fetched lazy objects Configure Jackson to omit lazy-loading attributes in Spring Boot ,更多的人,但全部由exdending WebMvcConfigurerAdapter这spring5我认为已经过时呢?

如果它的东西与Spring数据JPA做......那么,请提供一些线索,因为我一直认为你需要@Transaction注释懒惰取与Hibernate的Session关联关系...

很抱歉的长螺纹...

hibernate spring-boot jackson spring-data-jpa lazy-loading
1个回答
1
投票

我觉得你有spring.jpa.open-in-view启用(默认是true)的财产。检查看看,如果你看到你的日志中看到以下信息:

spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

您应该看到所需的异常,如果你禁用它。

© www.soinside.com 2019 - 2024. All rights reserved.