-->
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.  [ 6 posts ] 
Author Message
 Post subject: Composite doesn't work in joined-subclass ?
PostPosted: Mon Feb 20, 2006 9:55 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.2


Name and version of the database you are using: MySql 5.0

My Code is like below:

Code:
/**
* Represents a PostingDetails
*
* @hibernate.class   
*/
public abstract class PostingDetails implements Serializable{

   private CompositeId id;
   /**
    * @return Returns the id.
    * @hibernate.id
    *
    */ 
   public CompositeId getId() {
      return id;
   }   
   
}

public class CompositeId implements Serializable {

   private Long categoryId;
   private Long postId;

   public CompositeId() {}

   public CompositeId(Long categoryId, Long postId) {
      this.categoryId = categoryId;
      this.postId = postId;
   }
    /**
     * @return Returns the categoryId.
     * @hibernate.property access="field" column="category_id"
     *
     */
    public Long getCategoryId() {
        return categoryId;
    }
    /**
     * @param categoryId The categoryId to set.
     */
    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }
    /**
     * @return Returns the postId.
     * @hibernate.property access="field" column="post_id"
     */
    public Long getPostId() {
        return postId;
    }
    /**
     * @param postId The postId to set.
     */
    public void setPostId(Long postId) {
        this.postId = postId;
    }
   public boolean equals(Object o) {
      if (o instanceof CompositeId) {
          CompositeId that = (CompositeId)o;
         return this.categoryId.equals(that.categoryId) &&
               this.postId.equals(that.postId);
      } else {
         return false;
      }
   }

   public int hashCode() {
      return categoryId.hashCode() + postId.hashCode();
   }
}


/**
* Represents a ConsumerElectronics
* @hibernate.joined-subclass table="consumer_electronics"
* @hibernate.joined-subclass-key column="id"
*/
public class ConsumerElectronics extends PostingDetails implements Serializable{

   private String regionCode;
    private Brand brand;

    /**
     *
     */
    public ConsumerElectronics() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @return Returns the regionCode.
     * @hibernate.property not-null="false"
     * @hibernate.column name="region_code" length="10"
     */
    public String getRegionCode() {
        return regionCode;
    }

    /**
     * @param regionCode The regionCode to set.
     */
    public void setRegionCode(String regionCode) {
        this.regionCode = regionCode;
    }
    /**
     * @return Returns the brand.
    * @hibernate.many-to-one column="fk_brand_id"
    *                     class="com.abc.model.posting.Brand"
    *                     cascade="none"
    *                     not-null="true"
     */
    public Brand getBrand() {
        return brand;
    }

    /**
     * @param brand The brand to set.
     */
    public void setBrand(Brand brand) {
        if(brand == null)
            throw new IllegalArgumentException("Can't add a null brand into ConsumerElectronics.");
        this.brand = brand;
    }
}



and when I tried to setup database using this code, I got the following exception:

Schema text failed: Foreign key (FK34C61EAECAB24961:consumer_electronics [id])) must have same number of columns as the referenced primary key (posting_details [category_id,post_id])

pls help, Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 20, 2006 10:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have only defined one key colunm in you ConsumerElectronics annotation, but the key has two columns. I presume you just need to add a new column:
Code:
/**
* Represents a ConsumerElectronics
* @hibernate.joined-subclass table="consumer_electronics"
* @hibernate.joined-subclass-key column="category_id"
* @hibernate.joined-subclass-key column="post_id"
*/
I don't use annotations, so I'm guessing the syntax, but it's something like that.

Remember, anything labelled "column" means the database column, not the pojo field. You haven't defined a column called "id" anywhere, so column="id" is patently wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 11:46 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
It's doesn't work and I still get back the same error message. Pls help, THanks !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 1:11 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Read whatever documentation annotations comes with. Obviously it supports it, otherwise it wouldn't have allowed you set up the composite key on the parent table. Just do the equivalent thing on the joined-subclass. And if the documentation is lacking (as it all too often is in open source projects), you can search through annotations' source code. You're looking for joined-subclass-key, joined-subclass-composite-key, something like that.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 1:25 am 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
ok how will it present it hbm.xml for my case?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 4:29 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I don't understand the question. If you want to know what composite keys look like and how they're referred to in mapping files, have a look at the ref docs, section 8.4, "Components as composite identifiers". That example uses a many-to-one, but the same rules apply for joined-subclasses.


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