Using persistence.xml to create a Session to query the database? - java

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.

Related

How dynamically define properties for JPA connection with Injected EntityManager

How can I configure persistence.xml for production? My question specifically concerns the definition of parameters such as user, password and bank url, for example.
I would like to not include this data directly in the file, but in an environment variable, for example.
However, it can be any other approach, except overwriting the file settings via the entity manager factory as was said in these questions 1 e 2
This restriction exists because my EntityManager is injected via #PersistenceContext. This even generated a comment
I'm using a JavaEE application with JAX-RS, Hibernate JPA and Spring MVC

How to set index base directory in hibernate full text search dynamically

I am trying to implement Hibernate full text search in my project and in this during login user select database which he/she wants to use. Means using dynamic data source routing. Index base directory path is in hibernate.properties file and it will be different for different database.
So now I want to change it at run time on the basis of selection of database by user. When we are creating session bean in spring then code is reading Hibernate properties. I searched a lot on internet and got an idea to override hibernate.properties file at run time. But I have some doubts:
How to override it for session factory, not for JPA
I think I don't need to reinitialize the session factory after overriding property. It will automatically read new directory on overriding .
Hibernate Search will read properties from all these places:
the JPA persistence.xml file
other JPA configuration (e.g. programmatic)
the Hibernate ORM hibernate.properties file
any other Hibernate ORM properties applying to the SessionFactory
Java System Properties
Environment variables
In short: don't set the index base in a fixed configuration property but set it dynamically in any way you set the other Hibernate ORM properties.

set data source for both spring JPA and hibernate persistence API

I am new to Spring and hibernate. I am struggling to do my job with fast self learning but without luck. I would be very grateful for your patience and help.
My project involves data collecting(collect data and put it into database) and presenting(json REST-ful api).
My plan is to persist collected data with hibernate persistence API:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("hello");
EntityManager entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(this);
entityManager.getTransaction().commit();
For this to work, I have to write a persistence.xml to specify the data source.
For presentation part, I have to specify data source in spring's application.properties file.
Can I specify data source in just one place for this use case? For example, maybe I can use persistence.xml for all at once.
And preferably, I'd like to put data source outside of the jar file.
EDIT:
I end up with a single application.properties file, which defines both hibernate data settings and spring hibernate database settings:
spring.datasource.name=gateway
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/gateway
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=123456
hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/gateway

Hibernate dynamic entity model

I am creating a Java application that utilizes a JPA annotated model - the core model -. On top of these entities, at runtime, I would like to add a jar file from an external source that contains some other JPA classes definitions and mappings. The imported archive might change its class structure and mappings, but it is the application's duty to refresh the entire schema when changed.
However, when trying to add the jar to hibernate Configuration, I get a
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
The inner exception is related to the hibernate dialect:
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
However, I am sure to have specified the hibernate.dialect property in the persistence.xml file. Below is the code I am using in my application:
org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
cfg.addJar(new File("path/to/jar.jar"));
cfg.buildSessionFactory();
What am I doing wrong?
Also, could you please tell me if you find this a good approach to create a dynamically updateable schema shared between multiple applications?
I managed to solve the problem. The main point is that, when using EntityManagerFactory (the JPA API), the hibernate persistence provider only reads the persistence.xml configuration files and loads the persistence units that are specified therein.
However, using a hibernate API configuration, hibernate does not read the persistence.xml files, so one will have to explicitly specify all aspects such as dialect, connection parameters etc in the hibernate.cfg.xml file.
However, I managed to work around this issue. Indeed, in the dynamically loaded jar file, one must export the folders (the META-INF especially) and configure a persistence.xml file in there too. However, naming two persistence units the same, their corresponding classes will not get merged and neither will any other properties. By default, hibernate will load the first found persistence unit and will treat the identically-named ones as different. So, I created a more flexible core schema that allows access to multiple persistence units, while caching them in something similar to dictionaries. Consequently, for each schema in my application, I will load the corresponding persistence unit while storing all of them in a dictionary-style container, allowing the application to get notified should any changes occur to the underlying jar file.

Hibernate, load schema from file

In Hibernate, I can use hbm2dll tool to import data files after hibernate generates the schema from my model. This is fine, but I want to load the file with my database schema and make hibernate use that instead of generating one.
Let's say I have some database configured in my hibernate config
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:."/>
Now before I run my test I want to load the db with some schema defined in my schema.sql file. And I don't want to use Spring, I know there is support for it there.
I know I could use hibernate.hbm2ddl to generate schema when SessionFactory is created but I'd rather want to load my manually defined schema. Something like <jdbc:embedded-database> from spring-jdbc.
Is there any support for that in Hibernate?
link1 and link2 might help you out.
Also you can set hibernate.hbm2ddl to none, and add your import.sql file in the classpath which will be executed at the server startup.

Categories