-->
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.  [ 1 post ] 
Author Message
 Post subject: n:m relations
PostPosted: Fri Dec 17, 2004 6:27 am 
Newbie

Joined: Fri Dec 17, 2004 6:05 am
Posts: 1
Hello!

I have a problem with hibernate. I would like to implement many-to-many relation of 2 db-tables with the help of hibernate.. I have tried a lot of things out, but i didnt succeed.

I have following tables defined in my database:


CREATE TABLE `companies` (
`Id` int(6) unsigned NOT NULL auto_increment,
....
.....
.....
`description` text,
`changeDate` datetime default NULL,
PRIMARY KEY (`Id`)
)

CREATE TABLE `branch` (
`Id` int(6) unsigned NOT NULL auto_increment,
`branch` varchar(255) NOT NULL default '',
`description` text,
PRIMARY KEY (`Id`)
)


And relationship-table, that contains companyID and branchID

CREATE TABLE `companybranch` (
`Id` int(6) unsigned NOT NULL auto_increment,
`companyID` int(6) NOT NULL default '0',
`branchID` int(6) NOT NULL default '0',
PRIMARY KEY (`Id`),
KEY `companyID` (`companyID`),
KEY `branchID` (`branchID`)
)


Mapping documents:

Here related fragment from my mapping documents:

<!-- mapping for table companies -->
<class name="com.fh.marketplace.beans.CompanyFormBean" table="companies">

<!-- unique id -->
<id name="id" type="int" unsaved-value="any">
<column name="id" sql-type="int(6)" not-null="true"/>
<generator class="increment"/>
</id>
....
.....
.....
<array name="branches" table="companybranch" cascade="all">
<key column="companyID"/>
<index column="Id"/>
<many-to-many column="branchID" class="com.fh.marketplace.beans.masterdata.BranchBean"/>
</array>

</class>

<!-- mapping for table branches -->
<class name="com.fh.marketplace.beans.masterdata.BranchBean" table="branch">
<id name="id" type="int" unsaved-value="any">
<column name="Id" sql-type="int(6)" not-null="true"/>
<generator class="increment"/>
</id>
<property name="branch">
<column name="branch" not-null="true"/>
</property>
<property name="description">
<column name="description"/>
</property>
</class>

code fragments from beans

public class CompanyFormBean extends ActionForm implements Serializable{

private int id = -1;
...
....
private int[] branches;

// getters and setter are not listed here
...
....
/**
* @return Returns the branches.
*/
public int[] getBranches() {
return branches;
}
/**
* @param branches The branches to set.
*/
public void setBranches(int[] branches) {
this.branches = branches;
}
}


AND BranchBean with setters and getter not listed here...
public class BranchBean implements Serializable {
private int id = -1;
private String branch;
private String description;

...
...
}
Full stack trace of any exception that occurs:
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.fh.marketplace.beans.CompanyFormBean.setBranches
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:423)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.fh.marketplace.beans.CompanyFormBean.setBranches
net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:213)
net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2210)
net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:315)
net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
net.sf.hibernate.loader.Loader.list(Loader.java:1024)
net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
com.fh.marketplace.actions.LoginAction.loadCompanyBeans(LoginAction.java:190)
com.fh.marketplace.actions.LoginAction.execute(LoginAction.java:115)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.


Name and version of the database you are using:
MYSQL 4.1.7


Can you help me to solve this problem? What i am doing wrong?

I have seen that most many-to-many mappings use Sets or something else, but i have to use Array, because the array can be accessed by struts (select tag).

With best regards and great thanks for helping:-)

Michael


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

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.