2017-02-27 56 views
0

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

我知道,虽然不同势对象的装货清单将削减会议,但我想,如果它是一样的东西取=懒惰,所以它必须是做我想做的一个解决方案。

而且我在春天的应用开启了延迟加载: 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

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

spring.jpa.properties.hibernate.current_session_context_class=thread 

我也认为,延迟加载正确的属性是

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true 
+0

谢谢!它现在的作品:) – bielas

+0

不客气!请考虑接受此答案,以及您在SO上收到的所有答案(以防他们有帮助)。到目前为止,你没有那样做过。请参阅http://stackoverflow.com/help/someone-answers @bielas – baao