-1

I've got 3 classes, one superclass and two subclasses. Each subclass has a collection of the other subclass and I want to map a one to many relationship between them. My superclass is person, and the subclasses are called referrer and broker. The relationship I want to represent is that one referrer can have many brokers and that one broker can have many referrers.

@Component
@Entity
@Table(name="Referrer")
@PrimaryKeyJoinColumn(name="rowID")
public class Referrer extends Person implements Serializable{

    private static final long serialVersionUID = 972571370198603227L;

    @Column(name="rowId")
    private String referrerID;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn
    private List<Broker> brokers = new ArrayList<Broker>();

And broker looks like this:

@Component
@Entity
@Table(name="Broker")
@PrimaryKeyJoinColumn(name="rowID")
public class Broker extends Person implements Serializable {

    private static final long serialVersionUID = 5648239413711716027L;

    @Column(name="AdminID", nullable=true)
    private String adminID;

    @Column
    private boolean isAdmin = false;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="referrer")
    private List<Referrer> referrers = new ArrayList<Referrer>();

The primary key for Broker should be a field called rowID that is contained within the Person superclass.

I'm really stuck, so any help would be greatly appreciated.

JamesENL
  • 6,400
  • 6
  • 39
  • 64

2 Answers2

2

In the end what you have here is a ManyToMany relationship. One question aside, are you using a join table? It looks like thats the case. Basicaly what you try is this:

   @ManyToMany(cascade=CascadeType.ALL)
        @JoinTable(
           name = "jointablename",
           joinColumns = @JoinColumn(name = "FlexRowId"), 
           inverseJoinColumns = @JoinColumn(name = "FlexRowId")
         )
private List<Referrer> referrers = new ArrayList<Referrer>();

Basically thats what you would need to add in both entities. Probably you can then ommig the @PrimaryKeyJoinColumn(name="FlexRowID"). But I have to admit that I don't know how it would work out using basicaly the same column for mapping on both sides. So please keep me posted on ho it works out.

Carsten
  • 1,511
  • 2
  • 13
  • 24
  • I ended up using a join table and did a One to Many then Many To One inside the join table. So both Broker and Referrer have a One to Many and then in my join table both have a Many To One. It seems to work. I hope. – JamesENL May 30 '13 at 03:50
  • 1
    This sort of mapping is usually done when there are additional columns in the join table. If the join table has only two columns (Pk of Broker,Referrer) then it is over head. http://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns Anyways its your choice but just saying that there is unnecessary over head of managing crud for the entity mapped to join table – srikanth yaradla May 30 '13 at 09:41
1

If the primary key for Broker is in Person then my approach is this

  1. Define one-to-one join relation ship between Person and Broker each having its own table

  2. Use Person's PK in foreign-id generator for Broker (make it bi directional so that cascade will work fine)

  3. Repeat the same for Referrer (it has its own table)

  4. Introduce another intermediate table having two columns (of course with different names) composite PK of both tables (Broker and Referrer). Use many-to-many relation ship for Broker (and Referrer) and Join on respective PK columns on intermediate table.

JamesENL
  • 6,400
  • 6
  • 39
  • 64
srikanth yaradla
  • 1,225
  • 10
  • 13