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)

In the Application entity class I have created a List of Education with the relationship ONE_TO_MANY. Mapped by attribute is appl. In the Education entity class i have created a field appl with the relationship MANY_TO_ONE.

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.

Is there an other way to save my data to a database?

thanks.

asked 20.04.2016 at 16:25

joha0123's gravatar image

joha0123
(suspended)
accept rate: 0%

edited 23.04.2016 at 11:34


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.

Education.setAppl(Application)

I've created a similar datastructure and a demo project here: simpleJpa_51.iar

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();
        }
    }
}
link

answered 22.04.2016 at 11:41

Reguel%20Wermelinger's gravatar image

Reguel Werme... ♦♦
9.4k31958
accept rate: 70%

edited 07.03.2018 at 10:38

Alex%20Suter's gravatar image

Alex Suter ♦♦
3.1k122247

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×147
×32
×9
×5

Asked: 20.04.2016 at 16:25

Seen: 2,750 times

Last updated: 07.03.2018 at 10:38