Composite primary key, foreign key. Reference to object or key?
Asked Answered
S

1

15

I have two classes Foo and Bar. The tables in the database look like this:

|Foo|
|id : INT (PK) | bar_id : INT (PK, FK) |

|Bar|
|id : INT (PK) |

Normally I would map it like this:

@Entity
public class Bar
{
    @Id
    @Column(name = "id")
    private int id;

    @OneToMany
    private Set<Foo> foo;
}

@Entity
public class Foo
{
    @EmbeddedId
    private FooPK key;

    @MapsId("barId")
    @ManyToOne
    @JoinColumn(name = "bar_id", referencedColumnName = "id")
    private Bar bar;
}

@Embeddable
public class FooPK
{
    @Column(name = "id")
    private int id;
    @Column(name = "bar_id")
    private int barId;
}

However the id's in FooPK are loosely mapped and need to be connected manually. I would prefer a solution that maps using Objects in stead of loose ids. I tried the following but (of course) it didn't work, but I think it gives an idea of what I would like to achieve:

@Entity
public class Bar
{
    @Id
    @Column(name = "id")
    private int id;

    @OneToMany
    private Set<Foo> foo;
}

@Entity
public class Foo
{
    @EmbeddedId
    private FooPK key;

    @MapsId("barId")
    @ManyToOne
    @JoinColumn(name = "bar_id", referencedColumnName = "id")
    @Access(AccessType.FIELD)
    private Bar getBar()
    {
        return key.getBar();
    }
}

@Embeddable
public class FooPK
{
    @Column(name = "id")
    private int id;

    @Transient
    private Bar bar;

    //....

    @Column(name = "bar_id")
    @Access(AccessType.PROPERTY)
    private int getBarId
    {
        return bar.getId();
    }
}

Another problem with the latter solution is that the getBarId() method in FooPK needs to have a setBarId(Int) method. Setting the Object using the ID can be done by accessing the data access layer, however this (in my opinion) violates the separation of business/domain/data layers.

So what to do? Go with the first solution and keep the ids in sync manually or is there another (best) practice?

Sturm answered 9/6, 2012 at 12:8 Comment(0)
A
25

Referring to the JPA 2: composite primary key classes discussion, make the following changes to the Foo and FooPK classes:

@Entity
public class Foo {

    @EmbeddedId
    private FooPK key;

    @MapsId("barId") //references EmbeddedId's property
    @JoinColumn(name = "bar_id", referencedColumnName = "id")
    @ManyToOne
    private Bar bar;
}

@Embeddable
public class FooPK {

    @Column(name = "id")
    private int id;
    @Column(name = "bar_id")
    private int barId;
}

I suggest first making it work with FIELD access and then apply PROPERTY access.

So what to do? Go with the first solution and keep the ids in sync manually or is there another (best) practice?

Save yourself from pain - generate ids automatically.

Ahron answered 11/6, 2012 at 9:4 Comment(2)
So that's the first example. I've seen that this is 'the way to go', I was just wondering if the second example was a cleaner way. I guess that's a no then?Sturm
@Sturm oh, sorry, didn't notice the first example was the same ;o. I think, that it is easier to look at PK when there are IDs, not mapped Objects. If you were to search by FooPK, you would need only integers and not Objects with all unnecessary properties.Ahron

© 2022 - 2024 — McMap. All rights reserved.