Wednesday 15 July 2015

java - Why am I getting org.hibernate.id.IdentifierGenerationException? -


As soon as I run my main category (runner) The following exception:

  org.hibernate.id.IdentifierGenerationException: Attempted to assign an ID from zero one-to-one property: Country   

To know the reason, why am I getting this exception?

Mapping XML:

  & lt; Hibernate-Mapping & gt; & Lt; Class name = "pojo.ountry" table = "country" & gt; & Lt; Id name = "countryID" column = "c_id" & gt; & Lt; Generator class = "increment increase" /> & Lt; / Id & gt; & Lt; Property Name = "Country Name" column = "c_name" /> & Lt; One-to-one class = "pojo.PM" name = "pm" cascade = "all" /> & Lt; / Square & gt; & Lt; Class name = "pojo.PM" table = "pm" & gt; & Lt; Id name = "countryID" column = "c_id" & gt; & Lt; Generator class = "foreign" & gt; & Lt; Param name = "attribute" & gt; Country & lt; / Param & gt; & Lt; / Generator & gt; & Lt; / Id & gt; & Lt; Property Name = "pmName" column = "pm_name" /> & Lt; One-to-one class = "pojo.ountry" name = "country" constrained = "true" /> & Lt; / Square & gt; & Lt; / Hibernate-mapping & gt;   

POJO Classes:

Country

  Public category country {Private integer country ID; Private string countryname; Private PM; Public PM getPm () {return noon; } Public Zero Set PM (PM PM) {this.pm = pm; } Public int getCountryID () {return countryID; } Public Zero Set countountID (int countryID) {this.countryID = countryID; } Public string getCountryName () {return country name; } Public Zero setCountryName (string country name) {this.countryName = countryName; }}   

Prime Minister

  Prime Minister of the public class {Private Integer IDID; Private string pmName; Private country country; Public int getCountryID () {return countryID; } Public Zero Set countountID (int countryID) {this.countryID = countryID; } Public string getPmName () {return pmName; } Public Zero Set Pmnem (string BPN name) {this.pmName = pmName; } Receive public country () {return country; } Public zero set country (country country) {this.country = Country; }}   

And this is the class that tries to make the transaction:

  public class runner {public static zero main (string terms []) {Println ("dfdf"); Configuration config = new configuration () Configure (); SessionFactorySessFact = config.buildSessionFactory (); Session session = sessFact.open session (); Transaction trans = session.beginTransaction (); Country C = new country (); PM PM = new PM (); Pm.setPmName ("Manmohan Singh"); C.setCountryName ("India"); C.setPm (oclock); Session.save (c); Trans.commit (); }}   

SQL host is created:

  create table country (c_id INTEGER, C_name text, primary key (c_id)); Create table evening (c_id INTEGER, pm_name text);    

issue country is the variable to make some transactions You should start all atiruboots before trying.

Edit: In your hibernate file, you want to generate the PMID from the ID of the property country . However, this property has never been started.

  & lt; Class name = "pojo.PM" table = "pm" & gt; & Lt; Id name = "countryID" column = "c_id" & gt; & Lt; Generator class = "foreign" & gt; & Lt; Param name = "attribute" & gt; Country & lt; / Param & gt; & Lt; / Generator & gt; & Lt; / Id & gt; & Lt; Property Name = "pmName" column = "pm_name" /> & Lt; One-to-one class = "pojo.ountry" name = "country" constrained = "true" /> & Lt; / Square & gt;   

Therefore, in your code, pm.setCountry (c); Add .

No comments:

Post a Comment