JPA在加载对象列表时关闭会话

时间:2017-02-27 22:21:52

标签: hibernate session jpa

我经历了我找到的所有解决方案,并没有通过他们解决我的问题。问题是我使用jpa方法,例如findAllByClientPesel(),它返回整个Client对象。 Client对象引用了对象diffrent类(作为对象列表)。我想通过fetch = LAZY加载数据,所以EAGER不满意我。

我知道虽然加载不同对象的列表会削减会话,但我想如果它类似于fetch = LAZY,那么它必须是一个满足我想要的解决方案。

我在spring应用程序中打开了延迟加载: 的 spring.jpa.hibernate.enable_lazy_load_no_trans =真

堆栈跟踪看起来像:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.carwash.domains.Client.clientReservations, could not initialize proxy - no Session

at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:509)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.carwash.domains.Client.toString(Client.java:136)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at com.carwash.MapperTest.ClientMapperTest.test(ClientMapperTest.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

类客户端看起来像:

@Entity
public class Client {
private String clientPesel;
private String name;
private String surname;
private String email;
private String phone;
private String accountNumber;
private User clientUser;
// private Role CLIENT_ROLE;
private List<Reservation> clientReservations;
private List<Review> clientReviews;
private Adress clientAdress;
private List<Vehicle> clientVehicles;


@Id
@Length(min = 11, max = 11)
public String getClientPesel() {
    return clientPesel;
}

public void setClientPesel(String clientPesel) {
    this.clientPesel = clientPesel;
}

@Column(nullable = false)
@Length(max = 16)
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Column(nullable = false)
@Length(max = 16)
public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

@Column(nullable = false)
@Length(max = 40)
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Length(min = 9, max = 9)
@Column(nullable = false)
public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

@Length(min = 26, max = 26)
@Column(nullable = false)
public String getAccountNumber() {
    return accountNumber;
}

public void setAccountNumber(String accountNumber) {
    this.accountNumber = accountNumber;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "userId")
public User getClientUser() {
    return clientUser;
}

public void setClientUser(User clientUser) {
    this.clientUser = clientUser;
}

@OneToMany(mappedBy = "reservationClient", fetch = FetchType.LAZY)
public List<Reservation> getClientReservations() {
    return clientReservations;
}

public void setClientReservations(List<Reservation> clientReservations) {
    this.clientReservations = clientReservations;
}

@OneToMany(mappedBy = "reviewClient", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
public List<Review> getClientReviews() {
    return clientReviews;
}

public void setClientReviews(List<Review> clientReviews) {
    this.clientReviews = clientReviews;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "adressClient")
public Adress getClientAdress() {
    return clientAdress;
}

public void setClientAdress(Adress clientAdress) {
    this.clientAdress = clientAdress;
}

@OneToMany(mappedBy = "vehicleClient", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
public List<Vehicle> getClientVehicles() {
    return clientVehicles;
}

public void setClientVehicles(List<Vehicle> clientVehicles) {
    this.clientVehicles = clientVehicles;
}

@Override
public String toString() {
    return "Client{" +
            "clientPesel='" + clientPesel + '\'' +
            ", name='" + name + '\'' +
            ", surname='" + surname + '\'' +
            ", email='" + email + '\'' +
            ", phone='" + phone + '\'' +
            ", accountNumber='" + accountNumber + '\'' +
            ", clientUser=" + clientUser +
            ", clientReservations=" + clientReservations +
            ", clientReviews=" + clientReviews +
            ", clientAdress=" + clientAdress +
            ", clientVehicles=" + clientVehicles +
            '}';
}

public Client(String clientPesel) {
    this.clientPesel = clientPesel;
}

public Client() {
}
}

我如何测试它:

    @Test

public void test(){
    Client allByClient = test.findAllByClientPesel("72041317810");
    System.out.println(allByClient);

}

1 个答案:

答案 0 :(得分:1)

尝试在您的属性文件中将会话上下文设置为thread

spring.jpa.properties.hibernate.current_session_context_class=thread

我也相信延迟加载的正确属性是

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true