-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: a bidirectional many-to-many mapping problem
PostPosted: Mon Feb 01, 2010 1:32 am 
Newbie

Joined: Mon Feb 01, 2010 12:19 am
Posts: 4
There is a simple logic to have a try to build many-to-many bidirectional mapping.
persons to addresses, that is, a person has many addresses and a address has accommodated many persons.

The following are details:

Java classes:
public class Address implements java.io.Serializable
{
private Integer id;
private String addressName;
private Set apersonIds = new HashSet();
//omit getter and setter methods
}

public class Person implements java.io.Serializable
{
private Integer id;
private String name;
private Set paddressIds = new HashSet();
//omit getter and setter methods
}

Database Tables:
CREATE TABLE `address` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`address_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `person` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `personaddress` (
`jpersonid` int(11) NOT NULL DEFAULT '0',
`jaddressid` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`jpersonid`,`jaddressid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

person.hbm.xml:
<hibernate-mapping>
<class name="pojo.Person" table="person" catalog="testperson">

<id name="id" type="java.lang.Integer" column="Id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="10" />
</property>
<set name="paddresIds" table="personaddress">
<key column="jpersonid" />
<many-to-many column="jaddressid" class="pojo.Address" />
</set>
</class>
</hibernate-mapping>

address.hbm.xml:
<hibernate-mapping>
<class name="pojo.Address" table="address" catalog="testperson">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="native"/>
</id>

<property name="addressName" type="java.lang.String">
<column name="address_name" />
</property>

<set name="apersonIds" table="personaddress" cascade="all">
<key column="jaddressid" not-null="true" />
<many-to-many column="jpersonid" class="pojo.Person" />
</set>
</class>
</hibernate-mapping>

.cfg.xml:

<mapping resource="pojo/Address.hbm.xml" />
<mapping resource="pojo/Person.hbm.xml" />

When I run the following codes:

Transaction tr = ses.beginTransaction();

Person per1 = new Person();
per1.setName("Tom");
Person per2 = new Person();
per2.setName("jerry");

Address add1 = new Address();
add1.setAddressName("zoo1");
Address add2 = new Address();
add2.setAddressName("zoo2");

add2.getApersonIds().add(per1);
add1.getApersonIds().add(per1);
add1.getApersonIds().add(per2);
ses.save(add1);
ses.save(add2);
tr.commit();

The exceptions will be shown:
%%%% Error Creating SessionFactory %%%%
org.hibernate.PropertyNotFoundException: Could not find a getter for paddresIds in class pojo.Person
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275)
at org.hibernate.mapping.Property.getGetter(Property.java:272)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:247)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:125)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at session.factory.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
at pojo.BaseHibernateDAO.getSession(BaseHibernateDAO.java:14)
at pojo.TestPerson.main(TestPerson.java:17)
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource pojo/Address.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at session.factory.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
at session.factory.HibernateSessionFactory.getSession(HibernateSessionFactory.java:53)
at pojo.BaseHibernateDAO.getSession(BaseHibernateDAO.java:14)
at pojo.TestPerson.main(TestPerson.java:17)

Caused by: org.hibernate.DuplicateMappingException: Duplicate collection role mapping pojo.Address.apersonIds
at org.hibernate.cfg.Mappings.addCollection(Mappings.java:124)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2066)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2041)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:359)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
at org.hibernate.cfg.Configuration.add(Configuration.java:669)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 9 more

This exception is threw out at the statement:
Transaction tr = ses.beginTransaction();

I think the key point is "Duplicate collection role mapping pojo.Address.apersonIds" but I have searched it with no duplication.

can anyone help me deal with that? thanks so much


Top
 Profile  
 
 Post subject: Re: a bidirectional many-to-many mapping problem
PostPosted: Mon Feb 01, 2010 3:27 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think the main problem is the first exception:
Quote:
Could not find a getter for paddresIds in class pojo.Person


How come that the your code continues after that? It seems like it is trying to re-create the session factory a second time after the first failure and in the process of doing that doing something strange with the configuration. Could it be that second exception happens because it is trying to parse the mappings files a second time?

In any case. I think your getter/setter methods for paddresIds has an incorrect name. It should be getPaddresIds()/setPaddresIds().


Top
 Profile  
 
 Post subject: Re: a bidirectional many-to-many mapping problem
PostPosted: Mon Feb 01, 2010 4:47 am 
Newbie

Joined: Mon Feb 01, 2010 12:19 am
Posts: 4
I am sure that the class has the right getter/setter methods (getPaddressIds/setPaddressIds) like you mentioned above

Quote:
Could it be that second exception happens because it is trying to parse the mappings files a second time?


Yes, It maps a second time, but both mapping are failed.
the exception occurs at

configuration.configure(configFile);

and sure also that we have set a correct configFile value


Top
 Profile  
 
 Post subject: Re: a bidirectional many-to-many mapping problem
PostPosted: Mon Feb 01, 2010 4:53 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I just double-checked and noted that the mapping file has a spelling error: <set name="paddresIds"...> It should be <set name="paddressIds" ...>.


Top
 Profile  
 
 Post subject: Re: a bidirectional many-to-many mapping problem
PostPosted: Mon Feb 01, 2010 5:02 am 
Newbie

Joined: Mon Feb 01, 2010 12:19 am
Posts: 4
Thank you in advance, spelling-error is the point

Your double-check more usefully than my many-check.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.