Question

I have a table with three columns, "A", "B" and "C". Two of these columns (A and B) are the composite primary key for the table. I've written a Java class for this table and I'm using Hibernate to map the data in the class to the data in the table. I've created another class for the Embedded (i.e., composite) key.

The two classes look like this:

@Entity
@Table(name="SOME_TABLE", schema="SOME_SCHEMA")
public class Outer {
    private Key key;
    private String a;
    private String b;
    private String c;

    @EmbeddedId
    public Key getId() {
        return this.key;
    }

    // Also a setter for Key...

    @Column(name="A")
    public String getA() {
        return this.a;
    }

    public void setA(final String a) {
        this.a = a;
    }

    // Also setters and getters for B and C.
}

.

@Embeddable
public class Key {
    private String a;
    private String b;

    @Column(name="A")
    public String getA() {
        return this.a;
    }

    public void setA(final String a) {
        this.a = a;
    }

    // Also a getter & setter for B.
}

It seems like all the Hibernate documentation glosses over where, exactly, the getters and setters should go. Should both Outer and Key have setters for the values that make up the composite key? If I do have a setter in both places, does Hibernate do the right thing (i.e., set the value on Key) if I set the value for A on Outer? Should Outer.getA/B() and Outer.setA/B() defer to the Key class?

I think it's bad coding practice to have setters in two places for the same data. It's ambiguous at the very least. I'm hoping that Hibernate will set the values on the Key class for me, but I'd like to know for sure. Thanks!

-joe

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top