Not-null property references a transient value - transient instance must be saved before current operation

Related searches

I have 2 domain models and one Spring REST Controller like below:

@Entity
public class Customer{

@Id
private Long id;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;

// other stuff with getters/setters

}

@Entity
public class Country{

@Id
@Column(name="COUNTRY_ID")
private Integer id;

// other stuff with getters/setters

}

Spring REST Controller:

@Controller
@RequestMapping("/shop/services/customers")
public class CustomerRESTController {

   /**
    * Create new customer
    */
    @RequestMapping( method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public com.salesmanager.web.entity.customer.Customer createCustomer(@Valid @RequestBody   Customer customer, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        customerService.saveOrUpdate(customer);

        return customer;
    }

    // other stuff
}

I am trying to call above REST service with below JSON as body:

{
    "firstname": "Tapas",
    "lastname": "Jena",
    "city": "Hyderabad",
    "country": "1"
}

Where country code 1 is already there in Country table. The problem is when I am calling this service getting below error:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.test.model.Customer.country -> com.test.model.Country; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.test.model.Customer.country -> com.test.model.Country

Any help will be appreciated!

Try putting CascadeType.ALL

@OneToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name="COUNTRY_ID", nullable=false) 

private Country country;

What does "org.hibernate.TransientPropertyValueException: Not , TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation"� The exception that you are getting Not-null property references a transient value transient instance must be saved before current operation means that while you are persisting dimensionDO object, there is a relation of thi object that references a non-persisted ( that is a transient) object, and thus it can not manage foreign key.

I had a similar problem. Two entities: Document and Status. Document had a relationship OneToMany with Status, that represented the history of Status the Document had.

So, there was a @NotNull @ManyToOne reference of Document inside Status.

Also, I needed to know the actual Status of Document. So, I needed another relationship, this time @OneToOne, also @NotNull, inside Document.

The problem was: how can I persist both entities the first time if both had a @NotNull reference to the other?

The solution was: remove @NotNull reference from actualStatus reference. This way, it was able to persist both entities.

A fix for org hibernate TransientPropertyValueException in Spring Boot, If you define the JPA annotations correctly, that is. TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing means, nor what is a transient property. us "object references an unsaved transient instance" and that we should save the� Not-null property references a transient value - transient instance must be saved before current operation 2 Not-null property references a transient value transient instance must be saved before current operation

I got same error and this is how I solved it:

1st Entity:

    @Entity
    public class Person implements Serializable{
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int personId;
        private String name;
        private String email;
        private long phoneNumber;
        private String password;
        private String userType;
        @OneToOne(fetch = FetchType.LAZY, mappedBy = "personCustomer", cascade 
        = CascadeType.ALL)
        private Customer customer;

2nd Entity:

@Entity
public class Customer implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int customerId;
    @OneToOne(fetch = FetchType.LAZY, optional = false, cascade = 
    CascadeType.ALL)
    @JoinColumn(name = "person_customer")
    @JsonIgnore
    private Person personCustomer;

My Controller:

@PostMapping("/customer/registration")
    public PersonCustomer addCustomer(@RequestBody Person person)
    {
        Customer customer = new Customer(person);
        person.setCustomer(customer);
        Customer cust = customerRepo.save(customer);
        logger.info("{}", cust);
        Optional<Person> person_Cust = 
        personRepo.findById(cust.getPersonCustomer().getPersonId());
        Person personNew = person_Cust.get();
        PersonCustomer personCust = new PersonCustomer();

        if(cust.equals(null))
        {   
            personCust.setStatus("FAIL");
            personCust.setMessage("Registration failed");
            personCust.setTimestamp(personCust.timeStamp());
        }
        personCust.setStatus("OK");
        personCust.setMessage("Registration OK");
        personCust.setTimestamp(personCust.timeStamp());
        personCust.setPerson(personNew);

        return personCust;
    }

The problem got solved when I added "person.setCustomer(customer);". As both POJO classes has each others reference, so we have to "set" each others reference before using the JPA repository method(customerRepo.save(customer));

org.hibernate.TransientPropertyValueException java code examples , throw new TransientPropertyValueException( "Not-null property references a transient value - transient instance must be saved before current operation",� org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: S043.entity

you should change :

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;

to :

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID")
private Country country;

just delete nullable setting.

Following exception is logged when trying to persist entity: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient

This line: "repository.Add(newAsset);" fails with a "not-null property references a null or transient value" on Content. I have tried the following: 1) Move line to after the factory create of an asset. Result = Success BUT NHibernate issues an INSERT for the Asset followed by an UPDATE for the change of name. It works, but it's inefficent

Or you can just give the property a different name. I expect that the mechanism for initializing that property is not active from the console. Separate from all of that, the dateCreated property is not bindable by default when passing a Map to the User constructor as you are. That is by design.

The "null or transient value" is the Questionnaire foreign key that is needed to insert the Page instance. The Questionnaire object is what was actually passed to the merge() method. The only reason that the Page object is being examined for a merge is that it's a child object of the Questionnaire which was passed in, so hibernate must know

Comments
  • Ok then do one thing. Your customer object has country object with id 1. But that country object is not in persistence state. First get country object by sending country id. Set the country object retrieved from database into Customer object. Then save customer object. I hope this time it won't give any error.
  • Bizarrely, CascadeType.ALL works for me when this doesn't: @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation.
  • I presented a solution to my problem which was similar. Nothing to do with a new question.
  • can you please explain how the nullable tends to be problematic in this case?