I'm also running into this problem
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at nl.msw.compraventa.model.URLPlace_$$_javassist_44.toString(URLPlace_$$_javassist_44.java)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.search.util.HibernateHelper.unproxy(HibernateHelper.java:62)
at org.hibernate.search.engine.impl.HibernateStatelessInitializer.unproxy(HibernateStatelessInitializer.java:48)
at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:459)
at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:552)
at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:552)
at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:552)
at org.hibernate.search.engine.DocumentBuilderIndexedEntity.getDocument(DocumentBuilderIndexedEntity.java:443)
at org.hibernate.search.engine.DocumentBuilderIndexedEntity.createAddWork(DocumentBuilderIndexedEntity.java:380)
at org.hibernate.search.batchindexing.EntityConsumerLuceneworkProducer.index(EntityConsumerLuceneworkProducer.java:160)
at org.hibernate.search.batchindexing.EntityConsumerLuceneworkProducer.indexAllQueue(EntityConsumerLuceneworkProducer.java:128)
at org.hibernate.search.batchindexing.EntityConsumerLuceneworkProducer.run(EntityConsumerLuceneworkProducer.java:99)
at org.hibernate.search.batchindexing.OptionallyWrapInJTATransaction.run(OptionallyWrapInJTATransaction.java:107)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The problem seems to occur on the Lazy ManyToOne associations with optional = true. when optional = false, there is no problem.
The relationship is set up as follows:
Code:
@Entity
@Indexed
public class User implements Serializable{
@Embedded
@JsonIgnore
@IndexedEmbedded(prefix="location.")
public Address getAddress() {
return address;
}
}
@Embeddable
@Indexed
public class Address implements Serializable{
@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinColumn(name = "FK_CityID")
@IndexedEmbedded
public City getCity() {
return city;
}
@Entity
@Table(name="City")
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region=CacheRegion.CITY)
@Indexed
public class City implements Serializable{
//OK
@Override
@ManyToOne(optional=false,cascade=CascadeType.MERGE,fetch=FetchType.LAZY)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region=CacheRegion.NEVERCHANGE)
@JoinColumn(name="FK_CountryID", nullable=false)
@IndexedEmbedded(depth=1)
public Country getCountry() {
return country;
}
//OK
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="city")
@BatchSize(size=25)
@Fetch(FetchMode.SUBSELECT)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnore
@IndexedEmbedded
public Set<Barrio> getBarrios() {
return barrios;
}
//BOMB
@ManyToOne(optional=true,cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region=CacheRegion.CITY)
@JoinColumn(name="FK_regionID", nullable=true)
@IndexedEmbedded(depth=1)
public Region getRegion() {
return region;
}