-->
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.  [ 3 posts ] 
Author Message
 Post subject: how to use @ManyToOne with composite keys
PostPosted: Tue Feb 06, 2007 5:42 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 6:54 am
Posts: 22
Hi,
I have two hopefully simple questions concerning how to use relations together with annotations and composite keys.

Hibernate version:
Core 3.2.2GA, Annotations 3.2.1

I have two tables, Company and Platform, where a Company can have many platforms.

Company has the PK companyID, while Platform has a composite PK: (companyID (FK), PlatformID)

I set my classes up like this (stripped from constructors and setters and non-required fields):

Company:

Code:
@Entity
public class Company implements Serializable
{
   private int mCompanyID;
   private Set<Platform> mPlatforms;

   @Id
   public int getCompanyID()
   {
      return mCompanyID;
   }

   @OneToMany(mappedBy="company")
   public Set<Platform> getPlatforms()
   {
      return mPlatforms;
   }

   public void setPlatforms(Set<Platform> aPlatforms)
   {
      mPlatforms = aPlatforms;
   }
}



Platform:

Code:
@Entity
public class Platform implements Serializable
{

   //Columns
   private PlatformPK mComp_id;

   @Id
   public PlatformPK getComp_id()
   {
      return mComp_id;
   }

   private Company mCompany;
   @ManyToOne(cascade = CascadeType.ALL)
   @JoinColumns({
         @JoinColumn(name="companyID", referencedColumnName="companyID", insertable = false, updatable = false)
         })
   public Company getCompany()
   {
      return mCompany;
   }

   public void setCompany(Company aCompany)
   {
      mCompany = aCompany;
   }
}


PlatformPK:

Code:
@Embeddable
public class PlatformPK implements Serializable
{
   private int mCompanyID;
   private int mPlatformID;

   public int getCompanyID()
   {
      return mCompanyID;
   }

   public int getPlatformID()
   {
      return mPlatformID;
   }
}


First, why does the @ManyToOne in Platform have to be insertable = false, updatable = false? I have looked under references with composite key in the manual and doesnt find this there.

Second, if I insert some companies and platforms, I want to fetch the company from a platform:

session.beginTransaction();
Platform platform = (Platform)session.load(Platform.class, new PlatformPK(1,1));
Company company = platform.getCompany();
System.out.println(company.getCompanyID());
session.getTransaction().commit();

If I do this, shouldnt hibernate automatically fetch the company for me?
This seems to be not the case, and by looking in the console, nothing happens on the platform.getCompany() line (debug outputlevel), and I get a NPE on the following line. Am I missing something?

(I also generate schema with hbm2ddl and the tables/relations seems to be set up correctly, using HSQLDB)
thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 8:05 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 6:54 am
Posts: 22
Hi,
I solved the second question, the assocaition does not become active before the session is closed and reopened. but my first question still applies.
(btw, these pages are always slow on loading here, is this usual?)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 8:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
because otherwise hibernate would have to update the same column with potentially 2 different piece of information

Note that the many to one could be embedded in the id class

_________________
Emmanuel


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