0

I am farily new to Hibernate and here i am stuck with a transientexception. Please help.

Exception occured while saving the object Location object references an unsaved--transient instance - save the transient instance before flushing: <City object>; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

I am getting above exeption when i am trying to save data into my location table which has a City object with all its elements referring to null.

Location object       
   private String LocationAddress;  
   @ManyToOne
   @JoinColumn(name = "CITYID")
   private City city;


City
   private String cityName;
   private int cityId;

In Location table CityID is nullable field because city is not a required field. And we have a city table which is a static table so we dont want to insert data in City table. Now when we are storing Location object. It stores data fine if City object has some values like some city. But throws exception when object contains a blank object(cityName and cityId are null)

Please help.

Example scenario

Successful
      Location 
       locationAddress = "test Address"
        city 
           cityId="1"
           cityName="testCity"

Failure
      Location 
           locationAddress = "test Address"
           city 
               cityId=null
               cityName=null
1

2 Answers 2

0

It's hard to say for sure without seeing your code, but it sounds like you are not saving your City before saving your Location class. You have two options for persisting:

You can use the @Cascade annotation to have the insert cascade from the parent to the child. Doing this, you can just persist the Location class and Hibernate will take care of the rest.

@ManyToOne
@JoinColumn(name = "CITYID")
@Cascade(CascadeType.PERSIST)  //Or ALL if you want Updates, Deletes to go to
private City city;

More on @Cascade here.

If you don't want to do that, you will need to explicitly persist the City class before saving the Location.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I missed the part about where you are trying to store a City class with null identifiers. @Yair is right, you are going to need to set city to null. If you don't want to do that programmatically, you can do it with a @PrePersist annotation on a method in Location like: @PrePersist public void prePersist() { if(city.cityId == null) city = null; }. Then it should save, and as long as the city_id column is in the Location table, Hibernate will just ignore it when it loads that object again.
0

If you're trying to save a Location instance that actually refers no City, then you should have Location.City refer to null, rather than creating a City instance of which fields are null.

(a City instance of which fields are null is regarded by Hibernate as if you want a new record in cities table of which columns values are null - which doesn't make much sense)

2 Comments

@ yair, City object sometimes have value and sometime not and this is govern by JSF beans so i am not able to make it null until and unless i put a if check..... However making it null is also not solving the problem... when i make it null hibernate start looking for city object.
@user1422423 if Location.City is null, Hibernate has no object to look for... Unless you're doing something peculiar when you're saving. Can you show how do you save the Location object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.