-->
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: need help w/ complex mapping atleast for this hib newbie
PostPosted: Sat Nov 19, 2005 8:01 pm 
Newbie

Joined: Sat Nov 19, 2005 7:34 pm
Posts: 4
This is my first hibernate project. I"m building a simple user type web system. I have a Person.java class and a PaymentType.java class.
A Person object has fields like id, name, email, and PaymentType which is my own type or object, not a string or int. I want to be able to have one person have multiple payment types. I am no db expert so the if that it one-to-many , i don't know, i may be wrong. I am using a join table 'personpayment' . I cannot figure out how to do this. My main problem is the paymenttype field in the person object. I want to be able to get a set of paymenttypes in the person object for that field.
example:
Code:
public class Person
{
     int person_id;
     String name;
     [b]Set paymentTypes;[/b]

}



what am i doing wrong?? I get mapping exception: An association from the table person payment refers to an unmapped class: java.util.Set

Hibernate version: 3

Mapping documents:

<------------------- this is for Person.java--------------------->
Code:
<hibernate-mapping package="packageName">
   <class name="Person">
      <id name="person_id" column="person_id">
         <generator class="native"/>
      </id>
      <join table="personpayment"  optional="true">
         <key column="person_id" unique="false"/>
         <many-to-one name="paymentType"  column="paymentType_id"  not-null="true"/>
      </join>
   </class>
</hibernate-mapping>



<------------------- this is for PaymentType.java------------->
Code:
<hibernate-mapping package="packageName">
   <class name="packageName.PaymentType">
      <id name="paymentType_id" column="paymentType_id">
         <generator class="native"/>
      </id>
   </class>

</hibernate-mapping>


<---------- here is my table structures --------------------->

CREATE TABLE `paymenttype` (
`paymenttype_id` int(9) NOT NULL auto_increment,
`name` varchar(50) default NULL,
`description` text,
PRIMARY KEY (`paymenttype_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC COMMENT='InnoDB free: 3072 kB';

/*Table structure for table `person` */

drop table if exists `person`;

CREATE TABLE `person` (
`person_id` int(9) NOT NULL auto_increment,
`fname` varchar(50) default NULL,
`lname` varchar(50) default NULL,
`email` varchar(50) default NULL,
`username` varchar(50) default NULL,
PRIMARY KEY (`person_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;

/*Table structure for table `personpayment` */

drop table if exists `personpayment`;

CREATE TABLE `personpayment` (
`person_id` int(9) NOT NULL,
`paymentType_id` int(9) NOT NULL,
`startDate` datetime default NULL,
`expiration` datetime default NULL,
`status` tinyint(1) default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC COMMENT='InnoDB free: 3072 kB';

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:mySql 5

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 20, 2005 11:41 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Your database tables look sensible to me. I would, however, follow the recommendation in the Hibernate literature to add an id column and Java attribute also to the Personpayment table.

You can then map the relationship between Person and Personpayment with the <set ...> mechanism (using inverse="true") in the one-to-many direction, and with the <many-to-one ...> mechanism in the many-to-one direction. Same for the relationship between Payment and Personpayment, although I would consider omitting the inverse mapping for this one.

Concerning your remark about not being a db expert. Most database books have a section about how to proceed from a data model of your problem domain to get a database structure fitting that model. This might give you confidence in the setup of your database. Hibernate just follows this structure, so it can't improve mistakes that have been made in db design.


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.