users@glassfish.java.net

Re: No ID column

From: Markus Fuchs <Markus.Fuchs_at_Sun.COM>
Date: Tue, 12 Jun 2007 11:33:35 -0700
Hi Kenneth,

1) If your join table, say  "CUSTOMERCOMPANYTYPE", has attributes besides the join columns, you must map it as a separate Entity. Something like

In CompanyType:
@OneToMany

  private Collection<CustomerCompanyType> customerCompanyTypes;

 
In Customer:

  @OneToMany

  private Collection<CustomerCompanyType> customerCompanyTypes;

In CustomerCompanyType:
  @IDClass(name="CustomerCompanyTypeId")
  @Id private int customerId;
  @Id private int companyTypeId;

  @ManyToOne

  @JoinColumn(name = "customer_id", referencedColumnName = "id")

  private Customer customer;

 

  @ManyToOne

  @JoinColumn(name = "company_type_id", referencedColumnName = "id")

  private CompanyType companyType;

public class CustomerCompanyTypeId {
public int customerId;
public int companyTypeId;
}
 
2) If your join table just has the join columns, you should use the @ManyToMany annotation:

In CompanyType:
  @ManyToMany(mappedBy="companyTypes")

  private Collection<Customer> customers;

 
In Customer:

  @ManyToMany

  @JoinTable(
  name="
CUSTOMERCOMPANYTYPE",
   joinColumns=
   @JoinColumn(name="CUSTOMER_ID", referencedColumnName="ID"),
   inverseJoinColumns=
  @JoinColumn(name="COMPANY_TYPE_ID", referencedColumnName="ID")
  )

 private Collection<CompanyType> companyTypes;

-- markus.

Kenneth Clark wrote:

Sorry just to follow up, the verifier returns this message

 

Exception Description: Entity class [class xxx] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass.

 

Below are the fields

  @ManyToOne

  @JoinColumn(name = "customer_id", referencedColumnName = "id")

  private Customer customer;

 

  @ManyToOne

  @JoinColumn(name = "company_type_id", referencedColumnName = "id")

  private CompanyType companyType;

 

Does anyone have any input or direction? Am I going to have to create a primary key for this table. I cannot see the point of the primary key other than to circumvent this error

 

Kenneth Clark
System Architect / Lead Developer

Rabid Dog Laboratories ™
Putting the art back into development

tel:           +27 11 475 7409
mobile:   +27 82 500 5090
e-mail:     kenneth@rabiddog.co.za
website:  http://www.rabiddog.co.za/


From: Kenneth Clark [mailto:kenneth@rabiddog.co.za]
Sent: 12 June 2007 19:23
To: users@glassfish.dev.java.net
Subject: No ID column

 

I have a many to many table that designates the relationships between two entities and I get the following error:

It should define either an @Id, @EmbeddedId or an @IdClass. javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0 (Build b41d-beta2 (04/24/2007))):

 

The table contains no primary keys as it doesn’t require any.

 

Anyone have any ideas?

 

Kenneth Clark
System Architect / Lead Developer

Rabid Dog Laboratories ™
Putting the art back into development

tel:           +27 11 475 7409
mobile:   +27 82 500 5090
e-mail:     kenneth@rabiddog.co.za
website:  http://www.rabiddog.co.za/