users@glassfish.java.net

_at_ManyToMany Using Composite Key

From: <glassfish_at_javadesktop.org>
Date: Mon, 27 Aug 2007 16:44:57 PDT

I have searched this forum concerning this issue. While I see others with a similar question, I see very little on a possible solution. Here is my problem statement.

Class 1: Document.java
Class 2: Collection.java
Class 3: CollectionType.java


XRef Table: Document_Collection

Relationships:
A DocumentID may be inside many collections.
A CollectionID may have many documents.

The owner of the relationship is document.java.

The collection table uses 3 fields for defining a Primary Key--Site, Name, and Date. The three fields are Strings.

I am using a SEQUENCE table for generating the unique Ids for each of the primary keys, except the Collection Table. This table is using a CompositePK Class for the Primary Key for the Collection Table.

Document.java:

[code]
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="DOCUMENT_COLLECTION",
                  joinColumns=_at_JoinColumn(name="DC_DOCUMENT_ID", referencedColumnName="DOCUMENT_ID"),
          inverseJoinColumns={_at_JoinColumn(name="DC_COLLECTION_ID", referencedColumnName="SITE_ID"),
                                    @JoinColumn(name="DC_COLLECTION_ID", referencedColumnName="NAME"),
                    @JoinColumn(name="DC_COLLECTION_ID", referencedColumnName="GROUP_DATE")}
        )
public Set<Collection> getCollectionSet() {
                return collectionSet;
}
[/code]

Collection.java

[code]
@ManyToMany(mappedBy="collectionSet")
public Set<Document> getDocumentSet() {
        return documentSet;
}
[/code]

In the Collection Class it holds a reference to a Collection type.

CollectionType.java

[code]
@Id
@TableGenerator(name="COLLECTION_TYPE_TBL_GEN",
                  table="SEQUENCE",
                  pkColumnName="SEQ_NAME",
                  valueColumnName="SEQ_COUNT",
                  pkColumnValue="COLLECTION_TYPE_SEQ")
@GeneratedValue(strategy=GenerationType.TABLE, generator="COLLECTION_TYPE_TBL_GEN")
@Column(name="COLLECTION_TYPE_ID", nullable=false)
public Long getCollectionTypeId() {
        return collectionTypeId;
}
[/code]

My test application builds the relationship for all the tables, and then attempts to persist the information to the database. When I look at at the log files, I see no errors, but the data is not being populated in the database. When I look at the SEQUENCE Table, I see all entries in the table are being incremented correctly except the Collection_Type sequence--still set to 1. I surmised the Collection Set for the Document_Collection table is not being populated correctly.

My intent is to let TopLink generate the PK, Ids for the objects. This appears to be happening for the one-to-one relationships but not for the ManyToMany relationships. When I build the relationships, I create the collection and CollectionType and add it to a set. This set is added to the Document class. Lastly, I call the persist method on the Entity Manager. The log file shows the sequence number is being generated except for the Collection_Type Sequence. This may not be the correct way, but I did not see the correct steps for accomplishing this operation.

[code]
[#|2007-08-27T16:52:03.899-0500|FINE|sun-appserver-pe9.0|oracle.toplink.essentials.file:/C:/glassfish/domains/domain1/applications/j2ee-apps/MyProject/MyEJB_jar/-MYEJBPU.sql|_ThreadID=12;_ThreadName=httpWorkerThread-8080-0;ClassName=null;MethodName=null;_RequestID=22dc7e71-9cef-4824-920f-5b3a2464c1d9;|SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?
        bind => [URGENCY_SEQ]|#]
[/code]

This is how I build the Document Entity.

[code]
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Document addDocument(Document docVO) {

                entityManager.joinTransaction();
                
                Document doc = new Document();
                
                DataType dataType = getDataType(docVO.getDataType().getDescription());
                UrgencyIndicator urgency = getUrgencyIndicator(docVO.getUrgencyIndicator().getCode());
                DocMetaData metadata = getDocMetadata(docVO.getDocMetadata().getStoragePath());
                WeaponsSystem weaponsSys = getWeaponsSystem(docVO.getWeaponsSystem().getCode());
                FileType fileType = getFileType(docVO.getFileType().getExtension());
                
                Date sqlDate = new Date(new java.util.Date().getTime());
                Set<Collection> collectSet = getDocumentCollection("Site", "Name", sqlDate.toString());
                

                doc.setUrgencyIndicator(urgency);
                doc.setDocMetadata(metadata);
                doc.setFileType(fileType);
                doc.setWeaponsSystem(weaponsSys);
                doc.setDataType(dataType);
                doc.setCollectionSet(collectSet);
                entityManager.persist(doc);
                

                return doc;
}
[/code]

Any suggestions would be greatly appreciated.

Russ
[Message sent by forum member 'russ_ray' (russ_ray)]

http://forums.java.net/jive/thread.jspa?messageID=232822