-->
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.  [ 1 post ] 
Author Message
 Post subject: ManyToMany mapping where referencedColumn is non-primary key
PostPosted: Wed Sep 03, 2008 8:51 am 
Newbie

Joined: Wed Sep 03, 2008 7:53 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.