【发布时间】: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);
}
【问题讨论】: