Primary key object mapping
mikedougherty Feb 27, 2007 2:29 PMHello,
I have the following class structure:
@Entity @Table(name="FOO") @IdClass(FooPK.class) public class Foo { private Long id; private String name; private String description; private List<FooBar> bars; @Id @Column(name="FOO_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Id @Column(name="FOO_NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } public static class FooPK { private Long id; private String name; @Id @Column(name="FOO_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Id @Column(name="FOO_NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } } }
@Entity @Table(name="BAR") @IdClass(BarPK.class) public class Bar { private Long id; private String name; private String description; @Id @Column(name="BAR_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Id @Column(name="BAR_NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } public static class BarPK { private Long id; private String name; @Id @Column(name="BAR_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Id @Column(name="BAR_NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } } }
@Entity @Table(name="FOO_BAR") @IdClass(FooBarPK.class) public class FooBar { private Foo foo; private Bar bar; private Date effectiveDate; private Date endDate; @Id @JoinColumn(name="FOO_ID") public Foo getFoo() { return foo; } @Id @JoinColumn(name="BAR_ID") public Bar getBar() { return bar; } @Id @Column(name="EFF_DATE") public Date getEffectiveDate() { return effectiveDate; } public static class FooBarPK { private Foo foo; private Bar bar; private Date effectiveDate; @Column(name="FOO_ID") public Foo getFoo() { return foo; } @Column(name="BAR_ID") public Bar getBar() { return bar; } @Column(name="EFF_DATE") public Date getEffectiveDate() { return effectiveDate; } } }
The question I have relates to the FooBar class. The table behind it has FOO_ID and BAR_ID columns as part of the primary key. But I can't seem to figure out how to map these column values to appropriate Foo.id and Bar.id values. Any ideas on how I might be able to do this?
Thanks for your help.