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!