How can I use derby in memory with jpa2? - eclipselink

Regardless of the following persistence.xml configuration the database is persisted on disk.
<persistence-unit name="com.mysimpatico_inmemory_persistence_nbm_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.mysimpatico.memoplatform.persistence.entities.Expression</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:tempDb;create=true"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
</properties>
http://wiki.apache.org/db-derby/InMemoryBackEndPrimer

I'm using Derby in memory for testing and my configuration is very close to yours.
Here 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_2_0.xsd" version="2.0">
<persistence-unit name="TestPu" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.acme.Foo</class>
<class>com.acme.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Common properties -->
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:test-jpa;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<!-- EclipseLink specific properties -->
<property name="eclipselink.target-database" value="Derby"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.debug" value="ALL"/>
<property name="eclipselink.weaving" value="static"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.logging.level.sql" value="FINEST"/>
<property name="eclipselink.logging.level.cache" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
Where org.apache.derby:derby:jar:10.6.2.1 is on the classpath.
Now two questions/suggestions:
How do you know the database is persisted on disk?
If it really does, make sure you're using the persistence.xml you think you are.

Add the following to your maven configuration
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.2.GA</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.5.3.0</version>
<scope>test</scope>
</dependency>
The following persistance.xml works pretty well with hibernate.
<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>myapp.model.entities.Group</class>
<class>myapp.model.entities.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.url" value="jdbc:derby:memory:jpa"/>
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
</properties>
</persistence-unit>

Related

Empty MetaModel when using EclipseLink 2.7.4

My application has a composite persistence unit mainPU which contains 2 members (queryPU and entityPU). When using Criteria API in EclipseLink 2.5, it did not have any problem. However, when upgrading to EclipseLink 2.7, it got error "No [EntityType] was found for the key class [XXX] in the Metamodel" where XXX is the class defined in entityPU
I have some observation regarding the issue:
This problem only occurred when using Criteria API, and there is no
problem when using JPQL.
By default, my application load the mainPU. If I try to get the meta model, it always return empty list. If I explicitly load the
EntityManagerFactory with entityPU in a DAO, then it can load the full list of EntityType, and the problem gone!
EntityManagerFactory factory = Persistence.createEntityManagerFactory("entityPU");
Metamodel m2 = factory.getMetamodel(); //m2 return a list of EntityType!
Is there any wrong configuration in my application for the EclipseLink 2.7?
Below show the persistence.xml
mainPU
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="mainPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/appds</jta-data-source>
<jar-file>icms-entity-lib-0.2.9.544.jar</jar-file>
<jar-file>../icmshrnmc-ejb.jar</jar-file>
<properties>
<property name="jboss.entity.manager.jndi.name" value="java:jboss/persistence/mainPU" />
<property name="eclipselink.target-database"
value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform" />
<property name="eclipselink.target-server" value="JBoss" />
<property name="eclipselink.composite-unit" value="true" />
<property name="eclipselink.session.customizer"
value="x.y.ejb.dao.entity.DefaultEntityInterceptor" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.deploy-on-startup" value="true"/>
</properties>
</persistence-unit>
queryPU
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="queryPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/appds</jta-data-source>
<mapping-file>x/y/icmshrnmc/model/common/dao/namedquery/HrnNat.orm.xml</mapping-file>
...
...
...
<properties>
<property name="eclipselink.composite-unit-member" value="true" />
</properties>
</persistence-unit>
entityPU
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="entityPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/appds</jta-data-source>
<non-jta-data-source>java:jboss/datasources/appds</non-jta-data-source>
<mapping-file>x/y/fmk/model/application/dao/entity/AuditEntity.orm.xml</mapping-file>
...
...
...
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="eclipselink.jdbc.sequence-connection-pool" value="true" />
<property name="eclipselink.jdbc.sequence-connection-pool.non-jta-data-source" value="java:jboss/datasources/fmkseqds" />
</properties>

FOR XML PATH. Nesting with embedded select statement

When using the following SQL From a single temp Table.
Select DataType as '#Datatype',
(Select
'Order' as [Property/#name],
Order1 as Property,
null,
'ExtCode' as [Property/#name],
ExtCode as Property,
null
from #TempLine
for xml path('Properties'), type
)
from #TempLine
Group by DataType
for xml path('Object'), root('Objects')
I am getting the following XML
<Objects>
<Object Datatype="ItemInfo">
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#ISSMS</Property>
</Properties>
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#IMF</Property>
</Properties>
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#ILZF</Property>
</Properties>
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#L5898.5W</Property>
</Properties>
</Object>
</Objects>
I Need to return the following XML with Object surrounding Properties.
<Objects>
<Object Datatype="ItemInfo">
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#ISSMS</Property>
</Properties>
</Object>
<Object Datatype="ItemInfo">
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#IMF</Property>
</Properties>
</Object>
<Object Datatype="ItemInfo">
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#ILZF</Property>
</Properties>
</Object>
<Object Datatype="ItemInfo">
<Properties>
<Property name="Order">150825CREC004</Property>
<Property name="ExtCode">150825CREC004#L5898.5W</Property>
</Properties>
</Object>
</Objects>
I thought I could do it with embedded select but its not working out. I need to be able to have the object surround each of the properties and not the whole block.

Wildfly 8.2.0.Final: Misconfiguration because of example datasource or persistence.xml?

I have three datasources configured in my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
<persistence-unit name="MyPersistenceTestUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="hibernate.connection.username" value="myuser" />
<property name="hibernate.connection.password" value="mypass" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
<persistence-unit name="MyLoggingUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="hibernate.connection.username" value="myuser" />
<property name="hibernate.connection.password" value="mypass" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
</persistence>
The first is the 'default' unit. It's used by the standard entity manager. The second is needed for running JUnit tests outside of a Java EE environment. The third one is used for logging to be independent from JTA transactions.
This works. But when I start Wildfly, I even get some of these errors (although I don't use H2 in my application):
21:32:33,748 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 51) HHH000319: Could not get database metadata: org.h2.jdbc.JdbcSQLException: Table "PG_CLASS" not found; SQL statement:
select relname from pg_class where relkind='S' [42102-173]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
It only happens when the application is deployed.
The idea was to disable or remove the ExampleDS by admin console or in standalone.xml, but when I do that, I get a startup with errors:
21:36:20,992 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "com.zonacroft.mycuisine.webserver-TRUNK.war")]) - failure description: {"JBAS014771: Services with missing/unavailable dependencies" => [
"jboss.persistenceunit.\"com.mydomain.myapp.webserver-TRUNK.war#MyLoggingUnit\".__FIRST_PHASE__ is missing [jboss.data-source.java:jboss/datasources/ExampleDS]",
"jboss.persistenceunit.\"com.mydomain.myapp.webserver-TRUNK.war#MyPersistenceTestUnit\".__FIRST_PHASE__ is missing [jboss.data-source.java:jboss/datasources/ExampleDS]"
What on earth is going on here? Why does this persistence.xml need the exampleDS? Are the RESOURCE_LOCAL persistence units maybe wrong configured? (But if so, why do they work then). So what's the problem here?
[UPDATE]
I figured out that I don't get startup errors (with exampleDS enabled) when I remove the hibernate.hbm2ddl.auto properties from the RESOURCE_LOCAL persistence units. But it annoys me that I have to let the exampleDS of Wildfly enabled. Otherwise Wildfly would not start with the application as described above. Why is this so. What's wrong here?
You only define a datasource for the first persistence unit:
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
That means WildFly uses the default datasource exampleDS for your other persistence units.
This causes the exception, since the configured PostgreSQL dialect does not work for the default H2 datasource.

Error on persistence.xml configuring HSQLDB with Hibernate

I am configuring one HSQLDB database inside the application folder, follow below the 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="crmUnity" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.crm.model.entities.Cliente</class>
<class>br.com.crm.model.entities.Contato</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:file://database/crm;shutdown=true" />
<property name="hibernate.connection.user" value="sa" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
</properties>
</persistence-unit>
</persistence>
The JBOSS 7.1.3 while starting show the error on log:
18:40:10,477 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 170) HHH000319: Could not get database metadata: java.sql.SQLException: No suitable driver found for jdbc:hsqldb:file://database/crm;shutdown=true
Can anybody help me about configure the jdbc url to resolve this error?
Thank's, TarcĂ­sio.

NHibernate loading error message

I am having the error message with MappingNHibernateException:
{"Could not compile the mapping document: Infrastructure.DataAccess.Mappings.Post.hbm.xml"}
Could not find the dialect in the configuration
on the part
Configuration configuration = new Configuration()
.AddAssembly("Infrastructure");
_sessionFactory = configuration.BuildSessionFactory();
What is wrong?
hibernate.cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver </property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.connection_string">Server=localhost\SQLServer2005;database=NHibernate101;Integrated Security=True;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Infrastructure"></mapping>
</session-factory>
</hibernate-configuration>
Try this. In app.config file:
<configuration>
<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
</configSections>
<!-- Replace with your values -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Data Source=data.db3;Version=3</property>
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver, NHibernate</property>
<property name="show_sql">true</property>
<property name="adonet.batch_size">0</property>
<property name="default_batch_fetch_size">0</property>
<mapping assembly="Infrastructure" />
</session-factory>
</hibernate-configuration>
</configuration>
And in your code:
var cfg = new Configuration().Configure();
var factory = cfg.BuildSessionFactory();