Repeated column in mapping for entity

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Repeated column in mapping for entity



When I persist a WorkAction I need to store the FK Id for WorkflowInstance


WorkAction


WorkflowInstance


WorkAction wa = new WorkAction();
wa.setWorkflowInstance(wfi);
waDao.persist(wa);



However in the table for WorkAction the fk column, workflow_instance_id winds up being null.


WorkAction


workflow_instance_id



Turns out in the Entity for WorkAction I had mapped WorkflowInstance using , insertable = false, updatable = false like this


WorkAction


WorkflowInstance


, insertable = false, updatable = false


@ManyToOne
@JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable = false, updatable = false)
private WorkflowInstance workflowInstance;



When I removed it, I got the Repeated column in mapping for entity: WorkAction column: workflow_instance_id But in the WorkAction entity I didn't see workflow_instance_id mapped again , i.e. I did not have private Long workflow_instance_id;


Repeated column in mapping for entity: WorkAction column: workflow_instance_id


WorkAction


workflow_instance_id


private Long workflow_instance_id;



But then I noticed in my WorkAction entity, I had mapped a WorkflowInstancePlayer using a formula which referenced workflow_instamce_id


WorkAction


WorkflowInstancePlayer


workflow_instamce_id


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas( @JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.role_class_id FROM WF_WORK_ACTION_CLASS a WHERE a.work_action_class_id = work_action_class_id)", referencedColumnName = "role_class_id")),
@JoinColumnOrFormula(column = @JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable=false, updatable=false)) )
private WorkflowInstancePlayer player;



Even after adding insertable=false, updatable=false to that mapping I still have the issue. When I comment out the @JoinColumnsOrFormulas I can remove the insertable=false, updatable=false on the WorkflowInstance and the workflow_instance_id is then not null but I then lose my WorkflowInstancePlayer


insertable=false, updatable=false


@JoinColumnsOrFormulas


insertable=false, updatable=false


WorkflowInstance


WorkflowInstancePlayer



How do I get around this?





Is there a specific reason to not adding private Long workflow_instance_id to WorkAction class and use it for persisting?
– Ghokun
Nov 30 '15 at 22:18





Yes, because that would also produce the Repeated column in mapping for entity error.
– jeff
Nov 30 '15 at 22:24





It would not if you add insertable=false, updatable=false to your joined properties.
– Ghokun
Nov 30 '15 at 22:27





When I simple add private Long workflow_instance_id; to Class WorkAction, right above the WorkflowInstance @manyToOne mapping, it won't compile, repeated column.
– jeff
Nov 30 '15 at 22:31





if I do @Column(insertable=false, updatable=false) private Long workflow_instance_id; it compiles but I'd image when I wa.setWorkflow_instance_id(wfi.getWorkflow_instance_id()); I'd image the persist will still produce null for the fk. I'll test right now. But where am I supposed to add insertable=false, updatable=false?
– jeff
Nov 30 '15 at 22:35




3 Answers
3



Use insertable = false, updatable = false in your @JoinColumn annotations.


insertable = false, updatable = false


@JoinColumn


public class WorkAction

@Column(name="workflow_instance_id")
private Long workflow_instance_id;

@ManyToOne
@JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable = false, updatable = false)
private WorkflowInstance workflowInstance;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas(
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.role_class_id FROM WF_WORK_ACTION_CLASS a WHERE a.work_action_class_id = work_action_class_id)", referencedColumnName = "role_class_id")),
@JoinColumnOrFormula(column = @JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable=false, updatable=false)) )
private WorkflowInstancePlayer player;



UPDATE



I reproduced the same case as you did. I think this is some kind of bug because isUpdatable and isInsertable returns true.


isUpdatable


isInsertable


protected void checkColumnDuplication(Set distinctColumns, Iterator columns)
throws MappingException
while ( columns.hasNext() )
Selectable columnOrFormula = (Selectable) columns.next();
if ( !columnOrFormula.isFormula() )
Column col = (Column) columnOrFormula;
if ( !distinctColumns.add( col.getName() ) )
throw new MappingException(
"Repeated column in mapping for entity: " +
getEntityName() +
" column: " +
col.getName() +
" (should be mapped with insert="false" update="false")"
);





protected void checkPropertyColumnDuplication(Set distinctColumns, Iterator properties)
throws MappingException
while ( properties.hasNext() )
Property prop = (Property) properties.next();
if ( prop.getValue() instanceof Component ) //TODO: remove use of instanceof!
Component component = (Component) prop.getValue();
checkPropertyColumnDuplication( distinctColumns, component.getPropertyIterator() );

else




Source code is from org.hibernate.mapping.PersistentClass


org.hibernate.mapping.PersistentClass





I appreciate you trying to help, but this produces the repeated column mapping issue. Technically, this is really no different than what I have been trying when I did not have insertable = false, updatable = false on the JoinColumn for WorkflowInstance. Something is fishy with the crazy JoinColumnsOrFormulas because I don't have this issue with other "normal" entities
– jeff
Nov 30 '15 at 23:17





I almost have same class structure but can't check right now. I will post tomorrow again if you haven't figured it out yet.
– Ghokun
Nov 30 '15 at 23:18





so what does your update mean? Am I screwed? So Hibernate is ignoring the insertable = false, updatable = false on the JoinColumnOrFormula??
– jeff
Dec 1 '15 at 14:18





insertable = false, updatable = false on JoinColumnOrFormula is somehow true everytime but not for JoinColumn alone.
– Ghokun
Dec 1 '15 at 14:41





I guess it's a bug. What version are you using. me hibernate-core: 4.3.8.Final. I have a bunch other hibernate Dependencies listed in my POM.xml which varying version numbers. I wonder if I am using the latest and greatest and whether cleaning up my dependencies would get me a version of hibernate which works with @JoinColumnOrFormula
– jeff
Dec 1 '15 at 15:38



Work Around



The only way to get this Entity to compile with a column name (workflow_instance_id) in two mappings, one being a standard @JoinColumn and the other @JoinColumn within a @JoinColumnOrFormula, and be able to persist this entity with the workflow_instance_id field populated was to keep insertable = false, updatable = false on my WorkflowInstance mapping without the formula


@ManyToOne
@JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable = false, updatable = false)
private WorkflowInstance workflowInstance;



do a normal entity manager persist on the entity


WorkAction wa = new WorkAction();
//wa.setWorkflowInstance(wfi); is ignored
wa.set other stuff;
waDao.persist(wa);



then do a NativeQueryUpdate to populate the workflow_instance_id column.


waDao.updateWfi(wa.getWfi_work_item_action_id(), wfi.getWorkflow_instance_id());


public void updateWfi(Long wfi_work_item_action_id, Long workflow_instance_id) {
Query query = em.createNativeQuery("UPDATE table_name SET workflow_instance_id = :workflow_instance_id WHERE wfi_work_item_action_id = :wfi_work_item_action_id");



The columns with insertable=false and updatable=false, has to be specified first then JoinFormula.


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas(
@JoinColumnOrFormula(column = @JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable=false, updatable=false)),
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.role_class_id FROM WF_WORK_ACTION_CLASS a WHERE a.work_action_class_id = work_action_class_id)", referencedColumnName = "role_class_id"))
)
private WorkflowInstancePlayer player;



I was also facing similar issue, now the error is gone.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS