简体   繁体   中英

Hibernate java.lang.IllegalStateException An entity copy was already assigned to a different entity when I copy database from csv

I get following problem while saving an entity to the database. I use hibernate and postgresql

java.lang.IllegalStateException: Error occurred while storing entity [chart]. An entity copy [de.enwida.web.model.user.Role#2] was already assigned to a different entity [chart].
at org.hibernate.event.internal.EventCache.put(EventCache.java:192)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:285)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:924)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:906)

What i noticed is, normally the code works if i fill DB with insert commands But it doesnt work when i fill it with COPY command such as

copy users.groups from '/home/tarazano/Downloads/server/users.groups.csv';

My code is something like this

    // Modify user's set of groups
    final Set<Group> groups = new HashSet<Group>(user.getGroups());
    groups.add(group);
    user.setGroups(groups);
    userDao.update(user, true); // with flush

I believe the problem is copy command. when i copy table content manually, something is not generated? Maybe indexes?

Please help me, i already spend alot of time on this

I still dont know why it didnt work but i found a workaround. Simply i just used createNativeQuery method entityManager

EntityManager em;
em.createNativeQuery(" INSERT INTO users.group_role(group_id, role_id) VALUES ("+group.getGroupID()+","+role.getRoleID()+")");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM