-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate annotations composite key
PostPosted: Wed Apr 25, 2007 9:45 pm 
Newbie

Joined: Mon Apr 16, 2007 10:44 am
Posts: 9
Hi,

I would like to remove the tab_id column from TabObject and use a composite key instead. The composite key would be the two many-to-one variables.

Any help would be greatly appreciated.

=========================================

TabObject.java

Code:
@Entity
@Table(name="tabs")
public class TabObject implements Serializable {
   @Id
   @Column(name="tab_id")
   @GeneratedValue
   private int id;
   
   @ManyToOne
   @JoinColumn(name="site_id")
   private SiteObject site;
   
   @ManyToOne
   @JoinColumn(name="category_id")
   private CategoryObject category;

   // Set and Get methods...
}


CategoryObject.java

Code:
@Entity
@Table(name="categories")
public class CategoryObject {
   @Id
   @Column(name="category_id")
   @GeneratedValue
   private int id;
   
   @Column(nullable = false)
   private String name;

   // Set and Get methods...
}


SiteObject.java

Code:
@Entity
@Table(name="sites")
public class SiteObject {
   @Id
   @Column(name="site_id")
   @GeneratedValue
   private int id;
   
   @Column(nullable = false)
   private String name;

   // Set and Get methods...
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:02 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
It sound's like what you actually want is a many to many relationship between Category and Site rather than having the Tab object at all.
See The docs

If you actually want some additional information in the Tab class then you should be able to do this with an embedded id that just has the two many-to-one references. Though I havn't tried this.
Relevant Docs


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 10:22 am 
Newbie

Joined: Mon Apr 16, 2007 10:44 am
Posts: 9
Thanks for the advice. I've tried to map my objects as a many to many relationship with the following:

CategoryObject.java

Code:
@Entity
@Table(name="categories")
public class CategoryObject {
   @Id
   @Column(name="category_id")
   @GeneratedValue
   private int id;
   
   private SiteObject site;
   
   @Column(nullable = false)
   private String name ;
   
   public void setId(int id) {
      this.id = id ;
   }
   
   public int getId() {
      return id ;
   }
   
   public void setName(String name) {
      this.name = name ;
   }
   
   public String getName() {
      return name ;
   }
   
   public void setSite(SiteObject site) {
      this.site = site ;
   }
   
   @ManyToMany
   @JoinTable(name="sites", joinColumns={@JoinColumn(name="site_id")})
   public SiteObject getSite() {
      return site ;
   }
}


SiteObject.java

Code:
@Entity
@Table(name="sites")
public class SiteObject {
   @Id
   @Column(name="site_id")
   @GeneratedValue
   private int id;
   
   private CategoryObject category;
   
   @Column(nullable = false)
   private String name ;
   
   @Column(nullable = false)
   private String link ;
   
   @Column(nullable = false)
   private String logo ;
   
   public void setId(int id) {
      this.id = id ;
   }
   
   public int getId() {
      return id ;
   }
   
   public void setName(String name) {
      this.name = name ;
   }
   
   public String getName() {
      return name ;
   }
   
   public void setLink(String link) {
      this.link = link ;
   }
   
   public String getLink() {
      return link ;
   }
   
   public void setLogo(String logo) {
      this.logo = logo ;
   }
   
   public String getLogo() {
      return logo ;
   }
   
   public void setCategory(CategoryObject category) {
      this.category = category ;
   }
   
   @ManyToMany
   @JoinTable(name="categories", joinColumns={@JoinColumn(name="category_id")})
   public CategoryObject getCategory() {
      return category ;
   }
}


Which gives me the following error when I start my application server:

Code:
org.hibernate.MappingException: Could not determine type for:
SiteObject, for columns: [org.hibernate.mapping.Column(site)]
        at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
        at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
        at org.hibernate.mapping.Property.isValid(Property.java:185)
        at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:4
40)


I have commented out all my other hibernate mapping objects except these two. Any help would be greatly appreaciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 29, 2007 4:41 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
If you're using a ManyToMany relationship you need to have a collection on each side. I thought that was what you were trying to achieve.

Looking at your latest set of objects it looks like you're actually after a OneToOne. Docs


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