Note that there are some explanatory texts on larger screens.

plurals
  1. POGWT/RequestFactory/JPA foreign key not merged automatically
    primarykey
    data
    text
    <p>I have the 2 following entities:</p> <pre><code>@Entity @Table(name = "brand") public class Brand { @Id @Column(name = "id") private Integer id; @Column(name = "name") private String name; } </code></pre> <p>and </p> <pre><code>@Entity @Table(name = "car") public class Car { @Id @Column(name = "id") private Integer id; @Version @Column(name = "version") private Integer version; @ManyToOne(optional=false) @JoinColumn(name = "brand_id", referencedColumnName = "id") private Brand brand; @Column(name = "otherproperty") private String otherProperty; } </code></pre> <p>Basically the brand will be a predefined list that <em>should</em> never change, chosen in the GUI from a drop-down list. However, I'd like to be able to change the brand of my car as any other property.</p> <p>Here is the method in my DAO:</p> <pre><code>public void updateCar(Car update) throws Exception { EntityManager em = entityManager(); try { em.getTransaction().begin(); em.merge(update); em.getTransaction().commit(); } finally { em.close(); } } </code></pre> <p>The DAO works fine to update <code>otherProperty</code>, but if I provide another brand as child of the <code>Car update</code> parameter, it won't update the foreign key to the new brand.</p> <p>I've tried using the <code>CascadeType</code> properties, but it wants to update the brand table, which I don't want.</p> <p>Any help would be appreciated. Thanks!</p> <hr> <p>EDIT: OK I've found a solution, but it looks over complicated to me.</p> <p>If I add 2 lines in the DAO code in order to explicitly look for the brand to update, it works:</p> <pre><code>public void updateCar(Car update) throws Exception { EntityManager em = entityManager(); try { em.getTransaction().begin(); Brand lookedUp = findBrandById(update.getBrand().getId()); update.setBrand(lookedUp); em.merge(update); em.getTransaction().commit(); } finally { em.close(); } } </code></pre> <p>However I'm working with GWT/RequestFactory, and have implemented a <code>Locator</code> for my <code>Brand</code> and one for my <code>Car</code>, so I would assume the <code>Locator</code> would take care of that business for me.</p> <p>Am I missing something?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload