Re: n:m mapping problem

Armin Waibel <[email protected]> Sat, 15 Sep 2007 02:31:59 +0200
Newsgroups gmane.comp.jakarta.ojb.user
Message-ID <[email protected]>
Hi Truong,

seems that the Apache server removed the attached files. Could you 
please send the source code as normal text?
By the way the generated DB seems to be consistent, so the problem was 
caused by OJB or "wrong usage" of OJB.

regards,
Armin

Truong Nguyen Huy wrote:
>     Thank you very much for your attention to my problem.
>      I am sorry not supply full detail for you to test the problem. Let me
> put straight my issue.
> 
> Here is the script to create database (MySQL):
> -------------------------------------------------------------------
> # MySQL-Front 3.2  (Build 14.8)
> 
> /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
> /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
> /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
> /*!40101 SET NAMES latin1 */;
> /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
> /*!40103 SET TIME_ZONE='SYSTEM' */;
> /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */;
> /*!40101 SET
> SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
> */;
> /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES */;
> /*!40103 SET SQL_NOTES='ON' */;
> 
> 
> # Host: localhost    Database: test
> # ------------------------------------------------------
> # Server version 5.0.41-community-nt
> 
> DROP DATABASE IF EXISTS `test`;
> CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
> USE `test`;
> 
> #
> # Table structure for table car
> #
> 
> CREATE TABLE `car` (
>   `id` int(10) unsigned NOT NULL auto_increment,
>   `licencenum` varchar(45) NOT NULL,
>   `personid` int(10) unsigned NOT NULL,
>   PRIMARY KEY  (`id`),
>   KEY `FK_car_1` (`personid`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
> 
> #
> # Dumping data for table car
> #
> 
> INSERT INTO `car` VALUES (1,'52S4-1800',1);
> INSERT INTO `car` VALUES (2,'52S4-9448',1);
> INSERT INTO `car` VALUES (3,'50L1-2381',2);
> 
> #
> # Table structure for table person
> #
> 
> CREATE TABLE `person` (
>   `id` int(10) unsigned NOT NULL auto_increment,
>   `name` varchar(45) NOT NULL,
>   PRIMARY KEY  (`id`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
> 
> #
> # Dumping data for table person
> #
> 
> INSERT INTO `person` VALUES (1,'Truong');
> INSERT INTO `person` VALUES (2,'Tuan');
> INSERT INTO `person` VALUES (3,'Nguyen');
> INSERT INTO `person` VALUES (4,'Tam');
> 
> #
> # Table structure for table project
> #
> 
> CREATE TABLE `project` (
>   `id` int(10) unsigned NOT NULL auto_increment,
>   `Name` varchar(45) NOT NULL,
>   PRIMARY KEY  (`id`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
> 
> #
> # Dumping data for table project
> #
> 
> INSERT INTO `project` VALUES (1,'EdenTree');
> INSERT INTO `project` VALUES (2,'Simpatica');
> 
> #
> # Table structure for table project_person
> #
> 
> CREATE TABLE `project_person` (
>   `project_id` int(10) unsigned NOT NULL default '0',
>   `person_id` int(10) unsigned NOT NULL default '0',
>   PRIMARY KEY  (`project_id`,`person_id`),
>   KEY `FK_project_person_1` (`person_id`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
> 
> #
> # Dumping data for table project_person
> #
> 
> INSERT INTO `project_person` VALUES (1,1);
> INSERT INTO `project_person` VALUES (1,3);
> INSERT INTO `project_person` VALUES (2,1);
> INSERT INTO `project_person` VALUES (2,2);
> INSERT INTO `project_person` VALUES (2,3);
> INSERT INTO `project_person` VALUES (2,4);
> 
> #
> #  Foreign keys for table car
> #
> 
> ALTER TABLE `car`
>   ADD FOREIGN KEY (`personid`) REFERENCES `person` (`id`);
> 
> #
> #  Foreign keys for table project_person
> #
> 
> ALTER TABLE `project_person`
>   ADD FOREIGN KEY (`person_id`) REFERENCES `person` (`id`),
>   ADD FOREIGN KEY (`project_id`) REFERENCES `project` (`id`);
> 
> 
> /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
> /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
> /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
> /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
> /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
> /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
> 
> I have attached the source code. Please find it in the attachment.
> --------------------------------------------------------------------------------------------
> 
> Here is the list of library I use:
> --------------------------------------------
> commons-collections-3.1.jar
> commons-dbcp-1.2.1.jar
> commons-lang-2.1.jar
> commons-pool-1.2.jar
> commons-transaction-1.1.jar
> commons-logging-1.0.4.jar
> db-ojb-1.0.4.jar
> mysql-connector-java-5.0.7-bin.jar
> log4j-1.2.8.jar
> 
> OJB version that I use: 1.0.4. I download from a mirror location of Apache
> OJB
> ------------------------------------------------------------------------
> 
> 
> Thanks for your help,
> Regards,
> Truong Nguyen
> 
> 
> 
> On 9/7/07, Armin Waibel <[email protected] > wrote:
>> Hi,
>>
>> your mapping seems ok. Sounds like a bug in OJB.
>> I tried to reproduce your problem without success (using latest version
>> of OJB from SVN OJB_1_0_RELEASE branch) - never get a NPE.
>> I use the mapping from the m:n example (with Person and Project) and do:
>>
>> oid = broker.serviceIdentity().buildIdentity(Person.class, new
>> Integer(1));
>> person = (Person) broker.getObjectByIdentity(oid);
>>
>> No NPE. How can I reproduce your problem?
>>
>> regards,
>> Armin
>>
>>
>> Truong Nguyen Huy wrote:
>>> Hi all,
>>>
>>> I have a problem when using m:n mapping. I will describe it below.
>>>
>>> I just implement a tutorial in this link
>>> http://db.apache.org/ojb/docu/guides/basic-technique.html#Support+for+Non-Decomposed+m%3An+Mappings
>>>
>>> There are three table: PERSON, PROJECT, PROJECT_PERSON.
>>> PROJECT_PERSON is the connector standing for m:n relation between
>>> PROJECT and PERSON.
>>>
>>> PROJECT(id,name) PERSON(id,name) PROJECT_PERSON(project_id,
>>> person_id)
>>>
>>> The mapping file as below:____________________________
>>>
>>> <!--           vn.com.tma.testOJB.Person              -->
>>> <class-descriptor class="vn.com.tma.testOJB.Person "
>>>
>>>
>>> table="person" <field-descriptor name="id" column="id"
>>> jdbc-type="INTEGER" primarykey="true" autoincrement="true" />
>>> <field-descriptor name="name" column="name" jdbc-type="VARCHAR" />
>>>
>>>
>>> <collection-descriptor name="projects" collection-class="
>>> org.apache.ojb.broker.util.collections.ManageableArrayList "
>>> element-class-ref="vn.com.tma.testOJB.Project" auto-retrieve="true"
>>> auto-update="true" indirection-table="PROJECT_PERSON"
>>> <fk-pointing-to-this-class column="PERSON_ID"/>
>>> <fk-pointing-to-element-class column="PROJECT_ID"/>
>>> </collection-descriptor>
>>>
>>> </class-descriptor>
>>>
>>> <!--           vn.com.tma.testOJB.Project              -->
>>> <class-descriptor class="vn.com.tma.testOJB.Project"
>>>
>>> table="project" <field-descriptor name="id" column="id"
>>> jdbc-type="INTEGER" primarykey="true" autoincrement="true" />
>>> <field-descriptor name="name" column="Name" jdbc-type="VARCHAR" />
>>> <collection-descriptor name="employees" collection-class="
>>> org.apache.ojb.broker.util.collections.ManageableArrayList "
>>> element-class-ref="vn.com.tma.testOJB.Person" auto-retrieve="true"
>>> auto-update="false" indirection-table="PROJECT_PERSON"
>>> <fk-pointing-to-this-class column="PROJECT_ID"/>
>>> <fk-pointing-to-element-class column="PERSON_ID"/>
>>> </collection-descriptor> </class-descriptor>
>>>
>>> When I run these codes, it has error: PersistenceBroker broker =
>>> PersistenceBrokerFactory.defaultPersistenceBroker(); Identity oid =
>>> broker.serviceIdentity().buildIdentity(Person.class, new Integer(1));
>>>  Person person = (Person) broker.getObjectByIdentity(oid);
>>>
>>> The error is caused by the third line. This is the exception stack:
>>>
>>> Exception in thread "main"
>>> org.apache.ojb.broker.PersistenceBrokerException:
>>> java.lang.NullPointerException at
>>> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery
>> (Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.QueryReferenceBroker.doRetrieveCollection
>> (Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.QueryReferenceBroker.retrieveCollection
>> (Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.QueryReferenceBroker.doRetrieveCollections
>> (Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.QueryReferenceBroker.retrieveCollections(Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.PersistenceBrokerImpl.getDBObject(Unknown
>>> Source) at
>>> org.apache.ojb.broker.core.PersistenceBrokerImpl.doGetObjectByIdentity
>> (Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.PersistenceBrokerImpl.getObjectByIdentity
>> (Unknown
>>>  Source) at
>>>
>> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getObjectByIdentity
>> (Unknown
>>>  Source) at
>>>
>> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getObjectByIdentity
>> (Unknown
>>>  Source) at vn.com.tma.main.Run.main(Run.java:41) Caused by:
>>> java.lang.NullPointerException at
>>>
>> org.apache.ojb.broker.accesslayer.MtoNCollectionPrefetcher.associateBatched(Unknown
>>>  Source) at
>>>
>> org.apache.ojb.broker.accesslayer.MtoNCollectionPrefetcher.prefetchRelationship
>> (Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.QueryReferenceBroker.performRetrievalTasks(Unknown
>>>  Source) at
>>> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery
>> (Unknown
>>>  Source) ... 11 more
>>>
>>> But if I change auto-archive to "false" in arbitrary one of two
>>> class-descriptor, there is no error anymore. But it still causes the
>>> same error when I use method "broker.retrieveAllReferences()" on an
>>> instance of the rest class-descriptor.
>>>
>>> Only when I change auto-archive of both class-descriptor to "false",
>>> the error is omitted.
>>>
>>> Can anyone explain why and help me resolve the problem when using
>>> auto-archive="true" on both class-descriptor? Thank you very for your
>>> help.
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
> 
>