I'm facing a problem using Hibernate. I have 3 tables: tb_user, tb_book, tb_lending. In the tb_lending, I have the following fields:
- id_lending - int(11) - primary key
- id_user - int(11) - foreign key
- id_book - int(11) - foreign key
I have also the Beans representing the tables (tb_user and tb_book are working perfectly).
My tbLending.hbm.xml mapping this field:
<id name="id" type="java.lang.Integer">
<column name="id_lending" />
<generator class="identity" />
</id>
<many-to-one name="userId" class="com.wa2011.beans.UserBean"
not-null="true" cascade="all" unique="true" column="id_user" />
<many-to-one name="bookId" class="com.wa2011.beans.BookBean"
not-null="true" cascade="all" unique="true" column="id_book" />
From the business logic the association should be one-to-one, since for each id_lending I can have 1 user and 1 book. But I read on some forums to that in this way, using many-to-one and then declaring unique="true".
But then, when I execute a query.save I get the following error:
GRAVE: IllegalArgumentException in class: com.wa2011.beans.UserBean, getter method of property: id
I really don't know what the problem is since tb_book and tb_user as I said before work like a charm.
The save method in the LendingBean.java is:
public void saveLend(LendingBean lendingBean) {
Session session = iniHibernate();
try {
session.beginTransaction();
session.save(lendingBean);
session.getTransaction().commit();
} catch (Exception e) {
System.out.println("Error on registering lend:");
System.out.println(e);
}
}
This method is called by the servlet LendingActions inside the processRequest method, the same pattern I'm following with the other beans/servlets.
<class n开发者_StackOverflowame="com.wa2011.beans.LendingBean" table="tb_lending" catalog="wa2011">
<id name="id" type="java.lang.Integer">
<column name="id_lending" />
<generator class="identity" />
</id>
...
</class>
In the LeandingBean.java I have:
@Stateless
public class LendingBean {
private Integer id;
private Integer bookId;
private Integer userId;
...
}
Could you help me, please?
Thanks in advance.
Check LendingBean bean's id element to mapping. Is it same as in mapping?
I think your LendingBean should be like this:
@Stateless
public class LendingBean {
private Integer id;
private BookBean bookId;
private UserBean userId;
...
}
精彩评论