EJB2.x container managed relationship - ejb-2.x

I am a total novice to EJB technologies, so recently I started learning EJB3.0, when reading through books/online resources, I found lot of references and comparisons to EJB2.0 and how EJB3.0 simplifies things by requiring developer to create fever components. I left my reading of EJB3.0 and started reading EJB2.0. I started with book "Enterprise Java Beans by Richard Monson-Haefel III Edition", its pretty old edition (2000) but is very comprehensive. I understood the things pretty well until container managed relationship. I found that through abstract persistence model (abstract accessor methods) and abstract schema (deployment descriptor), a relationship is established between two entity beans. Also I found that for persistence fields of entity bean, at the time of deployment a mapping is established between virtual persistent field and actual database table column. When do such mapping is established for virtual relationship field, is it at the time of deployment only?
Uni-directional one to one
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>CustomerEJB</ejb-name>
<home>com.titan.customer.CustomerHomeRemote</home>
<remote>com.titan.customer.CustomerRemote</remote>
<ejb-class>com.titan.customer.CustomerBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Customer</abstract-schema-name>
<cmp-field><field-name>id</field-name></cmp-field>
<cmp-field><field-name>lastName</field-name></cmp-field>
<cmp-field><field-name>firstName</field-name></cmp-field>
<primkey-field>id</primkey-field>
<security-identity><use-caller-identity/></security-identity>
</entity>
<entity>
<ejb-name>AddressEJB</ejb-name>
<local-home>com.titan.address.AddressHomeLocal</local-home>
<local>com.titan.address.AddressLocal</local>
<ejb-class>com.titan.address.AddressBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Address</abstract-schema-name>
<cmp-field><field-name>id</field-name></cmp-field>
<cmp-field><field-name>street</field-name></cmp-field>
<cmp-field><field-name>city</field-name></cmp-field>
<cmp-field><field-name>state</field-name></cmp-field>
<cmp-field><field-name>zip</field-name></cmp-field>
<primkey-field>id</primkey-field>
<security-identity><use-caller-identity/></security-identity>
</entity>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Customer-Address</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>Customer-has-an-Address</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>CustomerEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>homeAddress</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>Address-belongs-to-Customer</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>AddressEJB</ejb-name>
</relationship-role-source>
</ejb-relationship-role>
</ejb-relation>
</relationships>
</ejb-jar>
In Unidirectional relationship Customer EJB has relationship field homeAddress. In the database table CUSTOMER has a column ADDRESS_ID. When do the mapping between homeAddress & ADDRESS_ID is established.
Also for bi-directional relationship fields where both relationship roles have element defined in deployement descriptor but in the actual database table only one table holds the foreign key (in case of many-one and one-may), do we need to map from other relationship role to any column?
Bi-directional one-to-one
<relationships>
<ejb-relation>
<ejb-relation-name>Customer-CreditCard</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>Customer-has-a-CreditCard</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>CustomerEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>creditCard</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>CreditCard-belongs-to-Customer</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>CreditCardEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>customer</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>
Customer EJB has relationship field creditCard, While CreditCard EJB has relationship field customer. In database CUSTOMER table has column CREDITCARD_ID while CREDIT_CARD table has column CUSTOMER_ID. Is it required for CUSTOMER table to have a CREDITCARD_ID column. Is it always necessary for abstract schema to map exactly to the database schema? Is this relationship established at deployment time?

Yes, the mapping from CMP metadata to DB tables happens at deploy time using vendor-specific tools. From the EJB 3.0 specification:
The EJB deployment descriptor for EJB 2.1 entity beans describes
logical relationships among entity beans. It does not provide a
mechanism for specifying how the abstract persistence schema of an
entity bean or of a set of interrelated entity beans is to be mapped
to an underlying database. This is the responsibility of the Deployer,
who, using the Container Provider’s tools, uses the logical
relationships that are specified in the deployment descriptor to map
to the physical relationships that are specific to the underlying
resource.

Related

Spring batch tables creation fails in MariaDB

I used this schema to create Spring batch tables in MariaDB - https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/resources/org/springframework/batch/core/schema-mysql.sql.
BATCH_JOB_EXECUTION_PARAMS table fails with below error
Error: (conn=10719030) This table type requires a primary key
SQLState: 42000
ErrorCode: 1173
Add PRIMARY KEY(JOB_EXECUTION_ID, KEY_NAME) to BATCH_JOB_EXECUTION_PARAMS if that combination is Unique.
BATCH_JOB_EXECUTION_SEQ also has no PK. The UNIQUE key could be promoted to be the PK. (Ditto for some other tables.) That particular table is rather weird -- it turns a 1-byte UNIQUE_KEY into an 8-byte id!?!
BATCH_JOB_EXECUTION_PARAMS is a pretty awful variant of the classic EAV schema.
MySQL and MariaDB are different products and it looks like they behave differently in regards to primary keys. You are using the MySQL DDL script against a MariaDB server which is not officially supported by Spring Batch.
So either adapt the script accordingly (by adding the primary keys manually) and be aware that Spring Batch would not necessarily work as expected since it does not support MariaDB officially, or open a feature request in the JIRA of the project to request support for MariaDB.

v1.0 - commonName in a legalName to create node

If I uses a commonName in a legalName to create a node (at least for MockNetwork), then networkMapCache.getNodeByLegalName(legalName) returns null. After removing the commonName, it works. Is that by design?
This is a bug in Corda V1.0. When creating a node, the common name part of the name is nulled out. However, when looking up a node using the network map, the common name field is not nulled out, so no match is found.
The Corda team will backport a fix to Corda V1.0 to throw an exception when a regular node is created with a common name. In Corda V1.0, a common name is used solely by notary clusters to identify one another.
In a future version of Corda, the Corda team will examine re-enabling the common name field for regular nodes.

looking for a strategy to avoid cyclic dependency in PostgreSQL schemas

In my db, I have schema a with entities that depend upon schema b, and schema b has entities that depend on schema a. This came about over a year of incremental development. Now, migrating the db to another server is failing. When I pgdump a schema from one server and try to pg_restore it on the other server, it croaks. What can I do to get around, both for now, and as a good strategy for the future?

Caching read-only entity beans in EJB 3.1

How do I go about marking an entity bean (let us say that I have an "Country" entity bean which holds a record about a country that never changes) read-only on a Glassfish 3.1 with EJBs 3.1?
There seems to be a way to mark column immutable, but information is very scarce and whatever I tried didn't really work.
Any ideas?
Using EclipseLink you can use the JPA exensions of EclipseLink and put the #ReadOnly on the entity. For query you can add the query hint READ_ONLY to the query:
query.setHint(QueryHints.READ_ONLY, HintValues.TRUE);
For details on this, see http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Using_EclipseLink_JPA_Extensions_for_Declaration_of_Read-Only_Classes
and
http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Read_Only

How to persist/flush doctrine2 entities with OneToMany // ManyToOne relations?

I am trying to persist/flush my doctrine2 entities, but I am gettig an error every time:
Entity of type Test\Main\MainBundle\Entity\ProductVariantAssociation has identity
through a foreign entity Test\Main\MainBundle\Entity\Product, however this entity has
no ientity itself. You have to call EntityManager#persist() on the related entity and make
sure it an identifier was generated before trying to persist
'Test\Main\MainBundle\Entity\ProductVariantAssociation'. In case of Post Insert ID
Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call
EntityManager#flush() between both persist operations.
I posted my ArticleController at pastebin:
http://pastebin.com/iN0BpGFc
Does anybody know, how to solve that problem?

Resources