With the following code, the EntityManagerFactory's metamodel has no packages/classes.
I'm missing the parallel sessionFactory.setPackagesToScan() in LocalSessionFactoryBean.
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, hibernateReactiveProperties());
Metamodel metamodel = emf.getMetamodel();
Mutiny.SessionFactory reactiveSessionFactory = emf.unwrap(Mutiny.SessionFactory.class);
solved by adding class tag for each entity in persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="default" >
<provider>org.hibernate.reactive.provider.ReactivePersistenceProvider</provider>
<class>com.company.EntityClassName</class>
</persistence-unit>
</persistence>
Related
I've tried to declare converter in my orm.xml file:
orm-pg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_2_1.xsd"
version="2.1">
<converter class="com.example.PgUuidConverter" auto-apply="true"/>
</entity-mappings>
persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="jpa" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=postgres-ds)</jta-data-source>
<mapping-file>META-INF/orm-pg.xml</mapping-file>
</persistence-unit>
</persistence>
Because the converter was not registered, I've added for debug another mapping-file:
<mapping-file>META-INF/orm-pg2.xml</mapping-file>
This file doesn't exist. To my surprise, the persistence unit has started without even the slightest warning in logs.
Now I'm confused because I have no idea if my converter was not registered because the orm.xml is wrong or because it was simply not loaded.
How can I debug if the mapping-file was loaded succesfully or not? Why the missing files that are referenced are ignored?
I'm deploying in Karaf Container using Aries JPA 2.5.0
I need two or more than two connections in my web application using jpa
To use different data sources, add multiple persistence units (say, source-1 and source-2 in persistence.xml and create multiple EntityManagerFactoryes by name):
EntityManagerFactory emf1 = Persistence.createEntityManagerFactory("source-1");
EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("source-2");
or, if you're working on Spring or Java EE application server, inject them by name also:
#PersistenceUnit(name = "source-1")
EntityManagerFactory emf1;
#PersistenceContext(unitName = "source-2") // as an option
EntityManager em2;
persistence.xml will thus look like the following:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="source-1" transaction-type="RESOURCE_LOCAL">
<properties>
<!-- source-1 properties here -->
</properties>
</persistence-unit>
<persistence-unit name="source-2" transaction-type="RESOURCE_LOCAL">
<properties>
<!-- source-2 properties here -->
</properties>
</persistence-unit>
</persistence>
Example of how to configure persistence unit, create EntityManager to manage entities and execute queries can be found here.
For single datasource jpa will use multiple connections internally.So you don't need to do anything.
I need to load queries in a repository from a property file. For example, here:
#Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
#Query(value="SELECT * FROM student where year= :le", native=true)
public List<Student> getStudentsByLevel(#Param("le") int level);
}
I need to load the "SELECT * FROM student where year= :le" string from a property file. Is there any way to do it?
As suggested above I have overcome this problem by using xml configured named queries. Below is the example.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatePersistenceUnit">
<!-- <mapping-file>META-INF/jpql/oracle-quries.xml</mapping-file> -->
<mapping-file>META-INF/jpql/mysql-quries.xml</mapping-file>
</persistence-unit>
</persistence>
mysql-quries.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd ">
<named-native-query name="Nodecontrol.lockNodeController">
<query>
SELECT * FROM student where year= :le
</query>
</named-native-query>
</entity-mappings>
In oracle-quries.xml and mysql-quries.xml I have defined all the native queries respectively. When I need to change the query without changing the class file I just need to mention the file name i need to use in persistence.xml.
Installing my web application to WebSphere Application Server 8 fails due to a syntax error in my persistence.xml. According to my IDE, there is no error. What is the actual problem?
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
>
<persistence-unit name="myapp">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
</persistence-unit>
</persistence>
Excerpts from stack trace (SystemOut.log):
1:
Caused by: com.ibm.ws.metadata.MetaDataException: CWWJP0040E: The persistence.xml in application PA_Appl, module Appl-0.1.war, has a syntax error at line number: 6, column number: 2.
2:
Caused by: org.xml.sax.SAXParseException: expected root element {http://java.sun.com/xml/ns/persistence}persistence
Please comment, if you need more information.
Try this
<?xml version="1.0" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="myapp">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
</persistence-unit>
</persistence>
I have a question regarding the class integration in the persistence xml.
Is something like this possible?
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ranea">
<class>com.dal.pojo.*</class>
</persistence-unit>
</persistence>
I know that this does not work....but I have a rather big database structure and I would appreciate an answere how to include my pojo classes in the persistence unit easily.
If you're using Eclipse, you can right click on persistence and use "synchronize class list".