tags:

views:

295

answers:

1

I have these 2 tables:

T1: County- Name - FK_Country
T2: Country - Name 

And when I want to save a County, I want to choose a default Country, but I receive this error: object is an unsaved transient instance - save the transient instance before merging: local.Country; nested exception is org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging: local.County

Java Code

Country country = new Country("USA");

localizationService.saveCountry(country);
localizationService.saveCounty(getForm().getModelObject());
A: 

I see two problems here:

  1. You are creating a new Country for each save.
  2. You are not associating the Country object with County before saving.

I would try loading a country from the database. That is, instead of doing:

Country country = new Country("USA");

Do something along the lines of:

Country country = localizationService.findByName("USA");
// My assumption on your model here
County county = getForm().getModelObject();
county.setCountry(country);
localizationService.saveCounty(county);
Jack Leow
Yes your solution is right!
cc
Ok, the problem was: I dind't knew that the Version column for my table shouldn't be null. I inserted a bunch of info into the tables, leaving the Version column null (this column apparently it's used for JPA). Any, thaks again for your time to answer.
cc