Okay I have read the forum,article,tutorial, documentation and nothing works. So pleazzzzz somebody help me :(
Okay basically i have to table :
1. Forum table
Code:
CREATE TABLE `tbl_opdg_forum` (
`ID` int(11) NOT NULL auto_increment,
`CATEGORY` varchar(100) NOT NULL default '',
`CREATED_DT` timestamp(14) NOT NULL,
`NAME` varchar(100) NOT NULL default '',
`POST_CNT` int(11) NOT NULL default '0',
PRIMARY KEY (`ID`)
) TYPE=InnoDB;
2. Topic Access table
Code:
CREATE TABLE `tbl_opdg_topicaccess` (
`ID` int(11) NOT NULL auto_increment,
`TOPIC_ID` int(11) default NULL,
`USER_ID` varchar(100) NOT NULL default '',
PRIMARY KEY (`ID`),
KEY `TOPIC_ID` (`TOPIC_ID`),
CONSTRAINT `0_34` FOREIGN KEY (`TOPIC_ID`) REFERENCES `tbl_opdg_forum` (`ID`)
) TYPE=InnoDB;
Basically topic access will contain all user that are allow to view a topic in a forum.
Here is my hbm.xml:
1.TopicBean
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate//Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="Forum">
<class name="Forum.TopicBean" table="TBL_OPDG_FORUM">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="category">
<column name="CATEGORY" sql-type="varchar" not-null="true"/>
</property>
<property name="name">
<column name="NAME" sql-type="varchar" not-null="true"/>
</property>
<property name="createdDt">
<column name="CREATED_DT" sql-type="timestamp" not-null="false"/>
</property>
<array name="topicAccessBean" inverse="true" cascade="all">
<key column="TOPIC_ID"/>
<index column="id"/>
<one-to-many class="Forum.TopicAccessBean"/>
</array>
</class>
</hibernate-mapping>
2. TopicAccessBean
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate//Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="Forum">
<class name="Forum.TopicAccessBean" table="TBL_OPDG_TOPICACCESS">
<id name="id" type="int" column="ID">
<generator class="native"/>
</id>
<many-to-one name="topicID" class="Forum.TopicBean"/>
<property name="userID">
<column name="USER_ID" sql-type="varchar" not-null="true"/>
</property>
</class>
</hibernate-mapping>
These are my two persistent classes :
1. TopicBean
Code:
public class TopicBean implements AbstractPersister{
private String category;
private String name;
private TopicAccessBean[] topicAccessBean = null;
private Timestamp createdDt = null;
private int id = 0;
private static final Logger logger = Logger.getLogger(TopicBean.class);
static {
BasicConfigurator.configure();
}
/**
* @return
*/
public String getCategory() {
return category;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @param string
*/
public void setCategory(String string) {
category = string;
}
/**
* @param string
*/
public void setName(String string) {
name = string;
}
public void setValues(TopicForm form) {
setCreatedDt(new Timestamp(System.currentTimeMillis()));
setCategory(form.getCategory());
setName(form.getTopicName());
if (form
.getPrivateIndicator()
.equalsIgnoreCase(TopicConstants.PRIVATE)) {
String[] privacyList = form.getPrivacyList();
logger.debug("SETTING PRIVACY LIST");
TopicAccessBean[] topicAccessBean =
new TopicAccessBean[privacyList.length];
for (int i = 0; i < privacyList.length; i++) {
logger.debug("PRIVACY LIST " + i + " NAME " + privacyList[i]);
TopicAccessBean bean = new TopicAccessBean();
bean.setUserID(privacyList[i]);
bean.setTopicBean(this);
topicAccessBean[i] = bean;
}
setTopicAccessBean(topicAccessBean);
}
}
/**
* @return
*/
public TopicAccessBean[] getTopicAccessBean() {
return topicAccessBean;
}
/**
* @param beans
*/
public void setTopicAccessBean(TopicAccessBean[] beans) {
topicAccessBean = beans;
}
/**
* @return
*/
public Timestamp getCreatedDt() {
return createdDt;
}
/**
* @param timestamp
*/
public void setCreatedDt(Timestamp timestamp) {
createdDt = timestamp;
}
/* (non-Javadoc)
* @see common.AbstractPersister#getPersisterClass()
*/
public Class getPersisterClass() {
return TopicBean.class;
}
/**
* @return
*/
public int getId() {
logger.debug("[TOPIC BEAN] CALLING ID GETTER () " + this.id);
return this.id;
}
/**
* @param i
*/
public void setId(int i) {
id = i;
}
public String toString()
{
return "ID : " + id + " name : " + name + " category " + category;
}
/* (non-Javadoc)
* @see common.AbstractPersister#addChild(java.lang.Object)
*/
public void addChild(Object obj) {
for(int i = 0; i < this.topicAccessBean.length; i++)
{
topicAccessBean[i].setTopicID(this.getId());
topicAccessBean[i].setTopicBean(this);
}
}
}
2. TopicAccessBean
Code:
public class TopicAccessBean implements AbstractPersister{
private int topicID;
private String userID;
private int id;
private TopicBean topicBean;
private static final Logger logger = Logger.getLogger(TopicBean.class);
static {
BasicConfigurator.configure();
}
/**
* @return
*/
public int getTopicID() {
return topicBean.getId();
}
/**
* @return
*/
public String getUserID() {
return userID;
}
/**
* @param string
*/
public void setTopicID(int string) {
topicID = string;
}
/**
* @param string
*/
public void setUserID(String string) {
userID = string;
}
/**
* @return
*/
public int getId() {
return id;
}
/**
* @param i
*/
public void setId(int i) {
id = i;
}
/**
* @return
*/
public TopicBean getTopicBean() {
logger.debug("CALLING TOPIC BEAN FROM TOPICACCESSBEAN");
return topicBean;
}
/**
* @param bean
*/
public void setTopicBean(TopicBean bean) {
topicBean = bean;
}
/* (non-Javadoc)
* @see common.AbstractPersister#addChild(java.lang.Object)
*/
public void addChild(Object obj) {
}
/* (non-Javadoc)
* @see common.AbstractPersister#getPersisterClass()
*/
public Class getPersisterClass() {
return TopicAccessBean.class;
}
}
Finally this is how i save the 2 bean above:
Code:
public void saveTopic(TopicBean topicBean)
throws ApplicationException, HibernateException {
Transaction tx = null;
try {
logger.debug("CREATING TOPIC WITH : " + topicBean);
Session s = DBManager.getSession(topicBean);
tx = s.beginTransaction();
//1.Save the topic
s.save(topicBean);
logger.debug("** COMITTING TRANSACTION");
tx.commit();
} catch (HibernateException hibEx) {
if (tx != null)
tx.rollback();
throw new ApplicationException(hibEx.getLocalizedMessage());
} catch (NamingException nameEx) {
if (tx != null)
tx.rollback();
throw new ApplicationException(nameEx.getLocalizedMessage());
} catch (SQLException sqlEx) {
if (tx != null)
tx.rollback();
throw new ApplicationException(sqlEx.getLocalizedMessage());
} finally {
DBManager.closeSession();
}
}
But I get this exception :
common.ApplicationException: IllegalArgumentException occurred calling
getter of Forum.TopicBean.id
at Forum.CreateTopic.saveTopic(CreateTopic.java:66)
at Forum.CreateTopicAction.saveTopic(CreateTopicAction.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
I dont understand what's wrong with my TopicBean getId() method.
Does hibernate try to pass some thing to my getId() method ? If yes why ?
How do I get around this problem. Shall I jsut manually insert the topicAccess.
Okay I am running MySQL and Resin.
Thanks in advance