0

As I run my main class (Runner) program I get the following exception :

org.hibernate.id.IdentifierGenerationException: attempted to assign id 
from null one-to-one property: country

I don't know the reason, why am I getting this exception.

The mapping xml :

<hibernate-mapping>
  <class name="pojo.Country" table="country">
      <id name="countryID" column="c_id">
          <generator class="increment" />
      </id>
      <property name="countryName" column="c_name" />
      <one-to-one class="pojo.PM" name="pm" cascade="all" />
  </class>

  <class name="pojo.PM" table="pm">
      <id name="countryID" column="c_id">
          <generator class="foreign">
              <param name="property">country</param>
          </generator>
      </id>
      <property name="pmName" column="pm_name" />
      <one-to-one class="pojo.Country" name="country" constrained="true" />
  </class>
</hibernate-mapping>

POJO Classes :

Country

public class Country {
    private int countryID;
    private String countryName;
    private PM pm;

    public PM getPm() {
        return pm;
    }

    public void setPm(PM pm) {
        this.pm = pm;
    }

    public int getCountryID() {
        return countryID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
}

PM

public class PM {
    private int countryID;
    private String pmName;
    private Country country;

    public int getCountryID() {
        return countryID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public String getPmName() {
        return pmName;
    }

    public void setPmName(String pmName) {
        this.pmName = pmName;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

}

and this is the class that tries to commit the transaction :

public class Runner {
    public static void main(String args[]) {System.out.println("dfdf");
        Configuration config = new Configuration().configure();
        SessionFactory sessFact = config.buildSessionFactory();
        Session session = sessFact.openSession();
        Transaction trans = session.beginTransaction();
        Country c = new Country();
        PM pm = new PM();
        pm.setPmName("Manmohan Singh");
        c.setCountryName("India");
        c.setPm(pm);

        session.save(c);
        trans.commit();

    }
}

SQL that created table :

CREATE TABLE country(c_id INTEGER,c_name TEXT,PRIMARY KEY(c_id));
CREATE TABLE pm(c_id INTEGER,pm_name TEXT);
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

1 Answers1

2

The problem is the country variable. You should initialize all the attirbutes before trying to do some transactions.

EDIT: In your Hibernate file, you want to generate the PM ID from the ID of the country property. However, this property has never been initialized.

 <class name="pojo.PM" table="pm">
      <id name="countryID" column="c_id">
          <generator class="foreign">
              <param name="property">country</param>
          </generator>
      </id>
      <property name="pmName" column="pm_name" />
      <one-to-one class="pojo.Country" name="country" constrained="true" />
  </class>

So, add pm.setCountry(c); to your code.

lvarayut
  • 13,963
  • 17
  • 63
  • 87