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.
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
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.
I use liquibase to set up my database schema. I disable hibernate to create anything. Because of that, my import.sql is ignored. Is there any way to configure spring boot or liquibase or any other part to load test data after liquibase has created the tables?
If you need something crude (i.e, not for actual data migrations), you can use Spring's JDBC initializer in Spring Boot. In a nutshell, you'll need to:
create a data-test.sql to load your data, and place it in your src/main/resources directory. For different environments, just use the naming convention data-{platform}.sql
add applications-test.properties to src/main/resources with the following:
spring.datasource.platform=test # this actives data-test.sql above
spring.datasource.continueOnError=???? # depends on your needs
to active the application-test.properties during your testing, make sure "test" is one of the profiles that's active during your integration test. One way to do this is to annotate your test class with #ActiveProfiles({"test", ...}).
The simplest way seems to load the data with liquibase. You can do it with a normal Changeset (XML or JSON) or a Changeset in SQL-Format.
The most common way is to load CSV-Data or run an existing SQL-File.
I'm creating a Java EE application that's using JPA for data access. Initially I used EclipseLink, but the bundled Geronimo Javamail implementation that it depends on via Moxy was giving me some odd issues and I couldn't force it to use Sun Javamail, so I've switched to Hiberate as the JPA provider.
EclipseLink was ignoring the lazy/eager annotations, it was eagerly loading everything. Hibernate pays attention to those annotations, and so dependant objects aren't loaded. That means if I load say a person, with a lazy loading of the persons parents, if I access the parents in the view it's not lazy loaded, I get an exception that says the database session's closed.
I understand there are two ways to get around this:
- Open Session in View pattern/antipattern (which isn't great from a layered point of view, and can have the N+1 database calls problem, but is easy)
- Have service methods that load all the data the view needs (which makes the service layer messy with lots of duplicate methods to get varying amounts of data)
For reference my layers are View -> Controller -> Service -> Entity Object -> JPA. I don't have a dto as it's a small app and I don't like the DTO anti-pattern.
Thinking about the Open Session in View pattern, the problem is the OpenSessionInViewInterceptor and OpenSessionInViewFilter are both Hibernate specific, and both require you to declare a hiberate session on your Spring configuration files. I prefer to stay with pure JPA, configured with a persistence.xml file.
What are my options here? Can I just change my Spring configuration to load Hibernate explicitly, but then use pure JPA inside my application? Is there a pure JPA way achieve the same result, lazy loading from the view?
it sounds odd the EclipseLink ignores standard JPA annotations.
the javamail implementation should not be in any way related to JPA
there is OpenEntityManagerInViewX (filter/interceptor) which handle the same scenario for JPA
you can easily go without this pattern if you declare and use your collections wisely.
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.