HSQLDB und JPA Anbindung

Hallo,

ich wollte mich mal an einer Lokalen Datenbank mit HSQLDB versuchen.

Habe also in der Java-EE Umgebung ein JPA Projekt erstellt.

Diese hat lediglich eine Entity

Ich weiß nicht ob ich auf dem richtigen Weg bin und wäre für jegliche Hilfe dankbar.


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {

	@Id
	@GeneratedValue
	private int id;

	private String name;

	private String steamId;

	public User(int id, String name, String steamId) {
		super();
		this.id = id;
		this.name = name;
		this.steamId = steamId;
	}

	public User() {

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSteamId() {
		return steamId;
	}

	public void setSteamId(String steamId) {
		this.steamId = steamId;
	}

}

So sieht meine main-Method aus

		EntityManagerFactory factory = Persistence.createEntityManagerFactory("Zuy",System.getProperties());
		EntityManager em = factory.createEntityManager();
		
		em.getTransaction().begin();
		
		em.persist(new User(0,"Bob","STEAM_ID:8888"));
		
		em.getTransaction().commit();
		
		em.close();
		```

Und so meine xml

[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="Zuy" transaction-type="RESOURCE_LOCAL">
		<class>data.User</class>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
			<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:MyDB"/>
			<property name="javax.persistence.jdbc.user" value="SA"/>
			<property name=""/>
		</properties>
	</persistence-unit>
</persistence>
[/XML]

Errorlog

[SPOILER]
> 32  hellojpa  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.1.0
> 78  hellojpa  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.HSQLDictionary".
> Exception in thread "main" <openjpa-2.1.0-r422266:1071316 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: <openjpa-2.1.0-r422266:1071316 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "org.hsqldb.jdbcDriver" and URL "jdbc:hsqldb:hsql://localhost/".  You may have specified an invalid URL.
> 	at org.apache.openjpa.jdbc.schema.DataSourceFactory.newConnectException(DataSourceFactory.java:261)
> 	at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:247)
> 	at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getConnectionFactory(JDBCConfigurationImpl.java:733)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> 	at java.lang.reflect.Method.invoke(Method.java:497)
> 	at org.apache.openjpa.lib.conf.ConfigurationImpl.instantiateAll(ConfigurationImpl.java:295)
> 	at org.apache.openjpa.conf.OpenJPAConfigurationImpl.instantiateAll(OpenJPAConfigurationImpl.java:1652)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:645)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:204)
> 	at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:154)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:60)
> 	at hellojpa.Main.main(Main.java:43)
> Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Connection is broken: java.io.EOFException)
> 	at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
> 	at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
> 	at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
> 	at org.apache.openjpa.jdbc.schema.DBCPDriverDataSource.getDBCPConnection(DBCPDriverDataSource.java:89)
> 	at org.apache.openjpa.jdbc.schema.AutoDriverDataSource.getConnection(AutoDriverDataSource.java:58)
> 	at org.apache.openjpa.jdbc.schema.SimpleDriverDataSource.getConnection(SimpleDriverDataSource.java:88)
> 	at org.apache.openjpa.lib.jdbc.DelegatingDataSource.getConnection(DelegatingDataSource.java:139)
> 	at org.apache.openjpa.lib.jdbc.DecoratingDataSource.getConnection(DecoratingDataSource.java:112)
> 	at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:239)
> 	... 14 more
> Caused by: java.sql.SQLException: Connection is broken: java.io.EOFException
> 	at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
> 	at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
> 	at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
> 	at org.hsqldb.jdbcDriver.connect(Unknown Source)
> 	at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
> 	at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
> 	at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
> 	at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
> 	... 22 more
> 
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:219)
> 	at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:154)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:60)
> 	at hellojpa.Main.main(Main.java:43)
> Caused by: java.lang.RuntimeException: There were errors initializing your configuration: <openjpa-2.1.0-r422266:1071316 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "org.hsqldb.jdbcDriver" and URL "jdbc:hsqldb:hsql://localhost/".  You may have specified an invalid URL.
> 	at org.apache.openjpa.jdbc.schema.DataSourceFactory.newConnectException(DataSourceFactory.java:261)
> 	at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:247)
> 	at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getConnectionFactory(JDBCConfigurationImpl.java:733)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> 	at java.lang.reflect.Method.invoke(Method.java:497)
> 	at org.apache.openjpa.lib.conf.ConfigurationImpl.instantiateAll(ConfigurationImpl.java:295)
> 	at org.apache.openjpa.conf.OpenJPAConfigurationImpl.instantiateAll(OpenJPAConfigurationImpl.java:1652)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:645)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:204)
> 	at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:154)
> 	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:60)
> 	at hellojpa.Main.main(Main.java:43)
> Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Connection is broken: java.io.EOFException)
> 	at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
> 	at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
> 	at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
> 	at org.apache.openjpa.jdbc.schema.DBCPDriverDataSource.getDBCPConnection(DBCPDriverDataSource.java:89)
> 	at org.apache.openjpa.jdbc.schema.AutoDriverDataSource.getConnection(AutoDriverDataSource.java:58)
> 	at org.apache.openjpa.jdbc.schema.SimpleDriverDataSource.getConnection(SimpleDriverDataSource.java:88)
> 	at org.apache.openjpa.lib.jdbc.DelegatingDataSource.getConnection(DelegatingDataSource.java:139)
> 	at org.apache.openjpa.lib.jdbc.DecoratingDataSource.getConnection(DecoratingDataSource.java:112)
> 	at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:239)
> 	... 14 more
> Caused by: java.sql.SQLException: Connection is broken: java.io.EOFException
> 	at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
> 	at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
> 	at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
> 	at org.hsqldb.jdbcDriver.connect(Unknown Source)
> 	at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
> 	at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
> 	at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
> 	at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
> 	... 22 more
> 
> 	at org.apache.openjpa.lib.conf.ConfigurationImpl.instantiateAll(ConfigurationImpl.java:309)
> 	at org.apache.openjpa.conf.OpenJPAConfigurationImpl.instantiateAll(OpenJPAConfigurationImpl.java:1652)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:645)
> 	at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:204)
> 	... 5 more

[/SPOILER]

nach welchen Tutorial richtest du dich zu property “javax.persistence.jdbc.url” usw.?
danach gesucht gibt es ja manche Varianten, evtl. verschiedene ausprobieren

unter Chapter*13.*Properties
steht bei ‘jdbc:hsqldb:mem:’

The old form for the URL, jdbc:hsqldb:. creates or connects to the same database as the new form for the URL, jdbc:hsqldb:mem:.

das klingt zwar danach, dass das Alte auch noch funktionieren sollte, also nicht unbedingt eine Lösung, liegt vielleicht an ganz was anderem,
aber zumindest auch, dass du etwas Altes verwendest, also kleine Hoffnung dass Neueres geht

und wenn Username angegeben, wie sieht es mit Passwort aus?
ein leerer Property dagegen etwas unschön


hast du zufällig auch Klarheit darüber welche Art HSQLDB installiert, welche Version usw.?
ist ein ‘jdbc.driver’ wirklich vorhanden, welche Version/ Quelle?

kannst du mit anderen Standardtools auf die HSQLDB zugreifen oder mit JDBC in Java-Befehlen ohne XML?
edit: hier Appendix B. First JDBC Client Example zu Version 1.8 auch noch mit alter URL

                                         + db_file_name_prefix,    // filenames
                                         "sa",                     // username
                                         "");                      // password```

nicht dass ich da besonders viel zu wüßte, habe es selber glaube ich noch nicht gemacht oder wenn so lange her dass wieder vergessen,
aber vielleicht wichtige Infos

Ich stütze mich auf ein Beispielprojekt einer Dozentin aus meiner Universität.
Ich benutze selber apache-openjpa 2.4.1 und hsqldb 2.3.3.

Ich kann auf die Datenbank durch den ServerManager von hsqldb zugreifen (.bat)

Natürlich würde ich auch auf andere Libarys zugreifen wenn es “bessere” gibt.

edit:

Wenn ich keine Transaktion durchführe, dann connected er ganz normal.
Was mir stetig auffällt ist das meine Dozentin .ant files hat , sind die wirklich notwendig ?
Ist eine “einfache” implementierung von einer lokalen Datenbank wirklilch so “aufwendig”?

973 Zuy INFO [main] openjpa.Runtime - OpenJPA dynamically loaded the class enhancer. Any classes that were not enhanced at build time will be enhanced when they are loaded by the JVM.
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See SLF4J Error Codes for further details.
1007 Zuy INFO [main] openjpa.Runtime - OpenJPA dynamically loaded a validation provider.
1018 Zuy INFO [main] openjpa.Runtime - Starting OpenJPA 2.4.1
1043 Zuy INFO [main] openjpa.jdbc.JDBC - Using dictionary class “org.apache.openjpa.jdbc.sql.HSQLDictionary”.
1322 Zuy INFO [main] openjpa.jdbc.JDBC - Connected to HSQL Database Engine version 2.2 using JDBC driver HSQL Database Engine Driver version 2.3.3.

siehe evtl. auch vorherigen edit in meinem ersten Posting zu JDBC-Zugriff,
mit Link dort zwar auf ein Programm zu Version 1.8, aber wäre in jedem Fall auch interessant zu wissen wie das reagiert:
wenn URL dort akzeptiert eine Aussage, wenn nicht dann auch eine

gerade noch Chapter*1.*Running and Using HyperSQL gefunden,
dort steht
Connection c = DriverManager.getConnection("jdbc:hsqldb:http://localhost/xdb", "SA", "");

ausprobieren als Java-Programm (zusammen mit dem Rest dort davor) schadet gewiss nicht,
vielleicht klappt gar diese Langform in der “javax.persistence.jdbc.url”-property?

na, zumindest kaum im Netz zu finden…
was ist denn mal ganz konkret gefragt mit einer Konfiguration a la
hibernate - Using HSQL in-memory database as JPA datasource - Stack Overflow

      <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.password" value=""/>

(zu beachten auch password-property)
geht das, ja oder nein, gleicher Fehler oder anderer?

Die Connection geht und die password property hat auch keinen Unterschied gemacht leider. Ich schaue mir mal dein Hibernate an, danke