-->
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.  [ 1 post ] 
Author Message
 Post subject: One to One mapping problem
PostPosted: Thu Feb 17, 2005 12:47 am 
Newbie

Joined: Thu Feb 17, 2005 12:35 am
Posts: 1
Hello all!

I am a student part of a team which is using Hibernate with Struts and Spring to create a web portal of sorts, and I am having an issue setting up Hibernate to work with these classes:

User.java

Code:
package com.TeamExpee.Model;

import java.util.ArrayList;
import com.TeamExpee.PIMP.Model.*;


public final class User
{
    private String email;
    private String firstname;
    private String id;
    private String lastname;
    private String password;
    private String username;
    private java.util.Date lastLogin;
    private int flags;
    private PIMPData PIMPData;
   
   /**
    * Default Constructor.
    */
   public User() {
      super();
   }
   
   /**
    * Constructor that takes input data from the user.
    * @param email The email address of the user.
    * @param firstname The first name of the user.
    * @param lastname The last name of the user.
    * @param password The password of the user.
    * @param username The username used to log in.
    */
   public User(String username, String password, String firstname, String lastname, String email)
   {
      super();
      this.email = email;
      this.firstname = firstname;
      this.lastname = lastname;
      this.password = password;
      this.username = username;
   }
   
   /**
    * @return Returns the pIMPData.
    */
   public PIMPData getPIMPData() {
      return PIMPData;
   }
   /**
    * @param data The pIMPData to set.
    */
   public void setPIMPData(PIMPData data) {
      PIMPData = data;
   }
   /**
    * @return Returns the email.
    */
   public String getEmail() {
      return email;
   }
   
   /**
    * @param email The email to set.
    */
   public void setEmail(String email) {
      this.email = email;
   }
   
   /**
    * @return Returns the firstname.
    */
   public String getFirstname() {
      return firstname;
   }
   
   /**
    * @param firstname The firstname to set.
    */
   public void setFirstname(String firstname) {
      this.firstname = firstname;
   }
   
   /**
    * @return Returns the flags.
    */
   public int getFlags() {
      return flags;
   }
   
   /**
    * @param flags The flags to set.
    */
   public void setFlags(int flags) {
      this.flags = flags;
   }
   
   /**
    * @return Returns the id.
    */
   public String getId() {
      return id;
   }
   
   /**
    * @param id The id to set.
    */
   public void setId(String id) {
      this.id = id;
   }
   
   /**
    * @return Returns the date of the lastLogin.
    */
   public java.util.Date getLastLogin() {
      return lastLogin;
   }
   
   /**
    * @param lastLogin The lastLogin to set.
    */
   public void setLastLogin(java.util.Date lastLogin) {
      this.lastLogin = lastLogin;
   }
   
   /**
    * @return Returns the lastname.
    */
   public String getLastname() {
      return lastname;
   }
   
   /**
    * @param lastname The lastname to set.
    */
   public void setLastname(String lastname) {
      this.lastname = lastname;
   }
   
   /**
    * @return Returns the password.
    */
   public String getPassword() {
      return password;
   }
   
   /**
    * @param password The password to set.
    */
   public void setPassword(String password) {
      this.password = password;
   }
   
   /**
    * @return Returns the username.
    */
   public String getUsername() {
      return username;
   }
   
   /**
    * @param username The username to set.
    */
   public void setUsername(String username) {
      this.username = username;
   }
   
    private static final class UserFlags {
      //A enum-like class for flags add them with value 2^n
       int Admin = 1;
       int SendEmails = 2;
    }
}


PIMPData.java

Code:
package com.TeamExpee.PIMP.Model;

import java.util.*;
import com.TeamExpee.Model.*;


public final class PIMPData {
   private String id;
   private String Name = "PIMP";
   private String Thumb = "PIMP.gif";
   private User User;
   
   public PIMPData() {}
   
   public PIMPData(User user) {
      User = user;
   }
   
   /**
    * @return Returns the user.
    */
   public User getUser() {
      return User;
   }
   /**
    * @param user The user to set.
    */
   public void setUser(User user) {
      User = user;
   }
   private Set Dates = new HashSet();
   
   /**
    * @return Returns the id.
    */
   public String getId() {
      return id;
   }
   /**
    * @param id The id to set.
    */
   public void setId(String id) {
      this.id = id;
   }   

   /**
    * @return Returns the dates.
    */
   public Set getDates() {
      return Dates;
   }
   /**
    * @param dates The dates to set.
    */
   public void setDates(Set dates) {
      Dates = dates;
   }
   
   /**
    * @param name The name to set.
    */
   public void setName(String name) {
      Name = name;
   }
   /**
    * @param thumb The thumb to set.
    */
   public void setThumb(String thumb) {
      Thumb = thumb;
   }
   /**
    * @return Returns the messages.
    */
   public List getMessages() {
      Date now = new Date();
      ArrayList messages = new ArrayList();
      for (Iterator it = Dates.iterator(); it.hasNext ();) {
         PIMPDate d = (PIMPDate)it.next();
         int diff = d.getStartDate().compareTo(now);
         if(d.isRemind() && (diff > -20) && (diff < 0)) {
            messages.add("Reminder: You have "+d.getName()+" in "+diff+"somethings.");
         }
      }
      return messages;
   }
   
   public List findDates(int month, int day, int year) {
      ArrayList dates = new ArrayList();
      for (Iterator it = Dates.iterator(); it.hasNext ();) {
         PIMPDate d = (PIMPDate)it.next();
         Date start = d.getStartDate();
         Date end = d.getEndDate();
         if(((start.getMonth() == month) && (start.getDay() == day) && (start.getYear() == year)) ||
            ((end.getMonth() == month) && (end.getDay() == day) && (end.getYear() == year))) {
            dates.add(d);
         }
      }
      return dates;
   }
   /**
    * @return Returns the name.
    */
   public String getName() {
      return Name;
   }
   /**
    * @return Returns the thumb.
    */
   public String getThumb() {
      return Thumb;
   }
}


PIMPDate.java

Code:
package com.TeamExpee.PIMP.Model;

public final class PIMPDate {
   private java.util.Date StartDate;
   private java.util.Date EndDate;
   private String Name;
   private String Description;
   private boolean Remind;

   /**
    * @return Returns the description.
    */
   public String getDescription() {
      return Description;
   }
   /**
    * @param description The description to set.
    */
   public void setDescription(String description) {
      Description = description;
   }
   /**
    * @return Returns the name.
    */
   public String getName() {
      return Name;
   }
   /**
    * @param name The name to set.
    */
   public void setName(String name) {
      Name = name;
   }
   /**
    * @return Returns the remind.
    */
   public boolean isRemind() {
      return Remind;
   }
   /**
    * @param remind The remind to set.
    */
   public void setRemind(boolean remind) {
      Remind = remind;
   }
   /**
    * @return Returns the endDate.
    */
   public java.util.Date getEndDate() {
      return EndDate;
   }
   /**
    * @param endDate The endDate to set.
    */
   public void setEndDate(java.util.Date endDate) {
      EndDate = endDate;
   }
   /**
    * @return Returns the startDate.
    */
   public java.util.Date getStartDate() {
      return StartDate;
   }
   /**
    * @param startDate The startDate to set.
    */
   public void setStartDate(java.util.Date startDate) {
      StartDate = startDate;
   }
}


What I am trying to figure out is, what exactly I can do to set up the relationships that I want between these classes. If you inspect the code, you will see that User contains one PIMPData, and PIMPData contains many PIMPDates. This may be trivial, but the mappings that I have come up with dont seem to work:

User.hbm.xml:

Code:
<?xml version="1.0"?>
   <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping.dtd">

   <hibernate-mapping>
      <class name="com.TeamExpee.Model.User" table="Users">
         <id name="id" column="id" type="string">
            <generator class="uuid.hex"/>
         </id>
         <property name="firstname" column="firstname" type="string"/>
         <property name="lastname" column="lastname" type="string"/>
         <property name="email" column="email" type="string"/>
         <property name="username" column="username" type="string"/>
         <property name="password" column="password" type="string"/>
         <property name="lastLogin" column="lastLogin" type="java.util.Date"/>
         <one-to-one name="PIMPData" class="com.TeamExpee.PIMP.Model.PIMPData" cascade="all"/>
      </class>
   </hibernate-mapping>


PIMPData.hbm.xml:

Code:
<?xml version="1.0"?>
   <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping.dtd">

   <hibernate-mapping>
      <class name="com.TeamExpee.PIMP.Model.PIMPData" table="PIMPData">
         <id name="id" column="id" type="string">
            <generator class="uuid.hex" />
         </id>
         <property name="Name"/>
         <property name="Thumb" />
         <set name="Dates" table="PIMPDates" lazy="true">
             <key column="id"/>
             <one-to-many class="com.TeamExpee.PIMP.Model.PIMPDate" />
         </set>
      </class>
   </hibernate-mapping>


As you can see, I tried to follow the manual's examples as best I could but I keep getting invalid mapping errors.

Any help, tutorial links, or troubleshooting is appreciated! Thanks for a wonderful, time-saving, and free product!

Kyle


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.