Persisting with Foreign Keys
We are going to use the Film and Language tables and entities for this example.

We have previously configured a @ManyToOne relationship between Language and Film. Looking at the tables we can see that a Film has a not null foreign key column called language_id.
To execute an INSERT on the Film table, we have to provide a value for that column or we will get a Foreign Key Constraint.
INSERT into Film (title, description, language_id) VALUES ("Something", "A description", 1);
As we know by now, JPA deals in Objects not columns. When creating a new Film we can use the setters to add a title and description, but what about the language_id?
When we created the relationship between Film and Language we added a field of type Language to the Film class.
@ManyToOne
@JoinColumn(name = "language_id")
private Language language;
This field represents the language_id and needs to be set before we can persist properly.
In order to set the foreign key and establish the appropriate relationship in SQL, we need to set the address field with the Managed Entity representing Address with id 1. The example below shows how we would accomplish this.
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("VideoStore");
EntityManager em = emf.createEntityManager();
Film film = new Film();
film.setTitle("Something");
film.setDescription("A description");
Language lang = em.find(Language.class,1);
film.setLanguage(lang); // sets the foreign key language_id to 1 when persisted
em.getTransaction().begin();
em.persist(film);
em.flush();
em.getTransaction().commit();