hibernate - @GeneratedValue(strategy = GenerationType.IDENTITY) does not work with @EmbeddedId -


i new hibernate , need here.

previously had 1 primary key , used @generatedvalue(strategy = generationtype.identity) create sequence in database.

now, have changed composite primary keys using @embeddedid , below new employee.java.

for reason, increment sequencing no longer works. stays @ 0 new entries.

some clarification of appreciated. , how can go automatically incrementing value?

thank you!

employee.java - new

@entity @table(name = "employee") public class employee implements serializable {      @embeddedid     private employeepk pk;     private string name;     private string username;      public employee() {     }      public employee(string username, string name) {         super();         this.pk = new employeepk(username);         this.name = name;     }      @embeddable     public class employeepk implements serializable {          @generatedvalue(strategy = generationtype.identity)         private int employeeid;         private string username;          public employeepk() {         }          public employeepk(string username) {             this.username = username;         }          public int getemployeeid() {             return employeeid;         }          public void setemployeeid(int employeeid) {             this.employeeid = employeeid;         }          public string getusername() {             return username;         }          public void setusername(string username) {             this.username = username;         }          @override         public int hashcode() {             final int prime = 31;             int result = 1;             result = prime * result + getoutertype().hashcode();             result = prime * result + employeeid;             result = prime * result + ((username == null) ? 0 : username.hashcode());             return result;         }          @override         public boolean equals(object obj) {             if (this == obj)                 return true;             if (obj == null)                 return false;             if (getclass() != obj.getclass())                 return false;             employeepk other = (employeepk) obj;             if (!getoutertype().equals(other.getoutertype()))                 return false;             if (employeeid != other.employeeid)                 return false;             if (username == null) {                 if (other.username != null)                     return false;             } else if (!username.equals(other.username))                 return false;             return true;         }          private employee getoutertype() {             return employee.this;         }      }      public int getemployeeid() {         return pk.getemployeeid();     }      public string getusername() {         return pk.getusername();     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     } } 

@generatedvalue javadoc mentions:

the generatedvalue annotation may applied primary key property or field of entity or mapped superclass in conjunction id annotation. use of generatedvalue annotation required supported simple primary keys. use of generatedvalue annotation not supported derived primary keys.


however hibernate supports such constructs according documenation (2.2.3.2.4. partial identifier generation). there example code, feel free give try. section of documentation contains following warning:

the hibernate team has felt such construct fundamentally wrong. try hard fix data model before using feature.

so maybe better solution rethink model rather try work (if can).


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -