【问题标题】:JPA close the session while loading list of objectsJPA 在加载对象列表时关闭会话
【发布时间】:2017-07-18 17:19:28
【问题描述】:

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

我知道在加载不同对象列表时会中断会话,但我想如果它是 fetch = LAZY 之类的东西,那么它一定是一个可以满足我要求的解决方案。

我已经在 Spring 应用程序中打开了延迟加载: spring.jpa.hibernate.enable_lazy_load_no_trans=true

堆栈跟踪看起来像:

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)

Client 类看起来像:

@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);

}

【问题讨论】:

    标签: hibernate session jpa


    【解决方案1】:

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

    spring.jpa.properties.hibernate.current_session_context_class=thread
    

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

    spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
    

    【讨论】:

    • 不客气!请考虑接受这个答案,以及您在 SO 上收到的所有答案(如果它们有帮助的话)。到目前为止,你从来没有这样做过。见stackoverflow.com/help/someone-answers@bielas
    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2017-10-13
    • 2021-06-26
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多