I am using JPA (hibernate) and have the following persistence.xml
<persistence version="1.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_1_0.xsd">
<persistence-unit name="DB1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.dto1.AccessRight</class>
<class>com.dto1.Component</class>
<class>com.dto1.UserRight</class>
<properties>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
<persistence-unit name="DB2" transaction-type="RESOURCE_LOCAL">
<class>com.dto2.Auditlog</class>
<properties>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
In code i use the following to get EntityManager factory the following way:
private static final EntityManagerFactory emf_db1 = Persistence.createEntityManagerFactory(DB1_PU_NAME, getConnectionProps(DB1_PU_NAME));
private static final EntityManagerFactory emf_db2 = Persistence.createEntityManagerFactory(DB2_PU_NAME, getConnectionProps(DB2_PU_NAME));
private static Map<String, String> getConnectionProps(String pu) {
Map<String, String> dbConfProps = null;
dbConfProps = new HashMap<String, String>();
// Configure the Database properties
ConnectionEntity conn_en = ConnectionEntity.getConnectionEntity();
dbConfProps.put("hibernate.dialect", conn_en.getDbdialect());
if (pu.equals(DB2_PU_NAME)) {
dbConfProps.put("hibernate.connection.url", conn_en.getDB2_dburl());
} else {
dbConfProps.put("hibernate.connection.url", conn_en.getDB1_dburl());
}
dbConfProps.put("hibernate.connection.driver_class", conn_en.getDriver());
dbConfProps.put("hibernate.connection.username", conn_en.getUsername());
dbConfProps.put("hibernate.connection.password", conn_en.getPassword());
return dbConfProps;
}
public static javax.persistence.EntityManager getInstance(String persistanceUnit) {
logger.log("getInstance entered");
if (persistanceUnit.equalsIgnoreCase(DB1_PU_NAME)) {
return emf_idm.createEntityManager();
}
return emf_logs.createEntityManager();
}
Where conn_en has the dbConfiguration in a property file and reads from it. The thing what happens is that both database create each other tables on runtime whenever my application performs some task. During the execution i have to make entries in the tables of both databases. DB1 creates extra tables from DB2 and vice-versa. Any suggestion what is going wrong here?
Use <exclude-unlisted-classes>true</exclude-unlisted-classes> in both of your persistence units. As per this document entities that are not listed in particular persistence unit will not be managed by this unit!
Update: As per new specification for JPA 2 in jsr317
The set of managed persistence classes that are managed by a persistence unit is defined by using one or
more of the following:[81]
• Annotated managed persistence classes contained in the root of the
persistence unit (unless the exclude-unlisted-classes element is specified)
and with reference to that following is exclude-unlisted-classes xsd
<xsd:element name="exclude-unlisted-classes" type="xsd:boolean" default="true" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
When set to true then only listed classes and jars will
be scanned for persistent classes, otherwise the
enclosing jar or directory will also be scanned.
Not applicable to Java SE persistence units.
</xsd:documentation>
</xsd:annotation>
Default value of <exclude-unlisted-classes> has been changed to true if you are using JPA 2 for implementation one should use <exclude-unlisted-classes/> only instead of configuration specified above.
Related
Is there a way to recreate the database dynamically at Run-Time in EclipseLink?
Right now I have it that if the database does not exist, it creates the database at compilation time :
<?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="DefaultUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.logging.level.sql" value="INFO" />
<property name="javax.persistence.schema-generation.database.action"
value="create" />
</properties>
</persistence-unit>
</persistence>
However, I want to be able to Drop, and Recreate the database while the application is running. I.E when a user passes in some special flag, I want to call something that will drop the current database, and regenerate it.
I found that you can do that in Hibernate using the SchemaExport class
You can add <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> into persistence.xml.
It will delete all table and create it again. Be careful, all your earlier data will be lost.
absolutely. Properties can be added dynamically at runtime, so you can add the "eclipselink.ddl-generation" with value "drop-and-create-tables" when you create the EntityManagerFactory for the first time to have it drop and create the database. Of course, this isn't much use to a running app where you want to change things on the fly.
To get around this, EclipseLInk has a few tricks that allow dynamic changes and reloading of the persistence unit. Try
Map properties = new HashMap();
properties.put("eclipselink.ddl-generation", "drop-and-create-tables");
properties.put("eclipselink.ddl-generation.output-mode", "database");
//this causes DDL generation to occur on refreshMetadata rather than wait until an em is obtained
properties.put("eclipselink.deploy-on-startup", "true");
JpaHelper.getEntityManagerFactory(em).refreshMetadata(properties);
The org.eclipse.persistence.jpa.JPAHelper class is just used to unwrap the Factory to get a JpaEntityManagerFactory instance to make the non-JPA refreshMetadata call. Any number of properties or settings can be changed, and any new EntityManagers obtained after the refresh call will reflect those changes.
This is essentially a duplicate of How to locate the source of JBAS011470 error in JBoss?
But essentially, As soon as I add a second persistence unit, it gives me this error. It's ridiculous. I'm not going to disable the JPA subsystem like some people suggest - that sounds wrong.
My persistence.xml setup is as follows, where java:/NAME is set up as a datasource in standalone.xml:
<?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="name" transaction-type="JTA">
<jta-data-source>java:/NAME</jta-data-source>
<class>za.co.classes.A</class>
<class>za.co.classes.B</class>
<class>za.co.classes.C</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:jboss/persistence/NAME" />
<property name="jboss.entity.manager.jndi.name"
value="java:jboss/persistence/em/NAME" />
<property name="hibernate.dialect" value="za.co.equrahealth.dao.SQLServerDialect" />
</properties>
</persistence-unit>
The error starts as soon as I add a second persistence unit. So spring context is irrelevant.
Well, I luckily have multiple databases within the same schema, so I came up with a workaround. But it's obviously not going to solve the problem when there are multiple schemas. I think the cause of this issue might actually be a bug in JBoss.
#PersistenceContext
private EntityManager entityManager;
private EntityManager getEntityManager(String source)
{
if ("a".equalsIgnoreCase(source))
{
entityManager.createNativeQuery("USE A_DB;").executeUpdate();
}
else
{
entityManager.createNativeQuery("USE B_DB").executeUpdate();
}
return entityManager;
}
We are using Hibernate at my workplace on some project and I had to modify some queryes recently. I found it really cumbersome to modify a query, run an ant smart or ant refresh and see whether my query works. When I asked one of my colleagues he told me that it is the way we use it.
Do you have any idea how can I speed up this process? I'm looking for a tool which can connect to a database (we are using PGSQL) and run my Hibernate query there and show the results without touching ant.
For example I would be able to try this:
#Query(query = "SELECT DISTINCT l FROM Line l, IN(l.workplaces) w WHERE w.workshop.sid=:wsid", params = "wsid")
JBoss Tools for eclipse has a HQL editor that you can open from the hibernate perspective, you can test hql queries there.
We have a junit-Test for hibernate which uses the derby database as a in-memory databse. This will create the database in derby with all tables and you should be able to execute the query, to see if it is valid.
We have all queries in the orm.xml, so those queries are already checked when creating the EntityManager.
setup
private static EntityManagerFactory emf;
private static EntityManager em;
#BeforeClass
public static void before()
{
emf = Persistence.createEntityManagerFactory("persistenztest");
em = emf.createEntityManager();
}
test
#Test public void test()
{
Query q = em.createQuery(YOUR_QUERY_HERE);
List<?> list = q.getResultList();
}
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="persistenztest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<mapping-file>orm.xml</mapping-file>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="hibernate.connection.url" value="jdbc:derby:memory:sa;create=true;territory=de_DE;collation=TERRITORY_BASED:SECONDARY;"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cglib.use_reflection_optimizer" value="false" />
</properties>
</persistence-unit>
</persistence>
I am new to JPA & Hibernate. After reading some online materials I now understand what Hibernate is and how it can be used with JPA.
Now, I am trying to run this JPA & Hibernate tutorial. I've done everything they mention in this tutorial.
I don't have Oracle DB, only MySQL. So I made some changes to persistence.xml using my understanding of JPA & Hibernate (I don't know if it's correct or not... Seems to me it is.)
Here is my persistence.xml
<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="customerManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
But I don't seem to get the output they describe. It's giving me:
Customer id before creation:null
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at CustomerDAO.create(CustomerDAO.java:8)
at CustomerDAO.main(CustomerDAO.java:22)
Any suggestions will be appreciated.
Update:
I have made the changes that are asked to done. But, still getting the asme error lines!!!
They didnt mentioned anything about orm.xml in that tutorial. may it be a problem causer!!!
Just for completeness. There is another situation causing this error:
missing META-INF/services/javax.persistence.spi.PersistenceProvider
file.
For Hibernate, it's located in hibernate-entitymanager-XXX.jar, so, if hibernate-entitymanager-XXX.jar is not in your classpath, you will got this error too.
This error message is so misleading, and it costs me hours to get it correct.
See JPA 2.0 using Hibernate as provider - Exception: No Persistence provider for EntityManager.
Your persistence.xml is not valid and the EntityManagerFactory can't get created. It should be:
<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="customerManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
(Note how the <property> elements are closed, they shouldn't be nested)
Update: I went through the tutorial and you will also have to change the Id generation strategy when using MySQL (as MySQL doesn't support sequences). I suggest using the AUTO strategy (defaults to IDENTITY with MySQL). To do so, remove the SequenceGenerator annotation and change the code like this:
#Entity
#Table(name="TAB_CUSTOMER")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="CUSTOMER_ID", precision=0)
private Long customerId = null;
...
}
This should help.
PS: you should also provide a log4j.properties as suggested.
I had the same problem today. My persistence.xml was in the wrong location. I had to put it in the following path:
project/src/main/resources/META-INF/persistence.xml
I was facing the same issue. I realised that I was using the Wrong provider class in persistence.xml
For Hibernate it should be
<provider>org.hibernate.ejb.HibernatePersistence</provider>
And for EclipseLink it should be
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
If you use Hibernate 5.2.10.Final, you should change
<provider>org.hibernate.ejb.HibernatePersistence</provider>
to
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
in your persistence.xml
According to Hibernate 5.2.2: No Persistence provider for EntityManager
If you are using Maven you may have both src/{main,test}/resources/META-INF/persistence.xml. This is a common setup: test your JPA code with h2 or Derby and deploy it with PostgreSQL or some other full DBMS. If you're using this pattern, do make sure the two files have different unit names, else some versions of the Persistence class will try to load BOTH (because of course your test-time CLASSPATH includes both classes and test-classes); this will cause conflicting definitions of the persistence unit, resulting in the dreaded annoying message that we all hate so much!
Worse: this may "work" with some older versions of e.g., Hibernate, but fail with current versions. Worth getting it right anyway...
A bit too late but I got the same issue and fixed it switching schemalocation into schemaLocation in the persistence.xml file (line 1).
I have seen this error , for me the issue was there was a space in the absolute path of the persistance.xml , removal of the same helped me.
I was also facing the same issue when I was trying to get JPA entity manager configured in Tomcat 8. First I has an issue with the SystemException class not being found and hence the entityManagerFactory was not being created. I removed the hibernate entity manager dependency and then my entityManagerFactory was not able to lookup for the persistence provider. After going thru a lot of research and time got to know that hibernate entity manager is must to lookup for some configuration. Then put back the entity manager jar and then added JTA Api as a dependency and it worked fine.
my experience tells me that missing persistence.xml,will generate the same exception too.
i caught the same error msg today when i tried to run a jar package packed by ant.
when i used jar tvf to check the content of the jar file, i realized that "ant" forgot to pack the persistnece.xml for me.
after I manually repacked the jar file ,the error msg disappered.
so i believe maybe you should try simplely putting META-INF under src directory and placing your persistence.xml there.
I am newbie with JPA. I'm trying to run some sample code using JPA but I get the following exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
I put my exception message here,
INFO: Could not find any META-INF/persistence.xml file in the classpath
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at com.myJpa.TestJPA.setUp(TestJPA.java:30)
at com.myJpa.TestJPA.main(TestJPA.java:72)
Any help would be appreciated.
Well, the error is self explaining, you need to provide a META-INF/persistence.xml to use JPA. This file is used to define a "persistence unit". From the JPA 1.0 specification:
6.2.1 persistence.xml file
A persistence.xml file defines a
persistence unit. It may be used to
specify managed persistence classes
included in the persistence unit,
object/relational mapping information
for those classes, and other
configuration information for the
persistence unit and for the entity
manager(s) and entity manager factory
for the persistence unit. The
persistence.xml file is located in
the META-INF directory of the root
of the persistence unit. This
information may be defined by
containment or by reference, as
described below.
Here is a sample persistence.xml for a Java SE environment (using Hibernate as JPA provider):
<?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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mycompany.Foo</class>
<class>com.mycompany.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Some comments about the above file:
when running in a Java SE environment, you cannot rely on JTA and the transaction type must be RESOURCE_LOCAL (which is actually the default in a Java SE environment but specifying it make it more clear).
when running in a Java SE environment, you cannot use a JDNI data source and the provider will obtain connections directly from the JDBC driver so you must pass the relevant informations to the provider (driver class name, connection url, user, password). With JPA 1.0 the properties used to pass these metadata are provider specific.
To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit.
For JPA to work, you need META-INF/persistence.xml. I will assume this is a web-app, so this folder has to be in WEB-INF/classes/.
The persistence.xml file would look like this:
<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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
EntityManagerFactory emf = Persistence.createEntityManagerFactory("<JDBC connection>");
Check the correct JDBC Connection.