Issue
I'm trying to add persistence to a faktor ips project that is based on the intro tutorial from https://www.faktorzehn.org. I have a model object that looks like this:
@Entity
public class Angebot {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
private Integer id;
private String angebotNr;
private String produktname;
private LocalDate wirksamAb;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private HausratVertrag hausratVertrag;
/**
* Erstellt ein neues Angebot {@code Angebot} f�r das gew�hlte
* Hausrat-Produkt zu dem angegeben Wirksamkeitsdatum.
*
* @param angebotNr
* eine Angebotsnummer.
* @param hausratProdukt
* ein Hausratprodukt.
* @param wirksamkeitsdatum
* das Wirksamkeitsdatum.
*/
public Angebot(String angebotNr,HausratProdukt hausratProdukt, LocalDate wirksamAb) {
this.angebotNr = angebotNr;
this.wirksamAb = wirksamAb;
}
public Angebot() {}
public String getAngebotNr() {
return angebotNr;
}
public void setAngebotNr(String angebotNr) {
this.angebotNr = angebotNr;
}
public String getProduktname() {
return produktname;
}
public void setProduktname(String produktname) {
this.produktname = produktname;
}
public LocalDate getWirksamAb() {
return wirksamAb;
}
public void setWirksamAb(LocalDate wirksamAb) {
this.wirksamAb = wirksamAb;
}
/**
* Liefert den Hausratvertrag
*
* @return Hausratvertrag
*/
public HausratVertrag getHausratVertrag() {
return hausratVertrag;
}
public void setHausratVertrag(HausratVertrag hausratVertrag) {
this.hausratVertrag = hausratVertrag;
}
@Transient
private HausratProdukt selectedProdukt;
public HausratProdukt getProdukt() {
if (produktname !=null) {
IProductComponent pc = Utils.getRuntimeRepository().getProductComponent(this.getProduktname());
return (HausratProdukt) pc;
}else {
return selectedProdukt;
}
}
public void setProdukt(HausratProdukt selectedProduct) {
this.selectedProdukt = selectedProduct;
}
/**
* Berechnet die Beitraege neu und
*/
public void neuBerechnen() {
hausratVertrag.berechneJahresbasisbeitrag();
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Angebot(produktName=").append(getProdukt().getProduktName()).append(")");
return sb.toString();
}
public Integer getId() {
return id;
}
}
the database has three tables
ANGEBOT: |ID | ANGEBOT_NR | PRODUKTNAME| HAUSRAT_VERTRAG_ID| |---|------------|------------|-------------------| |33 |kompakt | beispiel.HR-Kompakt 2022-01|33| VERTRAG: |Typ | ID | PRODUKT_CONFIGURATION| |----|----|----------------------| |HRV |33 | ... | DECKUNG: |Typ | ID | PRODUKT_CONFIGURATION|VERTRAG_ID| |------------|----|----------------------|----------| |HRG_DECKUNG |33 | ... |33|
basically how the project works is that a Contract (hausratVertrag) can have one base coverage(Grunddeckung - marked with typ = HRG_DECKUNG in the database) and 0 or many extra coverages (Zusatsdeckung - marked with typ = HRZ_DECKUNG in database, but my database doesn't have any saved). The object "Angebot" has a one to one relationship with HausratVertrag, and HausratVertrag has a one to one with HausratGrunddeckung and a one to many with HausratZusatzdeckung. Like so:
/**
* Membervariable fuer die Beziehung HausratGrunddeckung.
*
* @since 0.0.1
*
* @generated NOT
* @customizedAnnotations CONTENT-CHANGED
*/
@OneToOne(mappedBy = "hausratVertrag", fetch = FetchType.EAGER, targetEntity = HausratGrunddeckung.class, cascade = CascadeType.ALL)
private HausratGrunddeckung hausratGrunddeckung = null;
/**
* Membervariable fuer die Beziehung HausratZusatzdekung.
*
* @since 0.0.1
*
* @generated
*
*/
@OneToMany(mappedBy = "hausratVertrag", fetch = FetchType.LAZY, targetEntity = HausratZusatzdekung.class)
private List<HausratZusatzdekung> hausratZusatzdekungen = new ArrayList<>();
My problem is that when I try to load an "Angebot" from the database I get the following error.
com.vaadin.server.ServerRpcManager$RpcInvocationException: Unable to invoke method click in com.vaadin.shared.ui.button.ButtonServerRpc
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:159)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:442)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:407)
...
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155)
... 46 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in org.linkki.core.ui.aspects.ButtonInvokeAspectDefinition$$Lambda$300/0x0000000800812440 failed.
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:726)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:399)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:363)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1190)
at com.vaadin.ui.Button.fireClick(Button.java:384)
at com.vaadin.ui.Button$1.click(Button.java:57)
... 51 more
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratVertrag.hausratZusatzdekungen, 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.readSize(AbstractPersistentCollection.java:148)
at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261)
at org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratVertrag.getNumOfHausratZusatzdekungen(HausratVertrag.java:467)
at org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratVertrag.getNumOfDeckungenInternal(HausratVertrag.java:623)
at org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratVertrag.getDeckungen(HausratVertrag.java:597)
at org.linkki_framework.fips.Hausrat_linkki_app.page.VertragPage.<init>(VertragPage.java:39)
at org.linkki_framework.fips.Hausrat_linkki_app.view.VertragView.enter(VertragView.java:43)
at com.vaadin.navigator.Navigator.performNavigateTo(Navigator.java:778)
at com.vaadin.navigator.Navigator.lambda$navigateTo$9a874efd$1(Navigator.java:702)
at com.vaadin.navigator.ViewBeforeLeaveEvent.navigate(ViewBeforeLeaveEvent.java:54)
at com.vaadin.navigator.View.beforeLeave(View.java:79)
at com.vaadin.navigator.Navigator.runAfterLeaveConfirmation(Navigator.java:730)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:701)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:678)
at org.linkki_framework.fips.Hausrat_linkki_app.pmo.AngebotSectionPmo.send(AngebotSectionPmo.java:83)
at org.linkki.core.binding.dispatcher.reflection.accessor.InvokeMethod.invoke(InvokeMethod.java:61)
at org.linkki.core.binding.dispatcher.reflection.accessor.PropertyAccessor.invoke(PropertyAccessor.java:75)
at org.linkki.core.binding.dispatcher.reflection.ReflectionPropertyDispatcher.invoke(ReflectionPropertyDispatcher.java:154)
at org.linkki.core.binding.dispatcher.reflection.ReflectionPropertyDispatcher.push(ReflectionPropertyDispatcher.java:127)
at org.linkki.core.binding.dispatcher.AbstractPropertyDispatcherDecorator.push(AbstractPropertyDispatcherDecorator.java:70)
at org.linkki.core.binding.dispatcher.AbstractPropertyDispatcherDecorator.push(AbstractPropertyDispatcherDecorator.java:70)
at org.linkki.core.ui.aspects.ButtonInvokeAspectDefinition.lambda$initModelUpdate$7e820d24$1(ButtonInvokeAspectDefinition.java:45)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:706)
... 56 more
when I change the automatically generated annotation of hausratZusatzdeckungen to FetchType.EAGER, I get a different error:
com.vaadin.server.ServerRpcManager$RpcInvocationException: Unable to invoke method click in com.vaadin.shared.ui.button.ButtonServerRpc
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:159)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116)
...
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155)
... 46 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in org.linkki.core.ui.aspects.ButtonInvokeAspectDefinition$$Lambda$300/0x000000080082a840 failed.
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:726)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:399)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:363)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1190)
at com.vaadin.ui.Button.fireClick(Button.java:384)
at com.vaadin.ui.Button$1.click(Button.java:57)
... 51 more
Caused by: org.springframework.orm.ObjectRetrievalFailureException: Object [id=33] was not of the specified subclass [org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratGrunddeckung] : loaded object was of wrong class class org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratZusatzdekung; nested exception is org.hibernate.WrongClassException: Object [id=33] was not of the specified subclass [org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratGrunddeckung] : loaded object was of wrong class class org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratZusatzdekung
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:313)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy122.findById(Unknown Source)
at org.linkki_framework.fips.Hausrat_linkki_app.HrService.findAngebotById(HrService.java:25)
at org.linkki_framework.fips.Hausrat_linkki_app.view.VertragView.enter(VertragView.java:37)
at com.vaadin.navigator.Navigator.performNavigateTo(Navigator.java:778)
at com.vaadin.navigator.Navigator.lambda$navigateTo$9a874efd$1(Navigator.java:702)
at com.vaadin.navigator.ViewBeforeLeaveEvent.navigate(ViewBeforeLeaveEvent.java:54)
at com.vaadin.navigator.View.beforeLeave(View.java:79)
at com.vaadin.navigator.Navigator.runAfterLeaveConfirmation(Navigator.java:730)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:701)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:678)
at org.linkki_framework.fips.Hausrat_linkki_app.pmo.AngebotSectionPmo.send(AngebotSectionPmo.java:86)
at org.linkki.core.binding.dispatcher.reflection.accessor.InvokeMethod.invoke(InvokeMethod.java:61)
at org.linkki.core.binding.dispatcher.reflection.accessor.PropertyAccessor.invoke(PropertyAccessor.java:75)
at org.linkki.core.binding.dispatcher.reflection.ReflectionPropertyDispatcher.invoke(ReflectionPropertyDispatcher.java:154)
at org.linkki.core.binding.dispatcher.reflection.ReflectionPropertyDispatcher.push(ReflectionPropertyDispatcher.java:127)
at org.linkki.core.binding.dispatcher.AbstractPropertyDispatcherDecorator.push(AbstractPropertyDispatcherDecorator.java:70)
at org.linkki.core.binding.dispatcher.AbstractPropertyDispatcherDecorator.push(AbstractPropertyDispatcherDecorator.java:70)
at org.linkki.core.ui.aspects.ButtonInvokeAspectDefinition.lambda$initModelUpdate$7e820d24$1(ButtonInvokeAspectDefinition.java:45)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:706)
... 56 more
Caused by: org.hibernate.WrongClassException: Object [id=33] was not of the specified subclass [org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratGrunddeckung] : loaded object was of wrong class class org.linkki_framework.fips.Hausrat_linkki_app.model.hasurat.HausratZusatzdekung
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1582)
at org.hibernate.loader.Loader.getRow(Loader.java:1533)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:930)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:306)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2204)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:60)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:143)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2122)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:692)
at org.hibernate.type.EntityType.resolve(EntityType.java:434)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:154)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:128)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:238)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:209)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3967)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1022)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:639)
at org.hibernate.type.EntityType.resolve(EntityType.java:431)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:154)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:128)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1133)
at org.hibernate.loader.Loader.processResultSet(Loader.java:992)
at org.hibernate.loader.Loader.doQuery(Loader.java:930)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2617)
at org.hibernate.loader.Loader.doList(Loader.java:2600)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)
at org.hibernate.loader.Loader.list(Loader.java:2424)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529)
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:54)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372)
at com.sun.proxy.$Proxy149.getSingleResult(Unknown Source)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:206)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:85)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 87 more
So the way I understand it is that what was loaded from the Database was an instance of "HasuratZusatzdeckung" and not "HausratGrunddeckung", which is what I actually wanted. I find this very strange because I don't even have a "HausratZusatzdeckung" saved in the database, but only a "HausratGrunddeckung". Is the problem that I manually changed the annotation that was generated by the faktor ips code generator from FetchType.LAZY to FetchType.EAGER? Also, for the annotation of the attribute "hausratGrunddeckung" I added the CascadeType.ALL to the originally generated annotation. Is that a problem? If you could help me understand why this error occured and how to fix it, I would be very grateful.
the code for my HausratGrunddeckung
is
@Entity
@DiscriminatorValue("HRG_DECKUNG")
@IpsPolicyCmptType(name = "hasurat.HausratGrunddeckung")
@IpsAttributes({ "beitragGemaessZahlweise" })
@IpsAssociations({ "HausratVertrag" })
@IpsConfiguredBy(HausratGrunddeckungstyp.class)
@IpsDocumented(bundleName = "org.linkki_framework.fips.Hausrat_linkki_app.model.modell-label-and-descriptions", defaultLocale = "de")
public class HausratGrunddeckung extends Deckung {
/**
* Diese Konstante enthaelt den Namen der Beziehung hausratVertrag.
*
* @since 0.0.1
*
* @generated
*/
public static final String ASSOCIATION_HAUSRAT_VERTRAG = "hausratVertrag";
/**
* Diese Konstante enthaelt den Namen der Eigenschaft beitragGemaessZahlweise.
*
* @since 0.0.1
*
* @generated
*/
public static final String PROPERTY_BEITRAGGEMAESSZAHLWEISE = "beitragGemaessZahlweise";
/**
* Membervariable fuer das Parent-Objekt: HausratVertrag.
*
* @since 0.0.1
*
* @generated
*/
@OneToOne(fetch = FetchType.EAGER, targetEntity = HausratVertrag.class)
@JoinColumn(name = "VERTRAG_ID")
private HausratVertrag hausratVertrag;
...
}
the code for my HausratZusatzdeckung
is
@Entity
@DiscriminatorValue("HRZ_DECKUNG")
@IpsPolicyCmptType(name = "hasurat.HausratZusatzdekung")
@IpsAttributes({ "versSumme" })
@IpsAssociations({ "HausratVertrag" })
@IpsConfiguredBy(HausratZusatzdekungstyp.class)
@IpsDocumented(bundleName = "org.linkki_framework.fips.Hausrat_linkki_app.model.modell-label-and-descriptions", defaultLocale = "de")
public class HausratZusatzdekung extends Deckung {
/**
* Diese Konstante enthaelt den Namen der Beziehung hausratVertrag.
*
* @since 0.0.1
*
* @generated
*/
public static final String ASSOCIATION_HAUSRAT_VERTRAG = "hausratVertrag";
/**
* Diese Konstante enthaelt den Namen der Eigenschaft versSumme.
*
* @since 0.0.1
*
* @generated
*/
public static final String PROPERTY_VERSSUMME = "versSumme";
/**
* Membervariable fuer das Parent-Objekt: HausratVertrag.
*
* @since 0.0.1
*
* @generated
*/
@ManyToOne(fetch = FetchType.LAZY, targetEntity = HausratVertrag.class)
@JoinColumn(name = "VERTRAG_ID")
private HausratVertrag hausratVertrag;
...
}
the model classes from which the code was generated look like this:
Deckung
: the parent of HausratGrunddeckung
and HasuratZusatzdeckung
Solution
Try wrapping the method where you load the Angebot
with @Transactional
so that there is a session existing when lazy loading the assocation. The exception tells you that you are trying to lazy load the association without a transaction (session) with could not initialize proxy - no Session
.
I suggest you dig into this article by one of the Hibernate maintainers Vlad Mihalcea where he shows the best way to deal with LazyInitializationException
s. Eager fetching is definetly not the way to go, in fact it is actually considered a code smell. Apart from the join fetch
approach described by Vlad (where you need a JPQL query), you could also use Spring Data JPAs EntityGraph
annotation to fetch certain associations eagerly (examples here).
Regarding your second problem: I don't really understand what you mean by "automatically generated annotation". A Java annotation can't be generated automatically or added during runtime, it needs to be placed before compiling your code. You could try omitting the targetEntity
attributes in your association annotations, I have never needed this attribute so far and I don't think that you need it as the Javadoc states that Optional only if the collection property is defined using Java generics.
. Could you also show your code for the HausratGrunddeckung
and HausratZusatzdekung
class?
Answered By - Times
Answer Checked By - Katrina (JavaFixing Volunteer)