Joining tables with composite keys in a legacy system in hibernate

Posted by Steve N on Stack Overflow See other posts from Stack Overflow or by Steve N
Published on 2010-05-09T15:33:58Z Indexed on 2010/05/09 15:38 UTC
Read the original article Hit count: 166

Hi,

I'm currently trying to create a pair of Hibernate annotated classes to load (read only) from a pair of tables in a legacy system. The legacy system uses a consistent (if somewhat dated) approach to keying tables. The tables I'm attempting to map are as follows:

Customer                     CustomerAddress
--------------------------   ----------------------------
customerNumber:string (pk)   customerNumber:string (pk_1)
name:string                  sequenceNumber:int    (pk_2)
                             street:string
                             postalCode:string

I've approached this by creating a CustomerAddress class like this:

@Entity
@Table(name="CustomerAddress")
@IdClass(CustomerAddressKey.class)
public class CustomerAddress {

  @Id
  @AttributeOverrides({
    @AttributeOverride(name = "customerNumber", column = @Column(name="customerNumber")), 
    @AttributeOverride(name = "sequenceNumber", column = @Column(name="sequenceNumber"))
  })

  private   String       customerNumber;

  private   int          sequenceNumber;

  private   String       name;

  private   String       postalCode;
  ...
}

Where the CustomerAddressKey class is a simple Serializable object with the two key fields. The Customer object is then defined as:

@Entity
@Table(name = "Customer")
public class Customer {

  private String customerNumber;

  private List<CustomerAddress> addresses = new ArrayList<CustomerAddress>();

  private String name;

  ...
}

So, my question is: how do I express the OneToMany relationship on the Customer table?

© Stack Overflow or respective owner

Related posts about hibernate-annotations

Related posts about hibernate