Hibernate Update table with foreign key

ぐ巨炮叔叔 提交于 2019-12-11 17:29:25

问题


Am new to hibernate. Am trying to update a table and ended up getting java.sql.SQLIntegrityConstraintViolationException: Duplicate entry for key 'PRIMARY' exception

Am using entityManager.merge() method to update the table.
Below are the table relationships

TableA( 
@ID 
int col A , 
String colB....)

TableB( 
@ID 
int col X , 
@ManyToOne 
@Id 
@JoinColumn(name = "colA",referencedColumnName = "colA") 
TableA tableA ....)

I have used Entitymanager.persist() to insert data in both the tables. Now i want to update the data in TableB. Below is my method for the same

public void update(List<TableBObj> TableBObjList){
    for(TableBObj tableBObj: TableBObjList){
            em.merge(tableBObj);
        }
    }
}

Currently getting "Duplicate entry for key". Seeing the logs merge method isn't running update query but its running insert query instead. Any help appreciated?

来源:https://stackoverflow.com/questions/56797162/hibernate-update-table-with-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!