java - Composite Key in Hibernate -


java persistence hibernate shows how create composite key:

@entity @table(name = "categorized_item")   public class categorizeditem {    @embeddable   public static class id implements serializable {        @column(name = "category_id")       private long categoryid;        @column(name = "item_id")       private long itemid;        public id() {}       public id(long categoryid, long itemid) {           this.categoryid = categoryid;           this.itemid = itemid;       }        public boolean equals(object o) {           if (o != null && o instanceof id) {               id = (id)o;               return this.categoryid.equals(that.categoryid) &&                   this.itemid.equals(that.itemid);           } else {               return false;           }       }       public int hashcode() {           return categoryid.hashcode() + itemid.hashcode();       }   }  @embeddedid private id id = new id();  @column(name = "added_by_user") private string username;  @column(name = "added_on") private date dateadded = new date(); 

is approach of making id static common when making composite key? if so, why?

why necessary instantiate id in categorizeditem?

private id id = new id();

there 2 ways can specify composite keys, 1 being @embeddedid , other being id class: @idclass. pretty tutorial showing options , providing suggestions on how work on both styles of composite key specifier: http://www.objectdb.com/java/jpa/entity/id

i haven't seen primary key class embedded inside class using key, if key class not required used anywhere else, makes sense. public static nested class same root level class, shows intent class has tight association enclosing class.

as creating instance of class, think examples via constructor. must provide values primary key components before trying persist entity database. here's typical example of that: http://www.java2s.com/tutorial/java/0355__jpa/embeddedcompoundprimarykey.htm


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -