Spring JPA & Hibernate persistence_1_0.xsd not found - spring

So i Have this problem in implementing JPA and Hibernate in Spring WS. I have configured everything correctly and according to tutorial it should work - but it is not.
Problem lies in persistance.xml, here it is how it looks:
<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/persistance_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
and the exception i am getting:
Caused by: java.io.FileNotFoundException: class path resource [persistence_1_0.xsd] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:177)
at org.springframework.orm.jpa.PersistenceUnitReader.validateResource(PersistenceUnitReader.java:281)
at org.springframework.orm.jpa.PersistenceUnitReader.readPersistenceUnitInfos(PersistenceUnitReader.java:108)
... 57 more
I am struggling with this since a while... Do anyone have an idea what am i missing?

You need to include appropriate jar file containing the persistence_1_0.xsd.
This is explained here.
what is the use of xsi:schemaLocation?

There was a type in the url to the .xsd file. Try using: http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd . A good way to debug these types of issues is to try to hit the url in your browser, a successful request denotes a valid link.
<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" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
You make sure your project name does not contain any spaces, nor should the path to the project. This creates an error in Hibernate entity manager.

Related

Quarkus not discovering #Entity entities for persistence.xml based configuration

I'm running into an issue where I can't get Quarkus to work with persistence.xml.
My entities are not discovered so I always get "Not an entity" errors when querying.
(Caused by: java.lang.IllegalArgumentException: Not an entity: ...)
Using Quarkus 1.13.5.Final
This might be related to Quarkus Panache Not Working with persistence.xml after 1.8
I'm migrating an existing app, I only have a single project that contains all the entities. I only use a single persistence unit.
My persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="registry-pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.jdbc.time_zone" value="UTC"/>
</properties>
</persistence-unit>
</persistence>
I'm only specifying these additional env variables:
QUARKUS_DATASOURCE_USERNAME=
QUARKUS_DATASOURCE_PASSWORD=
QUARKUS_DATASOURCE_JDBC_URL=
I'm also only relying on this persistence.xml because the app being migrated needs the enable_lazy_load_no_trans prop to work
Any help would be appreciated
Found I had to add <class>EntityClass</class> entries to the <persistence-unit> tag for each of my entities to have them included. Otherwise you may be able to try <exclude-unlisted-classes>false</exclude-unlisted-classes>

Settng the schema of a Hibernate entity extra to the default schema configuration

I have a Hibernate / Spring 4 application, where the default schema of the entity classes is already configured, e.g. they end up mapped as testschema.table or prodschema.table.
However there is another entity class which needs its own configurable schema, e.g. only this entity must be mapped to testschema2.anothertable or prodschema2.anothertable.
Nice would be something like this:
#Entity
#Table(name="anothertable", schema = "${db.AntherEntitySchema}")
public class AnotherEntity {
// ..
}
where the schema gets injected from the properties file, but such a feature seems only to work with the #Value annotation.
Any idea how to proceed?
We solved it by this applicationContextPersistence.ctx.xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence-${env}.xml" />
<!-- .. -->
</bean>
where Spring can substitute the ${} expression from a property file.
We then have a persistence-dev.xml etc which features a specific orm-dev.xml:
<?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="fooUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>META-INF/orm-dev.xml</mapping-file>
</persistence-unit>
<!-- .. -->
</persistence>
The orm-dev.xml is now responsible for the entity mapping:
<?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_1_0.xsd"
version="1.0">
<description> XML Mapping file</description>
<package>foo.server.model</package>
<entity class="AnotherEntity">
<table schema="testschema2" name="anothertable" />
<attributes>
<!-- .. -->
</attributes>
</entity>
</entity-mappings>
Finally we removed the mapping annotations from the AnotherEntity POJO, as this one is now mapped via the orm-dev.xml file. The other enitity classes kept their annotations.
Note: We use the Spring Tool Suite flavour of Eclipse. This IDE expects a persistence.xml, so to get rid of an error message we kept a minimal persistence.xml to not have to remember the IDE option which deactivates the validator.

HibernatePersistence javax.naming.NameNotFoundException thrown with DataSource

I am building a Tomcat Servlet application using Hibernate with a jta data source. My persistence.xml has the following content:
<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="metadata.model" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/BitronixJTADataSource</jta-data-source>
...
</persistence>
In my ${web-app}/WebContent/META-INF/Context.xml, I have the following content:
<Context>
<Resource name="jdbc/BitronixJTADataSource" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="mysecretpassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/metadatadb" />
</Context>
Right after the application is started, I issue:
emf = Persistence.createEntityManagerFactory("metadata.model");
entityManager = emf.createEntityManager();
and get a
javax.naming.NameNotFoundException: Name [jdbc/BitronixJTADataSource] is not bound in this Context. Unable to find [jdbc].
Do you have any idea why this is occuring?
It appears that in the persistence.xml, the jta-data-source was supposed to have the java:comp/env/jdbc/BitronixJTADataSource value in order for it to be found in the InitialContext.

Tomcat 7 - Spring 3.2.1 - OpenJPA No persistent class is specified in eager initialization mode

I'm trying to get a simple web app called Debugger running under Tomcat 7 using Spring 3.2.1 and OpenJPA. I use Eclipse as the IDE and run Tomcat external to the IDE. I'm getting a error when the WAR is being deployed. This is the error message:
org.apache.openjpa.persistence.ArgumentException: No persistent class is specified in eager initialization mode.
Here is the 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"
version="2.0">
<persistence-unit name="applicationDB" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/>
<property name="openjpa.InitializeEagerly" value="true"/>
<property name="openjpa.DynamicEnhancementAgent" value="false"/>
</properties>
</persistence-unit>
</persistence>
Is the error caused by not having any classes specified in this file? I'm just trying to get a base application configuration setup so I'm not yet ready to place any classes in the persistence file. Maybe you have to have at least one?
Either list your persistent classes, or remove the openjpa.InitializeEagerly property.

Webapp cannot find Persistence Unit. No persistence unit named 'X' is available in scope

I am trying to run my webapp on my weblogic 12c server using EJB 3.1, JPA 2.0, M2E-WTP, and JSF 2.1 technologies and continue to get the following error:
javax.ejb.EJBException: EJB
Exception: : java.lang.IllegalArgumentException: No persistence unit named 'X'
is available in scope Webapp. Available persistence units: [] at
weblogic.persistence.ModulePersistenceUnitRegistry
.getPersistenceUnit(ModulePersistenceUnitRegistry.java:130) at
weblogic.persistence.BasePersistenceContextProxyImpl
.<init>(BasePersistenceContextProxyImpl.java:40) at
weblogic.persistence.TransactionalEntityManagerProxyImpl
.<init>(TransactionalEntityManagerProxyImpl.java:31) at
weblogic.persistence.EntityManagerInvocationHandlerFactory.
createTransactionalEntityManagerInvocationHandler
(EntityManagerInvocationHandlerFactory.java:20) at
weblogic.persistence.PersistenceManagerObjectFactory
.createPersistenceContextProxy(PersistenceManagerObjectFactory.java:66) at
weblogic.persistence.PersistenceManagerObjectFactory
.getObjectInstance(PersistenceManagerObjectFactory.java:31) at
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
at weblogic.jndi.internal.WLEventContextImpl.lookup(WLEventContextImpl.java:251) at
weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:406) at
weblogic.j2eeclient.java.ClientReadOnlyContextWrapper.lookup
This is the EJBBean that is triggering the exception
#Stateless
public class QuoteSessionEJB implements QuoteSessionEJBLocal {
/**
* Default constructor.
*/
public QuoteSessionEJB() {
// TODO Auto-generated constructor stub
}
#PersistenceContext(unitName="X")
private EntityManager em;
/**
* Returns a list of Quote objects in the database
* #return List<Customer>
*/
public List<Quote> getQuote() {
Query query = em.createNamedQuery("findQuotes");
return query.getResultList();
}
}
Here is the persistence.xml file which is located under src/main/java --> META-INF which eclipse built.
<?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="X" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>localJTA</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-server" value="WebLogic"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
I have spent hours searching the web for answers with no avail. I don't understand why the persistence unit X in my persistence.xml cannot be located...
I'm hoping someone has ran into this same problem or know hows to fix it.
Any help is greatly appreciated!
Update: Per better_use_mkstemp comment below I moved the META-INF folder containing my persistence.xml from src/main/java to WEB-INF/classes. I have to create the classes folder in eclipse. This worked! Much appreciated!
Following up question: Doing some more research I found that putting the META-INF folder in src/main/resources would also solve the problem, however it did not. I dont know if this is an issue with Maven's M2E-WTP eclipse plugin or I am simply misunderstanding something here. Below is a screen shot of the folder structure.
By browsing the war file the maven install command creates, I can see that the META-INF folder IS correctly placed in the WEB-INF/classes folder. But simply publishing to the server within Eclipse does not work. Unless I manually create the classes folder under WEB-INF and manually drop/move the META-INF folder in there I can't get it to work inside the IDE. Also it doesn't seem to make sense to have a copy of the META-INF folder in two places as a solution.
try to replace in your persistence.xml:
<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">
for:
<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">

Resources