-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to input foreign key?
PostPosted: Tue Jan 10, 2006 12:11 am 
Beginner
Beginner

Joined: Mon Sep 19, 2005 3:59 pm
Posts: 31
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();
   }

}


Top
 Profile  
 
 Post subject: Sorry, the bookAdd.jsp should looks like below
PostPosted: Tue Jan 10, 2006 10:59 am 
Beginner
Beginner

Joined: Mon Sep 19, 2005 3:59 pm
Posts: 31
Code:
<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>

<html>
        <head>
                <title>Add a book</title>
        </head>
        <body>
                <%-- create a html form --%>
                <html:form action="bookEdit">
                        <%-- print out the form data --%>
                        <table border="1">
                                <tbody>
                                <tr>
                                        <td>Author:</td>
                                        <td><html:text property="author" /></td>
                                </tr>
                                <tr>
                                        <td>Title:</td>
                                        <td><html:text property="title" /></td>
                                </tr>
                                <tr>
                                        <td>Available:</td>
                                        <td><html:checkbox property="available" /></td>
                                </tr>
                                <tr>
                                        <td>Customer:</td>
                                        <td>
                                        <html:select property="customer">
                                           <html:options collection="customerlist" property="id" labelProperty="name"/>
                                        </html:select>
                                        </td>
                                </tr>

                                </tbody>
                        </table>
                        <%-- set the parameter for the dispatch action --%>
                        <html:hidden property="do" value="saveBook" /> 
                       
                        <br>
                        <%-- submit and back button --%>
                        <html:button property="back"
                                                 onclick="history.back();">
                                                 Back
                        </html:button>
                        &nbsp;
                        <html:submit>Save</html:submit>
                </html:form>
        </body>
</html>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.