Hi there,
I am trying to modify Laliluna's sample of Struts and Hibernate to insert customer when typing in Book information.
In bookAdd.jsp, I can get all customers in drop down box, after I type in all fields and select one customer, and submit, it says
Quote:
argument type mismatch
. it is right, the customer returned from jsp is integer, but in Book.hbm.xml, it must be Class Customer, how can I make it works?
I researched many days and couldn't get it. your any input will be appreciated.
Lamborghini
Code:
Postgre SQL ScriptCREATE TABLE customer( id serial NOT NULL, firstname text, lastname text, age int4, CONSTRAINT customer_pk PRIMARY KEY (id)) ;CREATE TABLE book( id serial NOT NULL, title text, author text, customer_fk int4, borrowallowed bool NOT NULL DEFAULT true, CONSTRAINT book_pk PRIMARY KEY (id)) ;ALTER TABLE book ADD CONSTRAINT book_customer FOREIGN KEY (customer_fk) REFERENCES customer (id) ON UPDATE RESTRICT ON DELETE RESTRICT;
Book.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jul 27 12:01:58 CEST 2005 -->
<hibernate-mapping package="de.laliluna.library">
<class name="Book" table="book" lazy="false">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">book_id_seq</param>
</generator>
</id>
<property name="title" column="title" type="java.lang.String" />
<property name="author" column="author" type="java.lang.String" />
<property name="available" column="available"
type="java.lang.Boolean" />
<many-to-one name="customer" column="customer_fk"
class="Customer" />
</class>
</hibernate-mapping>
Customer.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jul 27 12:01:58 CEST 2005 -->
<hibernate-mapping package="de.laliluna.library">
<class name="Customer" table="customer" lazy="false">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">customer_id_seq</param>
</generator>
</id>
<bag name="books" inverse="false">
<key column="customer_fk" />
<one-to-many class="Book" />
</bag>
<property name="name" column="name" type="java.lang.String" />
<property name="lastname" column="lastname"
type="java.lang.String" />
<property name="age" column="age" type="java.lang.Integer" />
</class>
</hibernate-mapping>
BookEditForm.java
Code:
//Created by MyEclipse Struts
//XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.4/xslt/JavaClass.xsl
package de.laliluna.library.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import de.laliluna.library.Book;
import de.laliluna.library.Customer;
/**
* MyEclipse Struts Creation date: 07-27-2005
*
* XDoclet definition:
*
* @struts:form name="bookEditForm"
*/
public class BookEditForm extends ActionForm {
// --------------------------------------------------------- Instance
// Variables
private Integer customerId;
private Book book;
public String getAuthor() {
// TODO Auto-generated method stub
return book.getAuthor();
}
public Boolean getAvailable() {
// TODO Auto-generated method stub
return book.getAvailable();
}
public Customer getCustomer() {
// TODO Auto-generated method stub
return book.getCustomer();
}
public Integer getId() {
// TODO Auto-generated method stub
return book.getId();
}
public String getTitle() {
// TODO Auto-generated method stub
return book.getTitle();
}
public void setAuthor(String author) {
// TODO Auto-generated method stub
book.setAuthor(author);
}
public void setAvailable(Boolean available) {
// TODO Auto-generated method stub
book.setAvailable(available);
}
public void setCustomer(Customer customer) {
// TODO Auto-generated method stub
book.setCustomer(customer);
}
public void setId(Integer id) {
// TODO Auto-generated method stub
book.setId(id);
}
public void setTitle(String title) {
// TODO Auto-generated method stub
book.setTitle(title);
}
// --------------------------------------------------------- Methods
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public void reset(ActionMapping arg0, HttpServletRequest arg1) {
book =new Book();
}
}
bookAdd.jsp
Code:
//Created by MyEclipse Struts
//XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.4/xslt/JavaClass.xsl
package de.laliluna.library.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import de.laliluna.library.Book;
import de.laliluna.library.Customer;
/**
* MyEclipse Struts Creation date: 07-27-2005
*
* XDoclet definition:
*
* @struts:form name="bookEditForm"
*/
public class BookEditForm extends ActionForm {
// --------------------------------------------------------- Instance
// Variables
private Integer customerId;
private Book book;
public String getAuthor() {
// TODO Auto-generated method stub
return book.getAuthor();
}
public Boolean getAvailable() {
// TODO Auto-generated method stub
return book.getAvailable();
}
public Customer getCustomer() {
// TODO Auto-generated method stub
return book.getCustomer();
}
public Integer getId() {
// TODO Auto-generated method stub
return book.getId();
}
public String getTitle() {
// TODO Auto-generated method stub
return book.getTitle();
}
public void setAuthor(String author) {
// TODO Auto-generated method stub
book.setAuthor(author);
}
public void setAvailable(Boolean available) {
// TODO Auto-generated method stub
book.setAvailable(available);
}
public void setCustomer(Customer customer) {
// TODO Auto-generated method stub
book.setCustomer(customer);
}
public void setId(Integer id) {
// TODO Auto-generated method stub
book.setId(id);
}
public void setTitle(String title) {
// TODO Auto-generated method stub
book.setTitle(title);
}
// --------------------------------------------------------- Methods
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public void reset(ActionMapping arg0, HttpServletRequest arg1) {
book =new Book();
}
}