My project uses Morphia to map my Java entities to MongoDB. What I want to achieve is to create a persistence.xml and configure it so that I will not have to configure properties from source code. How can I achieve this ?
Morphia doesn't use persistence.xml. You'll have to use the annotations.
Related
I'm working on a spring boot application using spring data jpa at Spring Tool Suite IDE. I need to create entity classes from database but I couldn't find any answer to how to do it.
I saw an answer about adding JPA Facet adding and using it. But I couldn't add JPA Facet because my application is maven web , not jee application.
Mapping from db table -> entity:
Eclipse:
Make an empty JPA Project, set data sources and use JPA Tools > Generate entities from Tables. Then copy-paste model classes to your project.
IntelliJ:
Install plugin JPA Buddy and make a reverse engineering:
Reverse engineering video
Netbeans:
How to
If you know other ways to generate entities, please tell us.
You don't create entity classes from database. You create models to map your table and its relationships with other tables.
Application Properties to configure JPA mapping.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Map your classes to a table.
https://www.vogella.com/tutorials/JavaPersistenceAPI/article.html#simple
I am familiar with using Hibernate and the hibernate.cfg.xml file to create a Session.
However, I have a persistence.xml file within my project, not the hibernate.cfg.xml file.
How do I use it to create a Session that I can then use to query my database?
If you want to use persistance.xml you should use EntityManager. How to use EntityManager you can find in Hibernate, JPA – Part 1 tutorial.
The key config file for JPA is persistence.xml. This lives in the
META-INF directory. It details what the persistence driver to use
and what JNDI data source to connect to. Additional properties can
also be specified, in this case we’ll include some Hibernate
properties.
Next pat is about Hibernate, JPA & Spring MVC – Part 2.
My application uses persistence.xml to store the DB connection parameters. How can I initialize Flyway?
Flyway doesn't support persistence.xml (as of 3.0).
Find a different way to store the DB config (eg, Spring property files) or parse the persistence.xml manually.
An easy way is to simply read in the XML, extract the values and pass them to Flyway.setDataSource()
I'm always looking for ways to eliminate redundant code from my project.
I am using Hibernate with Spring. I have configured Spring's JPA LocalContainerEntityManagerFactoryBean, which lets me pass a bunch of properties to the JPA provider (Hibernate), which means I don't need to set those properties in the persistence.xml file.
Furthermore, Hibernate can find my persistent classes without my having to actually identify them in persistence.xml, so those <class> lines go away too.
I'm left with one line in my persistence.xml:
<persistence-unit name="bigDatabase" transaction-type="RESOURCE_LOCAL"/>
Does anyone know a way to eliminate persistence.xml completely and move the persistence unit definition into the Spring configuration file? I tried Googling of course but couldn't find an answer.
Thanks!
persistence.xml is not mandatory for JPA configurations. You can create your entity manager with the EntityManagerFactory.createEntityManager(map)
method. But LocalContainerEntityManagerFactoryBean doesn't have methods that helps creation of entity manager with a map. So you have to extend LocalContainerEntityManagerFactoryBean and override some methods according to your needs.
Is there a way to load only selected entities with Hibernate?
I would like to only load a selected handful for integration testing.
I create an AnnotationConfiguration programatically for this kind of tests and use methods such as addAnnotatedClass(Class) to "enlist" entities.
I ended up using a custom persistence.xml and gave it to the EntityManagerFactory in the spring config.