ID of object with all of its variables in hibernate -
with class, want class's id value of of attributes. implicitly means need row these values existing in database. how in hibernate?
public class weatherstate { private string weathertype; private double temperature; }
when persistent attributes should directly attributes of weatherstate, @idclass way go (persistence annotations imported javax.persistence package):
@entity @idclass(weatherstateid.class) public class weatherstate { @id private string weathertype; @id private double temperature; //getters, setters } public class weatherstateid implements serializable { private string weathertype; private double temperature; //getters, setters, equals, hashcode }
other options use @embeddedid:
@entity public class weatherstate { @embeddedid private weatherstateid weatherstateid; public weatherstateid getweatherstateid() { return weatherstateid; } public void setweatherstateid(weatherstateid weatherstateid) { this.weatherstateid = weatherstateid; } } @embeddable public class weatherstateid implements serializable { private string weathertype; private double temperature; //getters, setters, equals, hashcode }
in both cases important provide equals , hashcode, said in jpa 2.0 specification:
the primary key class must define equals , hashcode methods. semantics of value equality these methods must consistent database equality database types key mapped.
Comments
Post a Comment