<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Persistence Object is not saved - ForeignKey is Null.</title><link>https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null</link><description>&lt;p&gt;We have two persistence objects where a hasMany b. So a is a property of b.&lt;/p&gt;
&lt;p&gt;Now we are creating an object of type b and asign the corresponding a-object to b. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;b.a = a;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we try to save the newly created b object via:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ivy.persistence.persistenceUnit.merge(b);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then it throws an error that the ForeignKey of a in table b cannot be null. But in the debugger object a is assigned correctly to object b.&lt;/p&gt;
&lt;p&gt;In which pitfall did I run here?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 30 Apr 2014 09:30:42 -0400</lastBuildDate><item><title>Answer by Nikel Weis</title><link>https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null/671</link><description>&lt;p&gt;Hmm... ok it was indeed an Xpert.ivy related pitfall. Kind of embarrassing but I want to avoid anyone else spends time on that. It is possible to set "not insertable" and "not updatable" in the Data Class definition. AND THAT'S WHAT HAPPENS INDEED! No records can be inserted or updated from the program logic layer. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nikel Weis</dc:creator><pubDate>Wed, 30 Apr 2014 09:30:42 -0400</pubDate><guid>https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null/671</guid></item><item><title>Comment by Nikel Weis on Nikel Weis's question</title><link>https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null#669</link><description>&lt;p&gt;Thanks for your reply. A already exists in the database. The a-object is determined by object b which contains the id of a (contents of object b are from a third-party system and the object shall be created in the database). With that id given object a is fetched from the database. So that should actually not be the problem.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nikel Weis</dc:creator><pubDate>Tue, 29 Apr 2014 13:39:44 -0400</pubDate><guid>https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null#669</guid></item><item><title>Comment by Daniel Oechslin on Nikel Weis's question</title><link>https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null#668</link><description>&lt;p&gt;I think your problem is hibernate related. I guess you did not save 'a' first. so there is no actual (database wise) connection to b. But to be able to save 'b' with a reference to 'a' you must save 'a' first.
So if you do save 'a' first and then execute your line it should be fine.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Tue, 29 Apr 2014 13:37:05 -0400</pubDate><guid>https://answers.axonivy.com/questions/667/persistence-object-is-not-saved-foreignkey-is-null#668</guid></item></channel></rss>