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