I have a domain model which can be simplified as follows:

// AddressType.java

public enum AddressType implements java.io.Serializable {
        BILLING,
        SHIPPING;
}

// Address.java

import javax.persistence.*;
import java.util.*;

@Entity
public class Address implements java.io.Serializable {
        @Id @GeneratedValue
        public long Id;
        public String street, city, state, zip;
        @Enumerated(EnumType.STRING)
        public AddressType addressType;
        
        @ManyToMany(mappedBy="addresses")
        public Set<Customer> customers;
}

//Customer.java

import javax.persistence.*;
import java.util.*;

@Entity
public class Customer implements java.io.Serializable {
        @Id @GeneratedValue
        public long Id;
        public String name;
        
        @ManyToMany
        Set<Address> addresses;
}

Intuitively, though, AddressType is more relevant to the relationship
*between* Customer and Address, not solely Address.  The same Address could
have multiple AddressTypes depending on which Customer you're talking about
(for instance, if I have my office address listed as my personal SHIPPING
address but my employer is also a distinct Customer who uses the same
address for its BILLING).

Of course, the most obvious solution would be to explicitly define the
associative entity as a proper domain class:

// CustomerAddress.java

import javax.persistence.*;

@Entity
public class CustomerAddress implements java.io.Serializable {
        @Id @GeneratedValue
        public long Id;
        public Customer customer;
        public Address address;
        public AddressType addressType;
}


... but then I lose all the advantages of JPA to handle the many-to-many
relationship.

Ideally, I'd like to do something like this:

// Address.java

import javax.persistence.*;
import java.util.*;

@Entity
public class Address implements java.io.Serializable {
        @Id @GeneratedValue
        public long Id;
        public String street, city, state, zip;
        @Enumerated(EnumType.STRING)
        public AddressType addressType;
        
        @ManyToMany(mappedBy="addresses")
        public Map<Customer, Set<AddressType>> customers;
}

//Customer.java

import javax.persistence.*;
import java.util.*;

@Entity
public class Customer implements java.io.Serializable {
        @Id @GeneratedValue
        public long Id;
        public String name;
        
        @ManyToMany
        Map<AddressType, Set<Address>> addresses;
}


As you can see, this pattern would allow me to look up all the Addresses of
a certain AddressType directly from the Customer class.  It would also allow
me to lookup all the AddressTypes associated with a particular Customer
directly from the Address class.  But it doesn't seem to be covered in the
general JPA spec, and OpenJPA treats the Map as a BLOB.  Off in SQL-land,
it's very common to add lookup-table references to associate entities.  It
seems like there should be a way to do accommodate this in JPA without
creating a new class.  Any suggestions?
-- 
View this message in context: 
http://www.nabble.com/Using-a-Maps-instead-of-Sets-to-handle-%40ManyToMany--tp14743250p14743250.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to