Roger's answering machine is correct. To talk a little about what I mean (at first I did not understand it and thought it would help):
Let's say you have a Foo table as such:
TABLE Foo ( bar varchar(20), bat varchar(20) )
You can usually write a w / Annotations class to work with this table:
.. But hell. There is nothing in this table that we could use as an id, and this is an outdated database that we use to [insert an important business function]. I do not think that they will allow me to start modifying tables so that I can use hibernation.
Instead, you can split the object into a working structure with hibernation, which allows you to use the entire string as a key. (Naturally, this assumes the string is unique.)
Divide the Foo object into two:
@Entity @Table(name = "FOO") class Foo { @Id private FooKey id; public void setId(FooKey id) { this.id = id; } public void getId() { return id; } }
and
@Embeddable class FooKey implements Serializable { private String bar; private String bat; @Column(name = "bar") public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } @Column(name = "bat") public String getBat() { return bat; } public void setBat(String bat) { this.bat = bat; }
}
.. And it should be so. Hibernate will use the Embeddable key to identify it, and you can make the call as usual:
Query fooQuery = getSession().createQuery("from Foo");
We hope that this will help first graders to work with this.
awied Nov 21 '09 at 17:29 2009-11-21 17:29
source share