-->
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 pass an entity to Struts Action?
PostPosted: Tue Jan 10, 2006 11:54 am 
Beginner
Beginner

Joined: Mon Sep 19, 2005 3:59 pm
Posts: 31
Hi there,

I have 2 tables book and customer, and mapping files, the table book is associated to customer with many-to-one. In the jsp file, which is to insert new book, if I want to insert customer when I input book. I can get all customer info from table and list them in drop down box, when I click save to save the book with customer, it is supposed the bean of customer should be passed with book bean, but in my code, only customer id is passed. How can I fix the problem?

Thanks for your any reply.
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" outer-join="false"/>
   </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>


bookAdd.jsp
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>



N
Code:
eed
help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Code:
Code:
Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 6:55 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
When you have books and customers in your httpsession.
look for the customer whos id is specified.
reattach customer and book to the session and save them.

Regards Sebastian

Please rate, if this helped.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


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.