Using EclipseLink 2.6.0 on WebLogic 12.2.1.4.0 I started to get the following exception upon invoking EntityManager.merge()
java.lang.NullPointerException: null
at org.eclipse.persistence.descriptors.changetracking.AttributeChangeTrackingPolicy.updateListenerForSelfMerge(AttributeChangeTrackingPolicy.java:130)
at org.eclipse.persistence.mappings.CollectionMapping.mergeIntoObject(CollectionMapping.java:1642)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4136)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:601)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313)
at org.eclipse.persistence.mappings.CollectionMapping.mergeIntoObject(CollectionMapping.java:1638)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4136)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:601)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313)
at org.eclipse.persistence.mappings.CollectionMapping.mergeIntoObject(CollectionMapping.java:1638)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4136)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:601)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313)
at org.eclipse.persistence.mappings.ObjectReferenceMapping.mergeIntoObject(ObjectReferenceMapping.java:499)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4136)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:601)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3524)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeCloneWithReferences(RepeatableWriteUnitOfWork.java:387)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3484)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:553)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:530)
at weblogic.persistence.BasePersistenceContextProxyImpl.invoke(BasePersistenceContextProxyImpl.java:97)
at weblogic.persistence.TransactionalEntityManagerProxyImpl.invoke(TransactionalEntityManagerProxyImpl.java:164)
at weblogic.persistence.BasePersistenceContextProxyImpl.invoke(BasePersistenceContextProxyImpl.java:86)
at com.sun.proxy.$Proxy5085.merge(Unknown Source)
at com.acme.dao.Dao.save(Dao.java:86)
I found a workaround which is setting
<property name="eclipselink.weaving.changetracking" value="false"/>
in persistence.xml
However, this disables weaving changetracking for the whole persistence unit which might have an undesired global impact.
The transaction involves a bunch of objects with twisted #ManyToOne(fetch = FetchType.LAZY) relationships and the exception is thrown only when trying to delete one of the objects and merging another.
My question is: is it possible to disable weaving changetracking only for the entities involved in the transaction where the NullPointerException occurs?
I have tried adding #Mutable annotations on the fields and all types of #ChangeTracking on the entities but none of those eliminated the NullPointerException.
I try:
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
protected List<URL> urlList;
But I get error:
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'entityManagerFactory' defined in ServletContext resource
[/WEB-INF/servlet-context.xml]: Invocation of init method failed; nested
exception is org.hibernate.AnnotationException: Use of #OneToMany or
#ManyToMany targeting an unmapped class:
my.package.MyModel.urlList[java.net.URL]
I saw this answers, but I can't mapping list of URLs.
Using an ElementCollection and CollectionTable is the way to map a Collection of NON-ENTITY elements.
#ElementCollection
#CollectionTable(name="MY_URLS")
Collection<URL> urlList;
Then it would depend on whether your JPA provider supports persistence of that element type (URL) out of the box. My provider (DataNucleus) does. You always can use AttributeConverter if it doesn't
What version of Hibernate are you using? Also, in your database, what is the datatype of the column; is it varchar, or something else?
From what I am understanding, it looks like your issue is that Hibernate does not know how to translate the column in the database to the java.net.URL class, a Hibernate type would need to be defined to tell Hibernate how to handle this conversion.
It looks like Hibernate does have a built-in type that should handle this conversion, assuming you are using a version of Hibernate that has it -- 3.6 or greater, I think.
Try adding this annotation to your List:
#Type(type="org.hibernate.type.UrlType")
If your version of Hibernate does not have org.hibernate.type.UrlType or if your database column data type is not a varchar, you will need to create a custom Hibernate type that defines what Hibernate needs to do to convert whatever data type in the database to a java.net.URL and back. Let us know if it gets to that point and we can give you more information.
I've recentlly added Hibernate Search to an existing project with existing entity classes and marked a few entities and fields with Indexed/Field.
However I get following exception at bootstrap time:
org.hibernate.search.SearchException: HSEARCH000135: Unable to guess FieldBridge for <some entity class which is not marked with #Indexed>
The class which is mentioned changes from time to time, but it is not a class which is marked with #Indexed nor it is related with an entity which is indexed.
Why does it even look at these classes?
I'm using Hibernate 4.2.8 with JPA2 and Hibernate Search 4.4.0.
If the entity in question has a composite ID, it is probably caused by this bug.
I want to persist the XML of a JAXB object in a CLOB column in the table of the owning entity. OpenJPA ships with support for such constructs using its XMLValueHandler.
I followed this tutorial from IBM.
My sample code is:
#Entity
#Access(AccessType.FIELD)
public class EntityContainingXml {
#Id
private Long id;
#Persistent
#Strategy("org.apache.openjpa.jdbc.meta.strats.XMLValueHandler")
#Column(name = "xml")
#Lob
private SomeJaxbType xmlStuff;
//...
}
However the field xmlStuff is not recognized as persistent state by the OpenJPA enhancer. It does not make a change if SomeJaxbType is contained in the persistence unit.
What do I need to do so that the OpenJPA enhancer recognizes the field xmlStuff as persistent state?
The problem was a classpath issue due to maven dependencies. I can't say what exactly caused this issue. The original classpath contained some combination of the org.apache.openjpa artifacts openjpa-persistence-jdbc, openjpa-persistence, openjpa and openjpa-all. That was causing the org.apache.openjpa.persistence.PersistenceMetaDataFactory to be used during build time enhancement. But the parser org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.AnnotationPersistenceMetaDataParser created by this factory is not able to recognize #Strategy.
Now only openjpa-persistence-jdbc is currently used as a compile time dependency. And the build time enhancement has a dependency on the artifact openjpa.
I debugged through the maven build to find that out. In my case it was not possible to use the configuration property openjpa.MetaDataFactory to set the appropriate meta data factory, because it caused a class cast exception. I then started to throw out OpenJPA. During that I build and ran the application once more and it suddenly worked.
I am receiving the following Hibernate Exception:
org.hibernate.AnnotationException: #OneToOne or #ManyToOne on cz.rohan.dusps.model.Switchport.konfiguracniTemplateAccess references an unknown entity: cz.rohan.dusps.model.KonfiguracniTemplate
org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:103)
org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541)
org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523)
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
cz.rohan.dusps.helper.SessionFactoryHelper.initFactory(SessionFactoryHelper.java:122)
cz.rohan.dusps.helper.SessionFactoryHelper.getSessionFactory(SessionFactoryHelper.java:134)
cz.rohan.dusps.filter.HistorieZmenFilter.doFilter(HistorieZmenFilter.java:102)
cz.rohan.dusps.filter.CharsetFilter.doFilter(CharsetFilter.java:41)
after ~20 hours spent on the problem with various people, having read every possible blog or forum, I am really getting desperate here.
This is a mid-sized project. I should mention the database is Postgres 9.1 and we generate the DB using a modelling tool. Hibernate connects to the database but does not generate it.
I have created a new entity in the database, it's called "KonfiguracniTemplate" (configuration template). I have created the model, controller, form, validators, .jsp's, all basically copied 1:1 from an existing entity of a similar nature. I can now work with KonfiguracniTemplate, CRUD is fully working.
The problem comes when I reference this KonfiguracniTemplate from the entity called Switchport. In the DB there is a relation between the two:
Switchport 1:1 ... 0:N KonfiguracniTemplate (switchport always references a KonfiguracniTemplate; a KonfiguracniTemplate MAY BE referenced zero or more times)
Switchport has FK konfiguracniTemplateAccess_id for this relation.
In .../model/Switchport.java the relation is mapped just like all other relations that are working:
#ManyToOne
#JoinColumn(nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;
I have tried various forms:
#ManyToOne
#JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;
or
#ManyToOne(targetEntity=KonfiguracniTemplate.class)
#JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;
I have also checked:
both entities are in the same package
they are both annotated "#Entity" using "import javax.persistence.Entity;"
the build produces no error/warning messages
as long as the reference in Switchport is commented out, everything is fine
No matter what I try I cannot get rid of the "references an unknown entity" exception. Can somebody please share an idea what is happening or maybe how to debug the issue? The stacktrace at the top of the post is all I get in the logs.
All input is greatly appreciated!
Just add the class Team to the "hibernate-cfg.xml" file, because Hibernate doesn't identify without adding into it.
Possible Solutions:
1) Ensure that the entity has been appropriately referenced in hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
...
<mapping class="com.project.entitytwo.model.EntityTwo"/>
...
</session-factory>
2) Ensure that #Entity has been specified at the class-level ( at the top of the class )
#Entity
#Table( name="ENTITY_TWO" )
public class EntityTwo extends AnyClass
{
...
I just had this problem, with entity a referencing entity b. Both entities were located in a common JAR outside of the web project I was working on; a was declared in persistence.xml but b wasn't. I put b in a <class> tag in persistence.xml and it worked!
I ran into this problem when using Spring and not using the hibernate.cfg.xml file. It was solved by adding the fully qualified package name of the Model class to the setPackagesToScan method of LocalSessionFactoryBean class.
Finally got the solution from another developer on the team!
The classes need to be imported before the SessionFactory object is created. Here the import for the new class was missing, so it was unknown to the SessionFactory object.
Anyway, thanks everyone for your hints!
There is one more chance of getting such exception; when you don't mention your mapping class in hibernate.cfg.xml file.
As mentioned above.
I had same exception... I just forget add annotation (#Entity, and #Table) on MASTER class(class with Primary key)
so solution is double check every annotation in your entities , I mean not only #ManyToOne and #OneToMany like i did.
if your two entity in diffrent project,you can scan KonfiguracniTemplate's package in other project.you can do like this in spring boot
#EntityScan({"com.thispackage.entity","com.KonfiguracniTemplatepackage.entity"})
I'll give you a solution that should work for the same error with Spring Boot. This has less to do with the original question, but today, people would probably look for this answer instead because noone uses XML configuration today anymore.
I suffered the same problem and found the solution on this website: https://www.programmersought.com/article/1617314625/
He even describes this very question he would have looked up, but then I'm asking myself: why didn't he answered here after finding the solution? LOL
His own words:
In the Spring Boot project, the default scan package is the package where the main method is located, that is, only the entity classes in the same package as the main method will be discovered. This way you can understand why User is not found: because User is an entity class in another module. Spring Boot does not scan other packages at all;
Configure the #SpringBootApplication annotation on the main method that launches the application, telling Spring Boot that those packages need to be scanned: #SpringBootApplication(scanBasePackages = {“com.xiaomo.*”})
Then, User can be found.
So you basically reconfigure SpringBoot to scan more packages to include the other ones.
My personal addition: you could also move your packages into the package where the starter is located or move the starter a package up (that's what I did).