Tuesday, 3 December 2019

JPA Exceptions and Solutions

Faced following exceptions during first time use and solutions with the same:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: IntegratorMasterdataPU] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:119)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:853)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:73)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:78)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.gaivery.App.main(App.java:16)
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.gaivery.integrator.bnow.dao.model.CustomerAgreementComputerAttachments
at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:732)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3762)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3716)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
... 7 more

-------------

Caused by: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.gaivery.integrator.bnow.dao.model.CustomerAgreement.activePeriodFrom

String and Temporal does not work together. Changed type to Date


Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.gaivery.integrator.bnow.dao.model.CustomerAgreement.agreementPeriods

private CustomerAgreementPeriod agreementPeriods;
private List<CustomerAgreementPeriod> agreementPeriods;


Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

Moved to previous version 4.2.8 as it is found that 4.3.0 is compatible with 2.1 persistance version but we were using 2.0

--------------------
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.gaivery.integrator.bnow.dao.model.CustomerAgreement.customerId references an unknown entity: java.lang.String

It was mentioned String but it should mention entity class to which it is returning the data.

--------------------
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.gaivery.integrator.bnow.dao.model.CustomerAgreementPeriod.customerAgreementPeriod in com.gaivery.integrator.bnow.dao.model.CustomerAgreement.agreementPeriods

It is corrected when name of current entity is  given in mappedBy
@OneToMany(mappedBy = "customerAgreement", orphanRemoval = true, fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
private List<CustomerAgreementCustom> customFields;

--------------------
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.gaivery.integrator.bnow.dao.model.CustomerAgreementRoles.customerAgreement in com.gaivery.integrator.bnow.dao.model.CustomerAgreement.restrictedToRoles
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:708)

Setter was not in place in @ManyToOne

--------------------
Caused by: org.hibernate.MappingException: Composite-id class must implement Serializable: com.gaivery.integrator.bnow.dao.model.CustomerAgreementCustom

Implements serializable and added serialVersionId
--------------------
Nov 28, 2019 10:25:24 AM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000038: Composite-id class does not override equals(): com.gaivery.integrator.bnow.dao.model.CustomerAgreementCustom
Nov 28, 2019 10:25:24 AM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000039: Composite-id class does not override hashCode(): com.gaivery.integrator.bnow.dao.model.CustomerAgreementCustom
Nov 28, 2019 10:25:24 AM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000038: Composite-id class does not override equals(): com.gaivery.integrator.bnow.dao.model.CustomerAgreementRoles
Nov 28, 2019 10:25:24 AM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000039: Composite-id class does not override hashCode(): com.gaivery.integrator.bnow.dao.model.CustomerAgreementRoles
Nov 28, 2019 10:25:24 AM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000038: Composite-id class does not override equals(): com.gaivery.integrator.bnow.dao.model.CustomerAgreementPeriod
Nov 28, 2019 10:25:24 AM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000039: Composite-id class does not override hashCode(): com.gaivery.integrator.bnow.dao.model.CustomerAgreementPeriod

Added Hashcode and equals
-------------------
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115)
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1230)
at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:178)
at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89)
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:193)
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:188)
at com.gaivery.App.main(App.java:17)

Earlier transaction-type="JTA" >

Changed it to RESOURCE_LOCAL

-----------------------

Hibernate TABLES NOT GETTING CREATED

Earlier -> <property name="hbm2ddl.auto" value="create" />

JUST CHANGED IT TO <property name="hibernate.hbm2ddl.auto" value="create" /> AND DONE

The reference suggests it should be hibernate.hbm2ddl.auto
A value of create will create your tables at sessionFactory creation, and leave them intact.
A value of create-drop will create your tables, and then drop them when you close the sessionFactory.

------------------------------

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.gaivery.integrator.bnow.dao.model.CustomerAgreementComputerAttachments

@Id
@ManyToOne
    @JoinColumn(name = "id")

@Id was missing before


-------------------------------------

Caused by: java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.gaivery.integrator.bnow.model.CustomerAgreement.customerId -> com.gaivery.integrator.bnow.model.bnowCustomers

When you try to perisst parent entity from child using New Parent()

-------------------------------------

Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (MASTERDATA.FK_I0VXQXGAG2ISERDDTE3XNQ1OY) violated - parent key not found

When try to insert ID but no parent record exist yet

-------------------------------------

java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (MASTERDATA.SYS_C0021188) violated

Why try to reinsert with same ID

------------------------------------

Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
at com.gaivery.App.main(App.java:30)
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.gaivery.integrator.bnow.dao.model.CustomerAgreement.autoAttachComputersFromOrganizations
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80)
... 1 more
Caused by: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.gaivery.integrator.bnow.dao.model.CustomerAgreement.autoAttachComputersFromOrganizations

Need to find and merge

MERGE will shoot 2 Queries
FIND AND MERGE will shoot 2 queries in total.
No Updates in Object will not shoot Merge at all

em = emf.createEntityManager();
        CustomerAgreement cAgreement = em.find(CustomerAgreement.class, 32121212l);
        //CustomerAgreement cAgreement = new CustomerAgreement();
        //cAgreement.setId(32121212l);
        cAgreement.setName("singh");;
        cAgreement.setCustomerId("140");
        em.getTransaction().begin();
        em.merge(cAgreement);
        em.getTransaction().commit();;

No comments:

Post a Comment