-1

i'm new in the world of java-ee. I try to implement an java-ee App. I'm having some trouble to configure the persistence.xml. I want the default java h2:mem to be my persistence unit. Hier is my 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="customerDatabase">
        <description>My Customer-DB</description>
    </persistence-unit>
    <datasource jndi-name="java:jboss/datasources/ExampleDS"
        pool-name="ExampleDS" enabled="true" use-java-context="true">
        <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
        <driver>h2</driver>
        <security>
            <user-name>sa</user-name>
            <password>sa</password>
        </security>
    </datasource>
    <drivers>
        <driver name="h2" module="com.h2database.h2">
            <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
        </driver>
    </drivers>
</persistence>

It's something wrong with it because i'm getting the error:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'datasource'. One of '{"http://java.sun.com/xml/ns/persistence":persistence-unit}' 
 is expected.

what does the persistence.xml miss?

I changed my provider and i'm now using hibernate... Now i'm getting errors from an other order. Can anybody helps?

11:21:46,092 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 59) MSC000001: Failed to start service jboss.persistenceunit."javaee7-master.war#customerDatabase": org.jboss.msc.service.StartException in service jboss.persistenceunit."javaee7-master.war#customerDatabase": javax.persistence.PersistenceException: [PersistenceUnit: customerDatabase] Unable to build Hibernate SessionFactory
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:172)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:665)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: customerDatabase] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
    at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154)
    ... 7 more
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: de.ostfalia.entity.Customer
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
    ... 12 more

11:21:46,098 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "javaee7-master.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"javaee7-master.war#customerDatabase\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"javaee7-master.war#customerDatabase\": javax.persistence.PersistenceException: [PersistenceUnit: customerDatabase] Unable to build Hibernate SessionFactory
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: customerDatabase] Unable to build Hibernate SessionFactory
    Caused by: org.hibernate.AnnotationException: No identifier specified for entity: de.ostfalia.entity.Customer"}}
11:21:46,197 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "postgresql-9.4-1203.jdbc42.jar" (runtime-name : "postgresql-9.4-1203.jdbc42.jar")
11:21:46,198 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "javaee7-master.war" (runtime-name : "javaee7-master.war")
11:21:46,200 INFO  [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: Service status report
WFLYCTL0186:   Services which failed to start:      service jboss.persistenceunit."javaee7-master.war#customerDatabase": org.jboss.msc.service.StartException in service jboss.persistenceunit."javaee7-master.war#customerDatabase": javax.persistence.PersistenceException: [PersistenceUnit: customerDatabase] Unable to build Hibernate SessionFactory
Lycone
  • 650
  • 9
  • 18
  • persistence.xml doesn't "miss" anything. You miss reading what it is supposed to contain. That XSD at the top of your file tells you what you can put there http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd – Neil Stockton Mar 23 '16 at 07:10
  • Caused by: javax.persistence.PersistenceException: WFLYJPA0057: PersistenceProvider ' org.eclipse.persistence.jpa.PersistenceProvider – Lycone Mar 24 '16 at 06:41
  • and what does that have to do with anything in the "question"? – Neil Stockton Mar 24 '16 at 07:08
  • Nothing... I posted at the wrong place. It was for the suggestion below. sorry – Lycone Mar 25 '16 at 08:33

1 Answers1

1

According to the official schema, the <persistence> element can only contain persistence-unit elements. I would suggest you start with upgrading from JPA 1.0 to 2.0 (requires Java 6) or 2.1 (requires Java 7). An example persistence.xml 2.1 would be as follows:

<?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="customerDatabase" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>java:jboss/datasources/ExampleDS</non-jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.password" value="sa"/>
    </properties>
  </persistence-unit>
</persistence>

This configuration presumes your application gets the java:jboss/datasources/ExampleDS data source from some other configuration, like for example in Apache Tomcat, it would be in the context.xml and the properties listed in this example would not be necessary.
The provider is different if you're not using the EclipseLink library.
Also non-jta-data-source might have to be jta-data-source in your case.

update:

The error you are getting now comes from No identifier specified for entity: de.ostfalia.entity.Customer. You need to make sure all your entity classes have properly specified Id columns. If you are using JPA annotations, that would be the @Id annotation. You may also need a @GeneratedValue on it, if the database automatically assigns the ID on insert for that table (the most common case). There is also the option of specifying them through a mapping.xml file. These tutorials will get you started with good examples http://www.tutorialspoint.com/jpa/jpa_orm_components.htm http://www.javaworld.com/article/2077817/java-se/understanding-jpa-part-1-the-object-oriented-paradigm-of-data-persistence.html

One thing I learned the hard way is that every time you do EntityManager em = entityManagerFactory.createEntityManager(); you must em.close() it in the end, no matter if you've committed or rolled-back the transaction, which means you must also catch any Throwables that may occur. In our webapp, we do this by using a javax.servlet.Filter (only applies to webapps) for all urls. If you leave it open for too long with no activity, the database will eventually drop the connection (which might not happen in H2 specifically) and the thread becomes unusable until you restart the server.

coladict
  • 4,799
  • 1
  • 16
  • 27
  • Now it doesn't find the "org.eclipse.persistence.jpa.PersistenceProvider" where do i set it up? – Lycone Mar 24 '16 at 06:39
  • Depends on what persistence library you're using, if you're using one at all. It doesn't come standard with Java runtime. How you set it up depends on your build system. I only know how it's done with Maven, but if you're developing under Netbeans, in most cases you can use the dependency menu to add them graphically. – coladict Mar 24 '16 at 07:07
  • I also use maven. But i read on the internet, that i'm just missing the Eclipse implementation of PersistenceProvider... I'll try to download the jar and see what next. – Lycone Mar 25 '16 at 08:29
  • EclipseLink is supposedly the reference implementation, but I recommend Hibernate for just one extra that it supports: named parameters in native queries. That is not required, but also not forbidden by the JPA 2.1 standard. – coladict Mar 25 '16 at 09:40