-->
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.  [ 2 posts ] 
Author Message
 Post subject: Composite Primary Key
PostPosted: Mon May 08, 2006 11:40 am 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
Hi,

I have the following table structure which I should map to hibernate annotated classes

Code:
customer( customerId bigint primary key, ...);
address(addressId bigint primary key, ...);
customer2address(customer_fk bigint, address_fk bigint, type tinyint, ..., primary key(customer_fkd, address_fk, type);


Until now I was not able to map the composite primary key using hibernate. I tried the following:

2 Entity classes
Code:
public class Customer {
   @Id
   @GeneratedValue(generator = "incremendId")
   @GenericGenerator(name = "incremendId", strategy = "native")
        public Long getCustomerId() {...}

   @OneToMany(targetEntity=AddressAssignment.class, mappedBy="customer", cascade=CascadeType.ALL)
   public Set<AddressAssignment> getAddresses() {
      return this.addresses;
   }
        ....
}


Code:
public class Address {
...
   @OneToMany(targetEntity=AddressAssignment.class, mappedBy="address")
   public Set<AddressAssignment> getBusinessContacts() {
      return this.businessContacts;
   }
}


A class representing the join table
Code:
@Entity
@AssociationOverride( name="id.customer", joinColumns = @JoinColumn(name="customer") )
@AssociationOverride( name="id.address", joinColumns = @JoinColumn(name="address") )
public class AddressAssignment  {
   @EmbeddedId
   public AddressAssignmentKey id;
}


And an embeddable primary key
Code:
@Embeddable
public class AddressAssignmentKey implements Serializable{
   @ManyToOne
   @JoinColumn(name="address_fk")
   public Address getAddress() {
      return this.address;
   }
   @ManyToOne
   @JoinColumn(name="customer_fk")
   public Customer getCustomer() {
      return this.customer;
   }
   @Column(name="type")
   @Enumerated(EnumType.ORDINAL)
   public AddressType getType() {
      return this.type;
   }
}


Hibernate now always tells me, that a property 'customer' can not be found in AddressAssignment. Could anyone please tell me where the problem could be found?

Thanks,

john


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 3:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
id.customer but I'm not sure this structure work either.

_________________
Emmanuel


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