<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How can I add a List to a database</title><link>https://answers.axonivy.com/questions/1740/how-can-i-add-a-list-to-a-database</link><description>&lt;p&gt;I am filling a List and then I try to write it to the database. 
We have two Entity Classes: Application and Education. The relationship is 1:N. (One application can have many educations)&lt;/p&gt;
&lt;p&gt;In the Application entity class I have created a List of Education with the relationship ONE_TO_MANY. Mapped by attribute is &lt;strong&gt;appl&lt;/strong&gt;.
In the Education entity class i have created a field &lt;strong&gt;appl&lt;/strong&gt; with the relationship MANY_TO_ONE.&lt;/p&gt;
&lt;p&gt;The Problem is, that i have to initialize all objects before i can write data into them.
this is impossible to do, because the application Object has a Education object in it and the Education Object has a Application object in it.&lt;/p&gt;
&lt;p&gt;Is there an other way to save my data to a database?&lt;/p&gt;
&lt;p&gt;thanks.&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1740/how-can-i-add-a-list-to-a-database" rel="self"></atom:link><language>en</language><lastBuildDate>Fri, 22 Apr 2016 11:41:42 -0400</lastBuildDate><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/1740/how-can-i-add-a-list-to-a-database/1744</link><description>&lt;p&gt;You can set the relation if you assign appl to eduction and then persist em. You may have to persist them within a single transaction.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Education.setAppl(Application)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I've created a similar datastructure and a demo project here: &lt;a href="/upfiles/simpleJpa_51.iar"&gt;simpleJpa_51.iar&lt;/a&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;package ch.ivyteam.demo;

import javax.persistence.EntityManager;

import simpleJpa.Person;
import simpleJpa.Phone;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.process.data.persistence.IIvyEntityManager;

public class PersonDao {

    public static void writeDemoFixture()
    {
        Person rew = new Person();
        rew.setFirstname("Reguel");
        rew.setLastname("Wermelinger");

        Phone work = new Phone();
        work.setNumber("079 123 456");
        work.setType("work");
        work.setPerson(rew);

        Phone home = new Phone();
        home.setNumber("041 123 455");
        home.setType("home");
        home.setPerson(rew);

        persist(work, home, rew);
    }

    private static void persist(Object... objects)
    {
        IIvyEntityManager ivyEntityManager = Ivy.persistence().get("memory");
        EntityManager em = ivyEntityManager.createEntityManager();
        try{
            em.getTransaction().begin();
            for(Object obj : objects)
            {
                em.persist(obj);
            }
            em.getTransaction().commit();
        }
        finally
        {
            em.close();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Fri, 22 Apr 2016 11:41:42 -0400</pubDate><guid>https://answers.axonivy.com/questions/1740/how-can-i-add-a-list-to-a-database/1744</guid></item></channel></rss>