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: Help with one-to-one association
PostPosted: Wed Jul 09, 2008 11:13 pm 
Newbie

Joined: Wed Jul 09, 2008 4:12 pm
Posts: 3
Hi there

I'm having a problem figuring out (even after reading this http://www.hibernate.org/hib_docs/refer ... tions.html) how to set up the one-to-one relationship between my "messages" and my "messages_text" tables.

basically I want to do a join as follows

messages.id=messages_text.mesid

ARGH! What am i doing wrong?

the interesting parts are in MessageText.hbm.xml at the end of my post.

Hibernate version: 3.2.6 g.a. and Spring 2.5.5

Databse DDL
messages table
Code:
CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL auto_increment,
  `parent` int(11) default '0',
  `thread` int(11) default '0',
  `catid` int(11) NOT NULL default '0',
  `name` tinytext,
  `userid` int(11) NOT NULL default '0',
  `email` tinytext,
  `subject` tinytext,
  `time` int(11) NOT NULL default '0',
  `ip` varchar(15) default NULL,
  `topic_emoticon` int(11) NOT NULL default '0',
  `locked` tinyint(4) NOT NULL default '0',
  `hold` tinyint(4) NOT NULL default '0',
  `ordering` int(11) default '0',
  `hits` int(11) default '0',
  `moved` tinyint(4) default '0',
  `modified_by` int(7) default NULL,
  `modified_time` int(11) default NULL,
  `modified_reason` tinytext,
  PRIMARY KEY  (`id`),
  KEY `thread` (`thread`),
  KEY `parent` (`parent`),
  KEY `catid` (`catid`),
  KEY `ip` (`ip`),
  KEY `userid` (`userid`),
  KEY `time` (`time`),
  KEY `locked` (`locked`),
  KEY `hold_time` (`hold`,`time`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;


messages_text table
Code:
CREATE TABLE IF NOT EXISTS `messages_text` (
  `mesid` int(11) NOT NULL default '0',
  `message` text NOT NULL,
  PRIMARY KEY  (`mesid`)
) TYPE=MyISAM;


Java Classes:
MessageText.java
Code:
package com.comp.app.models;

public class MessageText {
   private int mesid;
   private String message;
   
   public MessageText(){
   
   }
   public MessageText(int aMesid, String someText){
      this.setMesid(aMesid);
      this.setMessage(someText);
   }
   /**
    * @param mesid the mesid to set
    */
   public void setMesid(int mesid) {
      this.mesid = mesid;
   }
   /**
    * @return the mesid
    */
   public int getMesid() {
      return mesid;
   }
   /**
    * @param text the text to set
    */
   public void setMessage(String text) {
      this.message = text;
   }
   /**
    * @return the text
    */
   public String getMessage() {
      return message;
   }
   
   @Override
   public boolean equals(Object obj){
      if (this == obj)                   
         return true;
      if (obj == null)                   
         return false;
      if (getClass() != obj.getClass()) 
         return false;
      
      // Implement real comparison logic here
      return false;
   }
   
   @Override
   public int hashCode()
   {
      final int PRIME = 31;
      int result = 1;

      // do some clever hash code algorithm here.
      return result;
   }
}

Message.java
Code:
package com.comp.app.models;

public class Message implements java.io.Serializable{
   private int id;
   private int parent;
   private int thread;
   private int catid;
   private String name;
   private int userid;
   private String email;
   private String subject;
   private int time;
   private String ip;
   private int topic_emoticon;
   private int locked;
   private int hold;
   private int ordering;
   private int hits;
   private int moved;
   private int modified_by;
   private int modified_time;
   private String modified_reason;
   private MessageText message;
   
   public Message(){
   };
   
   public Message(int anId, int aParent, int aThread, int aCatid,
               String aName, int aUserid, String anEmail, String aSubject,
               int aTime, String anIp, int aTopic_emoticon,
               int aLocked, int aHold, int anOrdering, int aHits, int aMoved,
               int aModified_by, int aModfied_time, String aModified_reason){
      this.setId(anId);
      this.setParent(aParent);
      this.setThread(aThread);
      this.setCatid(aCatid);
      this.setName(aName);
      this.setUserid(aUserid);
      this.setEmail(anEmail);
      this.setSubject(aSubject);
      this.setTime(aTime);
      this.setIp(anIp);
      this.setTopic_emoticon(aTopic_emoticon);
      this.setLocked(aLocked);
      this.setHold(aHold);
      this.setOrdering(anOrdering);
      this.setHits(aHits);
      this.setMoved(aMoved);
      this.setModified_by(aModified_by);
      this.setModified_time(aModfied_time);
      this.setModified_reason(aModified_reason);
   }

   /**
    * @param id the id to set
    */
   public void setId(int id) {
      this.id = id;
   }

   /**
    * @return the id
    */
   public int getId() {
      return id;
   }

   /**
    * @param parent the parent to set
    */
   public void setParent(int parent) {
      this.parent = parent;
   }

   /**
    * @return the parent
    */
   public int getParent() {
      return parent;
   }

   /**
    * @param thread the thread to set
    */
   public void setThread(int thread) {
      this.thread = thread;
   }

   /**
    * @return the thread
    */
   public int getThread() {
      return thread;
   }

   /**
    * @param catid the catid to set
    */
   public void setCatid(int catid) {
      this.catid = catid;
   }

   /**
    * @return the catid
    */
   public int getCatid() {
      return catid;
   }

   /**
    * @param name the name to set
    */
   public void setName(String name) {
      this.name = name;
   }

   /**
    * @return the name
    */
   public String getName() {
      return name;
   }

   /**
    * @param userid the userid to set
    */
   public void setUserid(int userid) {
      this.userid = userid;
   }

   /**
    * @return the userid
    */
   public int getUserid() {
      return userid;
   }

   /**
    * @param email the email to set
    */
   public void setEmail(String email) {
      this.email = email;
   }

   /**
    * @return the email
    */
   public String getEmail() {
      return email;
   }

   /**
    * @param subject the subject to set
    */
   public void setSubject(String subject) {
      this.subject = subject;
   }

   /**
    * @return the subject
    */
   public String getSubject() {
      return subject;
   }

   /**
    * @param time the time to set
    */
   public void setTime(int time) {
      this.time = time;
   }

   /**
    * @return the time
    */
   public int getTime() {
      return time;
   }

   /**
    * @param ip the ip to set
    */
   public void setIp(String ip) {
      this.ip = ip;
   }

   /**
    * @return the ip
    */
   public String getIp() {
      return ip;
   }

   /**
    * @param topic_emoticon the topic_emoticon to set
    */
   public void setTopic_emoticon(int topic_emoticon) {
      this.topic_emoticon = topic_emoticon;
   }

   /**
    * @return the topic_emoticon
    */
   public int getTopic_emoticon() {
      return topic_emoticon;
   }

   /**
    * @param locked the locked to set
    */
   public void setLocked(int locked) {
      this.locked = locked;
   }

   /**
    * @return the locked
    */
   public int getLocked() {
      return locked;
   }

   /**
    * @param hold the hold to set
    */
   public void setHold(int hold) {
      this.hold = hold;
   }

   /**
    * @return the hold
    */
   public int getHold() {
      return hold;
   }

   /**
    * @param ordering the ordering to set
    */
   public void setOrdering(int ordering) {
      this.ordering = ordering;
   }

   /**
    * @return the ordering
    */
   public int getOrdering() {
      return ordering;
   }

   /**
    * @param hits the hits to set
    */
   public void setHits(int hits) {
      this.hits = hits;
   }

   /**
    * @return the hits
    */
   public int getHits() {
      return hits;
   }

   /**
    * @param moved the moved to set
    */
   public void setMoved(int moved) {
      this.moved = moved;
   }

   /**
    * @return the moved
    */
   public int getMoved() {
      return moved;
   }

   /**
    * @param modified_by the modified_by to set
    */
   public void setModified_by(int modified_by) {
      this.modified_by = modified_by;
   }

   /**
    * @return the modified_by
    */
   public int getModified_by() {
      return modified_by;
   }

   /**
    * @param modified_time the modified_time to set
    */
   public void setModified_time(int modified_time) {
      this.modified_time = modified_time;
   }

   /**
    * @return the modified_time
    */
   public int getModified_time() {
      return modified_time;
   }

   /**
    * @param modified_reason the modified_reason to set
    */
   public void setModified_reason(String modified_reason) {
      this.modified_reason = modified_reason;
   }

   /**
    * @return the modified_reason
    */
   public String getModified_reason() {
      return modified_reason;
   }
   
   @Override
   public boolean equals(Object obj){
      if (this == obj)                   
         return true;
      if (obj == null)                   
         return false;
      if (getClass() != obj.getClass()) 
         return false;
      
      // Implement real comparison logic here
      return false;
   }
   
   @Override
   public int hashCode()
   {
      final int PRIME = 31;
      int result = 1;

      // do some clever hash code algorithm here.
      return result;
   }

   /**
    * @param message the message to set
    */
   private void setMessage(MessageText message) {
      this.message = message;
   }

   /**
    * @return the message
    */
   private MessageText getMessage() {
      return message;
   }

}


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

<hibernate-mapping>
    <class name="com.comp.app.models.Message" table="messages" catalog="myApp">
        <comment></comment>
        <id name="id" type="java.lang.Integer" column="id" >
            <generator class="foreign">
               <param name="property">messageText</param>
            </generator>
        </id>
        <one-to-one name="message"
                 class="com.comp.app.models.MessageText"
                 property-ref="mesid" cascade="all"/>
        <property name="parent" type="java.lang.Integer">
           <column name="parent" not-null="false"/>
        </property>
        <property name="thread" type="java.lang.Integer">
           <column name="thread" not-null="false"/>
        </property>
        <property name="catid" type="java.lang.Integer">
           <column name="catid" not-null="true"/>
        </property>
        <property name="name" type="java.lang.String">
           <column name="name" length="255" not-null="false"/>
        </property>
        <property name="userid" type="java.lang.Integer">
           <column name="userid" not-null="true"/>
        </property>
        <property name="email" type="java.lang.String">
           <column name="email" length="255" not-null="false"/>
        </property>
        <property name="subject" type="java.lang.String">
           <column name="subject" length="255" not-null="false"/>
        </property>
        <property name="time" type="java.lang.Integer">
           <column name="time" not-null="true"/>
        </property>
        <property name="ip" type="java.lang.String">
           <column name="ip" length="15" not-null="false"/>
        </property>
        <property name="topic_emoticon" type="java.lang.Integer">
           <column name="topic_emoticon" not-null="true"/>
        </property>
        <property name="locked" type="java.lang.Integer">
           <column name="locked" not-null="true"/>
        </property>
        <property name="hold" type="java.lang.Integer">
           <column name="hold" not-null="true"/>
        </property>
        <property name="ordering" type="java.lang.Integer">
           <column name="ordering" not-null="false"/>
        </property>
        <property name="hits" type="java.lang.Integer">
           <column name="hits" not-null="false"/>
        </property>
        <property name="moved" type="java.lang.Integer">
           <column name="moved" not-null="false"/>
        </property>
        <property name="modified_by" type="java.lang.Integer">
           <column name="modified_by" not-null="false"/>
        </property>
        <property name="modified_time" type="java.lang.Integer">
           <column name="modified_time" not-null="false"/>
        </property>
        <property name="modified_reason" type="java.lang.String">
           <column name="modified_reason" length="255" not-null="false"/>
        </property>
    </class>
</hibernate-mapping>




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

<hibernate-mapping>
    <class name="com.comp.app.models.MessageText" table="messages_text" catalog="myApp">
        <id name="mesid" type="java.lang.Integer">
            <column name="mesid" />
            <generator class="identity" />
        </id>
        <property name="message" type="java.lang.String">
            <column name="message" not-null="true"></column>
        </property>
    </class>
</hibernate-mapping>


Full stack trace of any exception that occurs:

Name and version of the database you are using:MySQL 5.0.51b


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.