Support JPA eclipselnk multi-tenancy - jpa

I have a JPA application running, and now I want to support multi-tenancy. I like to use XML instead of annotations.
I have a couple of orm.xml referenced from persistence.xml.
<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 orm_2_0.xsd"
version="2.0">
<package>mypackage</package>
<entity class="Foo" />
<entity class="Bar" />
</entity-mappings>
I like to use the same multi-tenancy configuration for all entities:
single-table, discriminator column is tenantUserId, context-property is tenant.userId.
According to:
https://wiki.eclipse.org/EclipseLink/Examples/JPA/EclipseLink-ORM.XML
<tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/>
Whether to put the line above? I tried to create eclipselink-orm.xml as following
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_1.xsd"
version="2.1">
<tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/>
<persistence-unit-metadata>
<persistence-unit-defaults>
<tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
Both are invalid according to the schema. Where to put eclipselink-orm.xml?
Is there a way to say that: all entities are multi-tenant(single table)? Do I have to specify them for all entities one by one?
<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 orm_2_0.xsd"
version="2.0">
<package>mypackage</package>
<entity class="Foo" >
<multi-tenant/>
</entity>
<entity class="Bar" >
<multi-tenant/>
</entity>
</entity-mappings>
Thanks.

From http://www.eclipse.org/eclipselink/documentation/2.5/solutions/multitenancy002.htm
you are using the persistence unit default correctly as:
<persistence-unit-metadata>
<persistence-unit-defaults>
<tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/>
</persistence-unit-defaults>
</persistence-unit-metadata>
The problem is you are using the wrong schema version. 2.1 did not include multitenant features, so you need to use the 2.5 xds, eclipselink_orm_2_5.xsd. This should be in the eclipselink.jar or pulled from git as James describes here http://git.eclipse.org/c/eclipselink/eclipselink.runtime.git/tree/jpa/org.eclipse.persistence.jpa/resource/org/eclipse/persistence/jpa

Related

How can I change table names in KIE / Drools persistence?

My question is pretty straightforward. My persistence.xml looks 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="org.jbpm.domain" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/jbpm</jta-data-source>
<!-- SessionInfo -->
<class>org.drools.persistence.info.SessionInfo</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
...
</properties>
</persistence-unit>
</persistence>
And this creates SessionInfo and SESSIONINFO_ID_SEQ tables in my database. However, these names are not in our organisational standards, and I am trying to change the table names at least (and the column names if I could). How can I achieve this? Thanks!
I did it using an orm.xml file under WEB-INF/classes/META-INF, which looked like this:
<?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 orm_2_0.xsd"
version="2.0">
<entity class="org.drools.persistence.info.SessionInfo">
<table name="session_info"/>
<attributes>
<id name="id">
<column name="id"/>
<generated-value generator="session_info_id_seq" strategy="SEQUENCE"/>
<sequence-generator name="session_info_id_seq" sequence-name="session_info_id_seq"/>
</id>
<basic name="lastModificationDate">
<column name="last_modification_date"/>
</basic>
<basic name="rulesByteArray">
<column name="rules_byte_array"/>
</basic>
<basic name="startDate">
<column name="start_date"/>
</basic>
<version name="version">
<column name="optlock"/>
</version>
</attributes>
</entity>
</entity-mappings>
For further reference on XML overriding: Here's the official guide.

EclipseLink does not work on Netbeans, is this normal?

It'll be weeks that I'm stuck with EclipseLink. I can not persist an object in my database. I use netbeans 7.3. I encountered this problem when I started designing a web application. What follows is the approach I have adopted. It may be that I do without me realize a mistake.
After that netbeans has finished generating the project files I configured the jndi. Then I converted automatically with netbeans, the database tables in entity object.
here is the link
then, from these classes, I created their JPAController . (Always automatically with netbeans)
and finally, as a test, I just instantiate the description of "Outils" and leave the fields empty id. Since the latter automatically increment in the database, if the persistence is done well, I should have an id when I appear with out the console.
<body>
<h1>Hello World!</h1>
<%
Outils o = new Outils();
o.setDesignation("hammers");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test_EclipseLinkPU");
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
OutilsJpaController o_ctrl = new OutilsJpaController(utx, emf);
o_ctrl.create(o);
out.println("this is the id of hammer " + o.toString());
%>
</body>
and I get as result: Outils[ id=null ].
I have no error or on glassfish even less about the debugger.
Ps : Here are the persistence.xml file
<?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="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Thank you for your future is in your answers and listening for any additional information.
(Not really an answer but I couldn't show this in a comment)
To increase logging your persistence.xml should look like this:
<?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="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
You need to commit or flush the transaction before anything will be written to the database. If you are using IDENTITY sequencing (I strongly recommend not using IDENTITY, use TABLE or SEQUENCE if db supports it), then the Id cannot be allocated until the insert occurs, so a persist() will not assign the Id (as it does for TABLE and SEQUENCE).
What does your create() method do? How is your Id mapped?

How to read some element name from XML using GData library?

I am making an app in which I am using SOAP web service, I am receiving this data -
<?xml version="1.0" encoding="UTF-8"?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.myurl.com/">
<code>200</code>
<profile>
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<id>1</id>
<country>Afghanistan</country>
<isd_code>93</isd_code>
<timezone>+04:30</timezone>
<visible>false</visible>
</Table>
</NewDataSet>
</profile>
</User>
And I am using GData library to read this data. I am able to read <NewDataSet> element values but don't know how to read value of <code> tag. So please suggest me to do this.

How do I configure OpenJPA SQL logging?

What is the OpenJPA configuration to view SQL query executed in a database? I would like to view the query with all parameters executed in log or console instead of viewing the JPQL query
<property name="openjpa.Log" value="SQL=Trace" />
Enables logging of all SQL statements, minus parameter values.
<property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
Enables logging of SQL parameters.
Logging documentation
If you're using log4j, you can setup your log4j.properties file as follows, which will display both the native SQL query and any parameters:
log4j.rootLogger=WARN, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p %t %d{ISO8601} %l - %m%n
log4j.category.openjpa.jdbc.SQL=TRACE
To configure Open JPA for Log4J, you need to do the following in persistence xml
Open JPA Configurations
{'property name=”openJpa.Log” value=”log4j”'}
Log4j properties for Open JPA configs
log4j.logger.openjpa.Query=TRACE
log4j.logger.openjpa.jdbc.SQL=TRACE
http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/ref_guide_logging_log4j.html
Hi I want to add running persistance.xml file
<?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="EHS_PU">
<jta-data-source>mysqlDataSource</jta-data-source>
<class>com.ap.entity.EHSDo</class>
<class>com.ap.entity.EventDo</class>
<properties>
<property name="openjpa.Log" value="log4j" />
<property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
</properties>
</persistence-unit>
</persistence>
In addition to Rick's answer, there is also the "openjpa.ConnectionFactory2Properties" property for connection factories used for unmanaged connections. (more details here: https://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/ref_guide_conf_openjpa.html#openjpa.ConnectionFactory2Properties)

OpenJPA does not create DB2 tables when deploying on Websphere Application Server

I deployed my ear file on Websphere Application server V7.0 and start the application. However, it does not auto create table to my database DB2 and don't have any error message.
Please see my persitence.xml file
<?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_1_0.xsd"
version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="xcrm_ejb" >
<jta-data-source>jdbc/xcrm</jta-data-source>
<non-jta-data-source>jdbc/non_xcrm</non-jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>ch.xpertline.xcrm.entity.base.BaseEntity</class>
<class>ch.xpertline.xcrm.entity.Address</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.Log" value="DefaultLevel=INFO,SQL=TRACE,File=./dist/jpaEnhancerLog.log,Runtime=INFO,Tool=INFO"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
My orm.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0" 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">
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>soreco</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
Thanks
Try sending in an application request. I don't think synchronize mappings is triggered until the first EntityManager is created.