Hi
I am trying to specify a ManyToMany mapping annotation through a join table and the join table has a non primary key of the Customer class as one of the column.
In the code blow the Customer class has manytomany relation to the CustomerGroup class and in the join table (CUSTOMER_LIST) has the columns :
customer_code(non primary key, code property of the Customer)
and the cg_id (CustomerGroup Id).
It works fine in the Customer class after I updated hibernate to 3.2.6 & 3.3.1 (annotations).
The second class CustomerGroup when it tries to get the list of
customers using manytomany(mappedby...), it tries to map the customers where the customer_code of the join table is equal to the Customer.id(primary key) instead of mapping to Customer.code
Hence it loads incorrect data or throws exception of data not found from the Customer object as the row does not exist.
Can please anyone let me know how can this be achieved.
The code is below:
The Customer class is:
Code:
@Entity
public class Customer implements Serializable {
private Long id;
private Set<CustomerGroup> customerGroups;
private String code;
@Id
@GeneratedValue
@Column (name = "ID", nullable = false)
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
@Column
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public void setCustomerGroups(Set<CustomerGroup> customerGroups) {
this.customerGroups = customerGroups;
}
@ManyToMany (fetch = FetchType.EAGER)
@JoinTable(name = "CUSTOMER_LIST",
joinColumns = { @JoinColumn(name = "customer_code", referencedColumnName = "code") },
inverseJoinColumns = { @JoinColumn(name = "cg_id") })
public Set<CustomerGroup> getCustomerGroups() {
return customerGroups;
}
}
The second class where I am trying to get a list of customers using mappedby is:Code:
@Entity
public class CustomerGroup implements Serializable {
private Long id;
private Set<Customer> customers = Sets.newHashSet();
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToMany(mappedBy = "customerGroups", fetch = FetchType.EAGER)
public Set<Customer> getCustomers() {
return this.customers;
}
}
Thanks all for your help in advance.