title: Hibernate/Spring: Not-null property references a null or transient value
body: <p>I'm developing an application using Hibernate, Spring and GWT in Java. I used reverse engineering under Hibernate (JBoss Developer Studio used) to obtain POJOs and configuration files from an existing MySQL database. It's very simple database with only two entities: Country and Citizen. They have OneToMany relationship between.</p>
  
  <p>Here is the code: </p>
  
  <p>app entry point:</p>
  
  <pre><code>...
  
                      Country country = new Country();
                      country.setName("NameOfCountry"+i);
                      country.setPopulation(10000);
  
                      Citizen ctz = new Citizen();
                      ctz.setName("John");
                      ctz.setSurname("Smith");
  
  
                      ctz.setCountry(country);
                      country.getCitizens().add(ctz);
  
                      service.saveCitizen(ctz, new AsyncCallback&lt;Boolean&gt;(){
  
                          @Override
                          public void onFailure(Throwable caught) {
                              System.out.println("Problem saving citizen");
  
                          }
  
                          @Override
                          public void onSuccess(Boolean result) {
                              System.out.println("Citizen successfully saved");
  
                          }
  
                      });
  
                      service.saveCountry(country, new AsyncCallback&lt;Boolean&gt;(){
  
                          @Override
                          public void onFailure(Throwable caught) {
                              System.out.println("Problem saving country");
  
                          }
  
                          @Override
                          public void onSuccess(Boolean result) {
                              System.out.println("Country successfully saved");
  
                          }
                      });
  
  ...
  </code></pre>
  
  <p>-- service provides simple GWT-RPC call to server</p>
  
  <p>Service on server:</p>
  
  <pre><code>@Service("componentService")
  public class ComponentServiceImpl implements ComponentService{
  
      @Autowired
      private CountryDAO daoCnt;
  
      @Autowired
      private CitizenDAO daoCtz;
  
      @Transactional(readOnly=false)
      @Override
      public boolean saveCitizen(Citizen citizen) {
          daoCtz.saveOrUpdate(citizen);
          return true;
      }
  
      @Transactional(readOnly=false)
      @Override
      public boolean saveCountry(Country country) {
          daoCnt.saveOrUpdate(country);
          return true;
      }
  }
  </code></pre>
  
  <p>Now SpringDAOs:</p>
  
  <p>CitizenDAO:</p>
  
  <pre><code>@Repository
  public class CitizenDAO {
  
  ...
      public void saveOrUpdate(Citizen citizen){
          sessionFactory.getCurrentSession().saveOrUpdate(citizen);
      }
  ...
  </code></pre>
  
  <p>CountryDAO:</p>
  
  <pre><code>@Repository
  public class CountryDAO {
  
  ...
      public void saveOrUpdate(Country country){
          sessionFactory.getCurrentSession().saveOrUpdate(country);
      }
  ...
  </code></pre>
  
  <p>Finally </p>
  
  <p>Citizen.hbm.xml:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="sk.jakub.mod.shared.model.Citizen" table="citizen" catalog="modeldb"&gt;
          &lt;id name="id" type="java.lang.Integer"&gt;
              &lt;column name="id" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;many-to-one name="country" class="sk.jakub.mod.shared.model.Country" fetch="select"&gt;
              &lt;column name="Country_id" not-null="true" /&gt;
          &lt;/many-to-one&gt;
          &lt;property name="name" type="string"&gt;
              &lt;column name="name" length="45" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;property name="surname" type="string"&gt;
              &lt;column name="surname" length="45" not-null="true" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Country.hbm.xml:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="sk.jakub.mod.shared.model.Country" table="country" catalog="modeldb"&gt;
          &lt;id name="id" type="java.lang.Integer"&gt;
              &lt;column name="id" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;property name="name" type="string"&gt;
              &lt;column name="name" length="45" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;property name="population" type="int"&gt;
              &lt;column name="population" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;set name="citizens" table="citizen" inverse="true" lazy="true" fetch="select"&gt;
              &lt;key&gt;
                  &lt;column name="Country_id" not-null="true" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="sk.jakub.mod.shared.model.Citizen" /&gt;
          &lt;/set&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>I havent listed Citizen.java and Country.java because they are only basic POJOs (if necessary I'll provide them). </p>
  
  <p>When I launch my app and I want to save my data into database I obtain following error:</p>
  
  <pre><code>org.hibernate.PropertyValueException: not-null property references a null or transient value: sk.jakub.mod.shared.model.Citizen.country 
  </code></pre>
  
  <p>I can't figure out where is the problem. I was trying also instead of saveOrUpdate method, persist method. Or also to change the order of saving into database. Nothing seemed to work.</p>
  
  <p>Thank you very much for help :) If needed, I can post more code from my application.</p>
  
  <p>EDIT:
  code for Citizen.java:</p>
  
  <pre><code>public class Citizen implements java.io.Serializable {
  
      private static final long serialVersionUID = -3102863479088406293L;
      private Integer id;
      private Country country;
      private String name;
      private String surname;
  
      public Citizen() {
      }
  
      public Citizen(Country country, String name, String surname) {
          this.country = country;
          this.name = name;
          this.surname = surname;
      }
  
      public Integer getId() {
          return this.id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public Stat getCountry() {
          return this.country;
      }
  
      public void setCountry(Country country) {
          this.country = country;
      }
  
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getSurname() {
          return this.surname;
      }
  
      public void setSurname(String surname) {
          this.surname = surname;
      }
  }
  </code></pre>
  
  <p>Country.java:</p>
  
  <pre><code>public class Country implements java.io.Serializable {
  
      private static final long serialVersionUID = -4085805854508658303L;
      private Integer id;
      private String name;
      private int population;
      private Set&lt;Citizen&gt; citizens = new HashSet&lt;Citizen&gt;();
  
      public Country() {
      }
  
      public Country(String name, int population) {
          this.name = name;
          this.population = population;
      }
  
      public Country(String name, int population, Set&lt;Citizen&gt; citizens) {
          this.name = name;
          this.population = population;
          this.citizens = citizens;
      }
  
      public Integer getId() {
          return this.id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getPopulation() {
          return this.population;
      }
  
      public void setPopulation(int population) {
          this.population = population;
      }
  
      public Set&lt;Citizen&gt; getCitizens() {
          return this.citizens;
      }
  
      public void setCitizens(Set&lt;Citizen&gt; citizens) {
          this.citizens = citizens;
      }
  }
  </code></pre>
  
  <p>Furthermore, I've checked the database manually and Country is saved but citizen is not.</p>