Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate insert twice with same PK
    primarykey
    data
    text
    <p>I have a one-table-per-subclass setup. I have a Model class which is mapped with the CarModelDesc table. This table has 3 columns : <code>(modelDescId, makeId, model)</code>.</p> <p>We are working on a new i18n system so I made a table catalog_model_i18n that has 3 column <code>(model_id, locale, model, display_name)</code>. DisplayName is new, but catalog_model_i18n.model will replace the column CarModelDesc .model later on. These are the columns definition in my model : </p> <pre><code>@Id @GeneratedValue @Column(name = Cols.modelId) private int modelId = -1; @Column(name = Cols.makeId) private Integer makeId = null; @Column(name = Cols.model) private String model = null; @ElementCollection @MapKeyClass(value = java.util.Locale.class) @MapKeyColumn(name = "locale") @CollectionTable(name = "catalog_model_i18n", joinColumns = @JoinColumn(name = "model_id")) @Column(name = "model") private Map&lt;Locale, String&gt; models = new HashMap&lt;&gt;(); @ElementCollection @MapKeyClass(value = java.util.Locale.class) @MapKeyColumn(name = "locale") @CollectionTable(name = "catalog_model_i18n", joinColumns = @JoinColumn(name = "model_id")) @Column(name = "display_name") private Map&lt;Locale, String&gt; displayNames = new HashMap&lt;&gt;(); </code></pre> <p>Each line in the table catalog_model_i18n is a translated entry. The primary key is based on the <code>model_is, locale</code> combination. When I try to save a model into the database, hibernates gives me a <code>Duplicate entry</code> exception. </p> <p>I enabled logging to see these queries being performed : </p> <pre><code>3 Query insert into sm360_webauto.CarModelDesc (makeId, model) values (45, 'modele fr') 3 Query insert into catalog_model_i18n (model_id, locale, display_name) values (884, 'fr', 'nom') 3 Query insert into catalog_model_i18n (model_id, locale, display_name) values (884, 'en', 'name') 3 Query insert into catalog_model_i18n (model_id, locale, model) values (884, 'fr', 'modele fr') 3 Query insert into catalog_model_i18n (model_id, locale, model) values (884, 'en', 'modele en') 3 Query rollback </code></pre> <p>What I expect hibernate to do in this context is whether to insert once, or to update like so : </p> <pre><code>3 Query insert into sm360_webauto.CarModelDesc (makeId, model) values (45, 'modele fr') 3 Query insert into catalog_model_i18n (model_id, locale, model, display_name) values (884, 'fr', 'modele fr', 'nom') 3 Query insert into catalog_model_i18n (model_id, locale, model, display_name) values (884, 'en', 'modele en', 'name') </code></pre> <p>Or : </p> <pre><code>3 Query insert into sm360_webauto.CarModelDesc (makeId, model) values (45, 'modele fr') 3 Query insert into catalog_model_i18n (model_id, locale, display_name) values (884, 'fr', 'nom') 3 Query insert into catalog_model_i18n (model_id, locale, display_name) values (884, 'en', 'name') 3 Query update catalog_model_i18n SET model_id=884 , locale='fr' , model='modele fr' 3 Query update catalog_model_i18n SET model_id=884 , locale='en' , model='modele en' </code></pre> <p>Is there anyway I can tell hibernate on how to do it properly? Possibly without having to write SQL code into my model.</p> <p>Thanks</p> <p>*<em>UPDATE : *</em></p> <p>The problem is that hibernate do not know theses two variables in my Model class link to the same table. Therefore, it performs two insert statements, but it is wrong. </p> <p>Until now this setup was working because I only had one column per i18n table which never caused a problem. But with 2 columns, hibernate does two insert request and a duplicate entry exception is raised. I still don't have a solution.</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.
 

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