-->
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.  [ 9 posts ] 
Author Message
 Post subject: Mapping Help.. one-to-one or many-to-one or join??
PostPosted: Thu Aug 02, 2007 2:22 pm 
Newbie

Joined: Thu Aug 02, 2007 2:02 pm
Posts: 5
hi,

i'm using Hibernate3 and Spring MVC to build a web app. i have 2 tables that are related:

Code:
TABLE Customer
CustomerID uniqueidentifier PRIMARY KEY NOT NULL
CustomerName varchar(50) NOT NULL


Code:
TABLE Comment
CommentID int identity(1,1) PRIMARY KEY NOT NULL
CustomerID uniqueidentifier FOREIGN KEY REFERENCES Customer(CustomerID)
CommentDate datetime NOT NULL
CommentText varchar(1000) NOT NULL


my object classes - Customer.java and Comment.java each has only variables that are the columns in its table, default constructor, all getter/setters.

there can be more than one Comment per Customer.

should i relate them with a many-to-one? how would i do this? i've tried a few ways, all of which have been hugely unsuccessful.

thanks in advance for any help! - i need it


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 02, 2007 3:04 pm 
Newbie

Joined: Thu Aug 02, 2007 11:51 am
Posts: 4
Location: Denmark
First of all, would be nice to just briefly know what you have tried just to know where from to begin.. However.. Right now i'm assuming you know the basics of configuring an entity and defining the key property.. Furthermore i assume you use annotations...

I think what you want is neither, but instead a one-to-many relation... Define a List<Comment> comments on the customer class and define the property accessors (getters/setters) and annotate the getter with @OneToMany.. Furthermore you have to specify a @JoinColumn("CustomerID") to tell Hibernate the name of the foreign key field...

This way, One Customer may have Many Comments....

One more thing.. You said that you have properties representing your table fields.. Just to be sure.. Do not provide property to "CustomerID" in your Java class.. That field in the database is all handled by hibernate... When you add a comment to a customer, you simply add it to the comments list on customers...

Did this help?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 02, 2007 5:44 pm 
Newbie

Joined: Thu Aug 02, 2007 2:02 pm
Posts: 5
yes it did help. some. i'm pretty new to Hibernate and web apps. basic Java is really all i know well. i'm not using annotations, i'm writing the XML files.

so i should add a one-to-many in my Customer.hbm.xml to relate to Comment? I can define a List<Comment> comments in the Customer class with accessors. Do i then need to add/change a column in my table in my database? and i'll see if i can figure out how to join a column.

when i added CustomerID to my Customer.hbm.xml file, i set it as the id, not a property like the other columns. i did, however, add that as a property in my Comment.hbm.xml file since it is the foreign key. so i don't need that in there if i put a List<Comment> comments in the Customer class? or can i use the primary/foreign key relationships between the tables instead of defining the list in the Customer class?

Thanks for helping!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 5:02 am 
Newbie

Joined: Tue Jul 31, 2007 10:32 am
Posts: 17
Location: Amsterdam
Hi,
Assuming you want to apply the one-to-many mapping, try the following to see if it fits your problem:
Code:
<hibernate-mapping>
   <class name="Customer" table="customer">
        <id name="customer_id" type="string" column="customer_id" not-null="true"/>
        <property name="cusotmer_name" type="string" column="customer_name" not-null="true"/>
        <set name="comments" inverse="true" fetch="subselect" lazy="false" cascade="all, delete-orphan">
         <key>
                <column name="customer_id" not-null="true" />
            </key>
         <one-to-many class="Comment"/>
         </set>
    </class>
</hibernate-mapping>


<hibernate-mapping>   
    <class name="Comment" table="comment">
        <id name="comment_id" type="string" column="comment-id" not-null="true"/>
        <many-to-one name="customer" class="Customer" fetch="select">
            <column name="customer_id" not-null="true" />
        </many-to-one>
     </class>
</hibernate-mapping>


You java objects would look like :
Code:
public class Customer imlpements Serializable {
    private String customer_id;
    private String customer_name;
    private Set comment = new HashSet();
    .
    .
    .
}

public class Comment implements Serializable {
    private Customer customer;
    private String comment_id;
    .
    .
    .
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 11:52 am 
Newbie

Joined: Thu Aug 02, 2007 2:02 pm
Posts: 5
thank you. this helps a lot. i'm so close...

in my Comment class, i have an object (column in the Comment table) called commentText and that is what i would like to get.

right now, this is what i have to display Customer.customerName but will not display commentText:

Code:
<c:forEach items="${customers}" var="customer">
<tr>
   <td><c:out value="${customer.customerName}"/></td>
   <td><c:out value="${customer.comment.commentText}"/></td>
</tr>
</c:forEach>


i know Customer and Comment are linked because if i replace ${customer.comment.commentText} with ${customer.comment} in that second cell's <c:out> expression, i get the value: [Comment@123defe].

how do i get the Comment.commentText to display?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 1:07 pm 
Newbie

Joined: Thu Aug 02, 2007 11:51 am
Posts: 4
Location: Denmark
Have you defined the property commentText in the xml file? Using XML you need to define every object property you want to access.. So specify a line just as the customer name property.. Hint below from previous example :)

Code:
<property name="cusotmer_name" type="string" column="customer_name" not-null="true"/>


Wondering why you don't use annotations. :) Much less work..

Just in response to mkarami's example.. Hmm.. Wondering why inverse=true on the Set definition on the customer side. Don't believe customer is intended to be the inverse side of the relationship... Believe it should be set to false... (It marks if that side is in control of the relationship or not..)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 1:30 pm 
Newbie

Joined: Thu Aug 02, 2007 2:02 pm
Posts: 5
yes. the commentText property is defined in Comment.hbm.xml. does it need to be referenced elsewhere? here's what i have:

Customer.hbm.xml
Code:
<hibernate-mapping>
   <class name="Customer" table="Customer">
      <id name="customerID" column="CustomerID"/>
      <property name="customerName" column="CustomerName"/>
      <set name="comment" inverse="true" fetch="subselect" lazy="false" cascade="all,delete-orphan">
         <key><column name="CustomerID"/></key>
         <one-to-many class="Comment"/>
      </set>
   </class>
</hibernate-mapping>


Comment.hbm.xml
Code:
<hibernate-mapping>
   <class name="Comment" table="Comment">
      <id name="commentID" type="int" column="CommentID">
         <generator class="identity"/>
      </id>
      <property name="customerID" column="CustomerID"/>
      <property name="commentText" column="CommentText"/>
      <many-to-one name="customer" class="Customer" fetch="select">
         <column name="customerID"/>
      </many-to-one>
   </class>
</hibernate-mapping>


so how do i get the statement below to work?

Code:
<c:out value="${customer.comment.commentText}"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 3:50 pm 
Newbie

Joined: Thu Aug 02, 2007 11:51 am
Posts: 4
Location: Denmark
Well.. You can't access it just like that... Comments is a Set... There's not a single comment, so you'll have to iterate over the collection or have some action serving the single values of the collection... Can't help you much on Spring MVC nor am i a jsp shark...

Point is.. You are trying to access a collection of comments as it was just single comment...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 5:08 am 
Newbie

Joined: Tue Jul 31, 2007 10:32 am
Posts: 17
Location: Amsterdam
Hi,
dba is totally right. Comment is a set and you need to iterate through the set. you can choose two ways to get to your comments:
1- If you want to display all comments for this customer id on the page, simply use a iterator in your page to go through the comments.
2- If you want to display a comment with a specific id, introduce a new method in your customer that retrieves the comment based on the given identifier. something like the following:

Code:
public class Customer imlpements Serializable {
    private String customer_id;
    private String customer_name;
    private Set comments = new HashSet();
    .
    .
    .
    public Comment getComment(String commentID) {
        for(Iterator iter = comments.iterator(); iter.hasNext();) {
            Comment comment = (Comment)iter.next();
            if (comment.getID().equals(commentID)) {
                return comment;
            }
        }
        return null;
    }
}

public class Comment implements Serializable {
    private Customer customer;
    private String comment_id;
    .
    .
    .
}


Again, the solution depends on what you want to display on your page.
Cheers, M


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