<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Persistence Api - Foreign Key is always null</title><link>https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null</link><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;currently i'm trying to use the persistence api.
I stuck at inserting a one to many relationship. The problem is that the foreign key is always null.&lt;/p&gt;
&lt;p&gt;I have two entity classes. User and Option.&lt;/p&gt;
&lt;h3&gt;User&lt;/h3&gt;

&lt;table border="1"&gt;
  &lt;tbody&gt;&lt;tr&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Type&lt;/th&gt;
    &lt;th&gt;DB Field&lt;/th&gt;
    &lt;th&gt;Persistent&lt;/th&gt;
    &lt;th&gt;Length&lt;/th&gt;
    &lt;th&gt;Properties&lt;/th&gt;
    &lt;th&gt;Association&lt;/th&gt;
    &lt;th&gt;Cascade&lt;/th&gt;
    &lt;th&gt;Mapped by&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;id&lt;/td&gt;
    &lt;td&gt;Integer&lt;/td&gt;
    &lt;td&gt;id&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;id, generated&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;name&lt;/td&gt;
    &lt;td&gt;String&lt;/td&gt;
    &lt;td&gt;name&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;255&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;opts&lt;/td&gt;
    &lt;td&gt;Listy&amp;lt;Option&amp;gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;ONE_TO_MANY&lt;/td&gt;
    &lt;td&gt;persist, merge&lt;/td&gt;
    &lt;td&gt;user_fk&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;h3&gt;Option&lt;/h3&gt;

&lt;table border="1"&gt;
  &lt;tbody&gt;&lt;tr&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Type&lt;/th&gt;
    &lt;th&gt;DB Field&lt;/th&gt;
    &lt;th&gt;Persistent&lt;/th&gt;
    &lt;th&gt;Length&lt;/th&gt;
    &lt;th&gt;Properties&lt;/th&gt;
    &lt;th&gt;Association&lt;/th&gt;
    &lt;th&gt;Cascade&lt;/th&gt;
    &lt;th&gt;Mapped by&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;id&lt;/td&gt;
    &lt;td&gt;Integer&lt;/td&gt;
    &lt;td&gt;id&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;id, generated&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;option&lt;/td&gt;
    &lt;td&gt;String&lt;/td&gt;
    &lt;td&gt;opt_value&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;255&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;user_fk&lt;/td&gt;
    &lt;td&gt;User&lt;/td&gt;
    &lt;td&gt;user_fk&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;not nullable&lt;/td&gt;
    &lt;td&gt;MANY_TO_ONE&lt;/td&gt;
    &lt;td&gt;persist, merge&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;The Option Objects are created from strings by a converter from a p:selectManyCheckbox. The field user_fk is not set manually in this converter because i don't have a reference to the User object in the converter.&lt;/p&gt;
&lt;p&gt;Xpert.ivy creates an entity class for User and adds a "setOpts(List)" method for me.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;
 public void setOpts(java.util.List&amp;lt; persistenceTest.Option &amp;gt; _opts)
  {
    opts = _opts;
  }
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If there where a "addOpt(Option) method I could add the reference to User myself.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;
 public void addOpt(persistenceTest.Option __opts)
  {
&lt;strong&gt;    _opts.setUser_fk(this);&lt;/strong&gt;
    optsList.add(_opts);
  }
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Am I thinking wrong or is it not possible to automate the creation of foreign keys?&lt;/p&gt;
&lt;p&gt;Best Regards
Harald&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 02 Apr 2014 12:37:37 -0400</lastBuildDate><item><title>Comment by HaraldWeber on HaraldWeber's answer</title><link>https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null#615</link><description>&lt;p&gt;Now I set the reference of User ins Option myself and it is working now.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">HaraldWeber</dc:creator><pubDate>Wed, 02 Apr 2014 12:37:37 -0400</pubDate><guid>https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null#615</guid></item><item><title>Answer by HaraldWeber</title><link>https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null/611</link><description>&lt;p&gt;I used a script step and called ivy.persistence.persistence.persist(in.user);&lt;/p&gt;
&lt;p&gt;My database looks like this:
&lt;code&gt;
CREATE TABLE [dbo].&lt;a href="[id]%20[int]%20IDENTITY(1,1)%20NOT%20NULL,
%20%20%20%20[email]%20[varchar](255)%20NULL,
%20%20%20%20[name]%20[varchar](255)%20NULL,
%20%20%20%20[password]%20[varchar](255)%20NULL,
PRIMARY%20KEY%20CLUSTERED%20
(
%20%20%20%20[id]%20ASC
)WITH%20(PAD_INDEX%20%20=%20OFF,%20STATISTICS_NORECOMPUTE%20%20=%20OFF,%20IGNORE_DUP_KEY%20=%20OFF,%20ALLOW_ROW_LOCKS%20%20=%20ON,%20ALLOW_PAGE_LOCKS%20%20=%20ON)%20ON%20[PRIMARY]"&gt;users&lt;/a&gt; ON [PRIMARY]
GO&lt;/code&gt;&lt;/p&gt;&lt;code&gt;
&lt;p&gt;CREATE TABLE [dbo].&lt;a href="[id]%20[int]%20IDENTITY(1,1)%20NOT%20NULL,
%20%20%20%20[opt_value]%20[varchar](255)%20NULL,
%20%20%20%20[user_fk_id]%20[int]%20NOT%20NULL,
PRIMARY%20KEY%20CLUSTERED%20
(
%20%20%20%20[id]%20ASC
)WITH%20(PAD_INDEX%20%20=%20OFF,%20STATISTICS_NORECOMPUTE%20%20=%20OFF,%20IGNORE_DUP_KEY%20=%20OFF,%20ALLOW_ROW_LOCKS%20%20=%20ON,%20ALLOW_PAGE_LOCKS%20%20=%20ON)%20ON%20[PRIMARY]"&gt;opts&lt;/a&gt; ON [PRIMARY]
GO&lt;/p&gt;
&lt;/code&gt;&lt;p&gt;&lt;code&gt;ALTER TABLE [dbo].[opts]  WITH CHECK ADD  CONSTRAINT [FK34282079933D49] FOREIGN KEY([user_fk_id])
REFERENCES [dbo].[users] ([id])
GO
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I think the problem is how I create new Items for the opts list in the user class.
In the view i have a selectManyCheckbox:
&lt;code&gt;
&amp;lt;p:selectManyCheckbox id="select" value="#{data.user.opts}" &amp;gt;
  &amp;lt;f:selectItem itemLabel="Option 1" itemValue="Option 1" /&amp;gt;
  &amp;lt;f:selectItem itemLabel="Option 2" itemValue="Option 2" /&amp;gt;
  &amp;lt;f:converter converterId="conv.OptionConverter" /&amp;gt;
&amp;lt;/p:selectManyCheckbox&amp;gt;
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The con.OptionConverter creates one option object from the given string.
&lt;code&gt;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
    Option option = new Option();
    option.setOption(arg2);
    return option;
}
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;There should also be something like 
&lt;strong&gt;option.setUser_fk(user);&lt;/strong&gt;
 so the reference from User to Option is set.
This is not possible in the converter because there is no reference to the User object.
The only way to set the reference of User in Option is in the User Entity Class where the list is set (or built up). But the generated code only sets the list with empty references to User. So the persistence api can't know their connection.
Maybe I'm thinking wrong and build the example wrong but without the references set the persistence api will never set the keys.&lt;/p&gt;
&lt;p&gt;Best Regards
Harald&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">HaraldWeber</dc:creator><pubDate>Mon, 31 Mar 2014 09:19:48 -0400</pubDate><guid>https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null/611</guid></item><item><title>Answer by roland</title><link>https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null/593</link><description>&lt;p&gt;Hi Harald,&lt;/p&gt;
&lt;p&gt;did you try to persist the dataclass?
ivy.persistence.xyz.persist(User);&lt;/p&gt;
&lt;p&gt;as i know the keys are set within the database server.&lt;/p&gt;
&lt;p&gt;in our case we used MSSQL and the keys are set by MSSQL table settings.&lt;/p&gt;
&lt;p&gt;Best regards 
Roland&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">roland</dc:creator><pubDate>Thu, 27 Mar 2014 20:13:09 -0400</pubDate><guid>https://answers.axonivy.com/questions/588/persistence-api-foreign-key-is-always-null/593</guid></item></channel></rss>