-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Violation of PRIMARY KEY constraint after migration to 3.5.6
PostPosted: Wed Jul 20, 2011 11:34 am 
Newbie

Joined: Thu Jul 14, 2011 11:36 am
Posts: 9
Hi,

I have the following POJO
Code:
@Entity @Table(name="organization")
public class Organization implements Serializable {

    private List<Organization> children = new ArrayList<Organization>();
    private List<String> parentsIds = new ArrayList<String>();

    @ElementCollection(targetClass=String.class, fetch=FetchType.LAZY)
    @JoinTable(name="organization_hierarchy",joinColumns=@JoinColumn(name="child_id"))
    @Column(name="parent_id")
    public List<String> getParentsIds() {
        return parentsIds;
    }

    @ManyToMany(cascade={}, fetch=FetchType.LAZY)
    @JoinTable(name="organization_hierarchy",
       joinColumns = {
        @JoinColumn(name="parent_id", unique=false, nullable=false, insertable=true, updatable=false) },
        inverseJoinColumns = {
        @JoinColumn(name="child_id", unique=false, nullable=false, insertable=true, updatable=false) })
    public List<Organization> getChildren() {
        return children;
    }
    ...   
}

So records in table "organization" has tree hierarchy using table "organization_hierarchy"

Before migration to 3.6.1 and annotations, the following code (adding new Organization to the hierarchy) worked fine:
Code:
child.getParentsIds().add(parent.getId());
parent.getChildren().add(child);


But now it throws java.sql.SQLException Violation of PRIMARY KEY constraint 'PK_organization_hierarchy'. Cannot insert duplicate key in object 'dbo.organization_hierarchy'.

As far as I understand, it tries to insert 2 records into organization_hierarchy with the same mapping (one for child.getParentsIds().add(parent.getId()) and one for parent.getChildren().add(child))

Removing child.getParentsIds().add(parent.getId() resolves the issue, but there are many places in code with the same operations and also there are some places, when size/content of child.getParentsIds() is checked after such adding. So commenting child.getParentsIds().add(parent.getId()) out requires re-factoring all related code.

It is possible to prevent insertion the second record using annotations? (how to make child.getParentsIds() read-only) ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.