Hibernate: More than one row with the given identifier was found error

前端 未结 10 863
有刺的猬
有刺的猬 2020-12-01 01:20

I\'m using spring 4.0.5 and hibernate 4.3.5; I\'m facing an error with hibernate and I can\'t figure where I\'m wrong (because I\'m sure I\'m wrong). I have a table related

相关标签:
10条回答
  • 2020-12-01 01:50

    I had the same issue while updating the data. I solved the problem by passing the parent object with its primary key. All child object's primary key was set to null. This worked for me. Neither of above solution worked in my case.

    0 讨论(0)
  • 2020-12-01 01:54

    I had the same problem and I also change the mapping from OneToOne mapping to OneToMany mapping. Previously, I have defined the mapping as OneToOne mistakenly. Now it's working properly.

    Old

    @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
        @JoinColumn(name="appointmentId", nullable=false, insertable=false, updatable=false)
        private Appointment appointment;
    

    Current

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
        @JoinColumn(name="appointmentId", nullable=false, insertable=false, updatable=false)
        private Appointment appointment;
    
    0 讨论(0)
  • 2020-12-01 01:54

    Try to check the Entity relation @OneToOne, in your case check the WcmDomain entity, it will contain two records referring to WebTree. so once you load the WebTree by findById it will fail, as it is doing internal join and the final result will be two records.

    So delete that extra record from the WcmDomain, it will work

    0 讨论(0)
  • 2020-12-01 01:59

    Find in the hibernate-mapping the relationship one-to-one, after that, find in the database in the associated table, is possible that the relationship table get more than one rows

    0 讨论(0)
  • 2020-12-01 02:00

    I'm sorry; it was my mistake......in my entity class I wrongly mapped a relation as oneToOne; it was, instead, oneToMany :) Now all works pretty good.....; this is my new entity class:

    @DynamicUpdate
    @Cache(region = "it.eng.angelo.spring.dao.hibernate.models.MediaGalleryTree", usage = CacheConcurrencyStrategy.READ_WRITE)
    @Entity
    @Table(name = "MEDIA_GALL_TREE", indexes = {@Index(name = "NOME_FOLDER_IDX", columnList = "NOME_FOLDER")})
    public class MediaGalleryTree extends AbstractModel
    {
    
        private static final long serialVersionUID = -4572195412018767502L;
        private long id;
        private String text;
        private boolean opened;
        private boolean disabled;
        private boolean selected;
        private Set<MediaGalleryTree> children = new HashSet<MediaGalleryTree>(0);
        private Set<FedoraCommonsEntity> media = new HashSet<FedoraCommonsEntity>(0);
        private MediaGalleryTree father;
        private WcmDomain dominio;
        public MediaGalleryTree()
        {
            super();
        }
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "ID_FOLDER", unique = true, nullable = false)
        public long getId()
        {
            return id;
        }
        public void setId(long id)
        {
            this.id = id;
        }
        @Column(name = "NOME_FOLDER", nullable = false, unique=false)
        public String getText()
        {
            return text;
        }
        public void setText(String text)
        {
            this.text = text;
        }
        @Column(name = "OPENED_FOLDER")
        public boolean isOpened()
        {
            return opened;
        }
        public void setOpened(boolean opened)
        {
            this.opened = opened;
        }
        @Column(name = "DISABLED_FOLDER")
        public boolean isDisabled()
        {
            return disabled;
        }
        public void setDisabled(boolean disabled)
        {
            this.disabled = disabled;
        }
        @Column(name = "SELECTED_FOLDER")
        public boolean isSelected()
        {
            return selected;
        }
        public void setSelected(boolean selected)
        {
            this.selected = selected;
        }
        @OneToMany( mappedBy = "father", orphanRemoval = true, 
                    targetEntity = MediaGalleryTree.class)
        public Set<MediaGalleryTree> getChildren()
        {
            return children;
        }
        public void setChildren(Set<MediaGalleryTree> children)
        {
            this.children = children;
        }
        @ManyToOne(targetEntity = MediaGalleryTree.class)
        @JoinColumn(name = "ID_PADRE", nullable = true)
        public MediaGalleryTree getFather()
        {
            return father;
        }
        public void setFather(MediaGalleryTree father)
        {
            this.father = father;
        }
        @ManyToOne(targetEntity = WcmDomain.class, cascade={CascadeType.ALL})
        @JoinColumn(name="ID_DOMINIO", nullable=false)
        public WcmDomain getDominio()
        {
            return dominio;
        }
        public void setDominio(WcmDomain dominio)
        {
            this.dominio = dominio;
        }
        @OneToMany( mappedBy = "folder", orphanRemoval = true, 
                targetEntity = Media.class, cascade = { CascadeType.ALL })
        public Set<FedoraCommonsEntity> getMedia()
        {
            return media;
        }
        public void setMedia(Set<FedoraCommonsEntity> media)
        {
            this.media = media;
        }
    
    }
    

    Angelo

    0 讨论(0)
  • 2020-12-01 02:03

    If you have @OneToOne mapping in your class then update the fetch type to LAZY. Because by default fetch type for OneToOne is EAGER so when we fetch object of main class (WebTree) class, it will fetch the object for OneToOne mapped class so when hibernate map the object it will throw the exception. In Short if you have OneToOne Mapping then replace it with @OneToOne(fetch = FetchType.LAZY).

    0 讨论(0)
提交回复
热议问题