Grails not mapping entities from hibernate.cfg.xml file - java

I am trying to create a sample project to demonstrate the use of grails 5 with gorm 7.
Here I am trying to map hibernate entities using hibernate.cfg.xml file. The hibernate.cfg.xml file is placed under grails-app/conf/ diectory.
The hibernate entity java classes are annotated with javax.persistence.Entity annotation.
But these entity classes are not mapped by grails and performing any save, list etc operations on these entities throws org.hibernate.MappingException: Unknown entity exception.
Please find the sample project here.

Can you try moving the hibernate.cfg file to hibernate folder or refer the path in application.yml https://docs.grails.org/3.0.x/guide/hibernate.html

Related

hibernate entity discovery only works inside the same folder

I have a Spring Boot project with Hibernate.
The project does not have a hibernate.cfg.xml file.
The project also does not have an applicationContext.xml file.
Nevertheless, all works well.
However, when I start adding new hibernate entities, then things go wrong. For some reason, the system only finds them when I put them inside the package of the other hibernate entities.
So, this leads me to believe that I do need additional configuration to help the auto-discovery mechanism. But what is the state-of-art in 2020 ? (I assume that the above xml files are now deprecated).
If you use spring + hibernate then it solved by #ComponenScan annotation.
If pure hibernate then I think you need persistence.xml
EntityManager is the class that performs database interactions in JPA.
It is initialized through a configuration file named persistence.xml.
This file is found in the META-INF folder in your CLASSPATH, which is
typically packaged in your JAR or WAR file. The persistence.xml file
contains:
The named "persistence unit," which specifies the persistence framework you're using, such as Hibernate or EclipseLink.
A collection of properties specifying how to connect to your database, as well as any customizations in the persistence
framework
A list of entity classes in your project
I totally overlooked these annotations which were present on the SpringBootApplication class.
#SpringBootApplication(scanBasePackages = {"com.domain.foo.bar.*"})
#EnableJpaRepositories(basePackages ={"com.domain.foo.bar.*"})
#EntityScan(basePackages ={"com.domain.foo.bar.*"})
public class SpringBootApplication extends SpringBootServletInitializer {
}
I needed to add my packages here.

How to create a Hibernate mapping in IntelliJ and H2

I connect h2 database and Hibernate in IntelliJ, but when I run the JPA Console I receive this:
StudentBean is not mapped [from StudentBean]
I am new in Hibernate and I will be crateful if someone shom me step by step how to mapping classes in JPA Hibernate
P.S. I awready import all necessary libraries
The problem with maping is resolved for me:
image
On the image you can see the correct path tree and my jar libraries.
But this path working for desctop app not for web.

I can not add hibernate in intellij

I use IntelliJ to hibernate but it does add error.
I follow video https://www.youtube.com/watch?v=bGl4u44WRiI
this error:
[2016-05-16 07:44:21] javax.persistence.PersistenceException:
[PersistenceUnit: persistenceUnit] class or package not found
[2016-05-16 07:44:21] java.lang.ClassNotFoundException:
models.PmaExportTemplates
From what I noticed about intellij is if you import the hibernate from the project structure to include support. You don't get all the files required. I am not sure what you are trying to do if you are wanting to use JPA and hibernate as a provider, then I would download hibernate separately from hibernate.org and put the lib -> required folder contents and JPA folder content into its own folder along with what jdbc driver you would be using. Ex: SQL, MYSQL ect. If you are wanting to use the function in intellij you also have to provide a persistance.xml file and Java EE persistance support. I would suggest if you are just wanting to save something using hibernate that you download the files as said and then create the hibernate library when you make the project. If you want to avoid using EntityManagerFactory and use native hibernate API then you would be using hibernate.cfg.xml and a (class).hbm.xml mapping file or JPA annotations in the source code. I would suggest using SessionFactory and configure that full support of hibernate. Can you clarify what you want to do?

Adding jpa/hibernate #Entity in submodule in playframework to entity manager to scan

I have problem with hibernate entity while working with submodules in playframework.
Normally (in single app without submodules) I've used that code:
package models;
#Entity
public class AppMode {
public static AppMode getCurrentConfigurationEntry() {
return JPA.em().find(AppMode.class, 1L);
}
}
But now I have to tell hibernate entity manager to scan submodules models, cause I'm getting an error:
[IllegalArgumentException: Unknown entity: AppMode]
My model class in submodule is in package:
package models.common;
I've already tried:
return JPA.em().find(models.common.AppMode.class, 1L);
return JPA.em().find(common.models.AppMode.class, 1L);
but I'm getting the same error:
[IllegalArgumentException: Unknown entity: models.common.AppMode]
My question is: How to configure hibernate in play subproject to add submodules models class to classpath at runtime?
I have in both build.sbt files declared libraryDependencies with hibernate.
Should I have persistence.xml file/configuration for each module?
I have to mannually add mapping thought xml persistance file and also using Annotation Mapping: #Entity
I think that your problem is about JPA configuration. Check this this answer which explains JPA multi-module configuration.
Good luck!
I think Hibernate only scans for JPA Entities inside jar files, and not in classes/ folders for example, or it only scans in the jar with the persistence.xml, or something like that. Here's a solution with pure Hibernate on JavaSE, no Spring: https://stackoverflow.com/a/41845759/377320

Hibernate/JPA Annotations -- Unknown Entity

An application that has been working well for months has stopped picking up the JPA #Entity annotations that have been a part of it for months. As my integration tests run I see dozens of "org.hibernate.MappingException: Unknown entity: com.whatever.OrderSystem" type errors.
It isn't clear to me what's gone wrong here.
I have no hibernate.cfg.xml file because I'm using the Hibernate Entity Manager. Since I'm exclusively using annotations, there are no .hbm.xml files for my entities. My persistence.xml file is minimal, and lives in META-INF as it is supposed to.
I'm obviously missing something but can't put my finger on it.
I'm using hibernate-annotations 3.2.1, hibernate-entitymanager 3.2.1, persistence-api 1.0 and hibernate 3.2.1. hibernate-commons-annotations is also a part of the project's POM but I don't know if that's relevant.
Is there a web.xml entry that has vanished, or a Spring configuration entry that has accidentally been deleted?
verify in your entity classe that you import javax.persistent.Entity and not org.hibernate.annotations.Entity
I seem to recall I had a similar issue at one time.
Its a long shot, but if you're not already doing this, have you explicitly specified the provider you are using?
<persistence ...>
<persistence-unit ...>
<provider>org.hibernate.ejb.HibernatePersistence</provider> <---- explicit setting
....
</persistence-unit>
</persistence>
Otherwise, I'm not sure?
Is this happening for one specific class (few classes) or all the entity classes. The persistence.xml file has a list of class and or jar files that need to be scanned for #Entity mappings. If it was working earlier you can do a quick diff with the version of persistence.xml that was working correctly. Another issue could be that it is picking up a different persistence.xml file - you can verify this by introducing an error (for e.g., make the xml invalid) in the persistence.xml.

Categories