Duplicate entry exception hibernate

此生再无相见时 提交于 2019-12-24 12:35:11

问题


I'm having some issues storing entries into my database. I am using the following heirarchy:

I have a parent class PageContent and two subclasses TextEntry and Image however when I try to store more than PageContent entity I get the following exception:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Duplicate entry '2' for key 'groups_groupId'; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Duplicate entry '2' for key 'groups_groupId'

Here are the relevant classes

Group.java:

package com.youthministry.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Entity(name="GROUP_DETAILS")
public class Group {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long groupId;
    @Column(name = "GROUP_NAME", unique = true, nullable = false)
    private String groupName;
    private String groupDesc;

    /*public String getIdAsString() {
        return new Long(groupId).toString();
    }*/
    public Long getGroupId() {
        return groupId;
    }
    public void setGroupId(Long groupId) {
        this.groupId = groupId;
    }
    public String getGroupName() {
        return groupName;
    }
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
    public String getGroupDesc() {
        return groupDesc;
    }
    public void setGroupDesc(String groupDesc) {
        this.groupDesc = groupDesc;
    }

}

PageContent.java

package com.youthministry.domain;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;

@Entity
@Inheritance (strategy=InheritanceType.JOINED)
public class PageContent {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long pageContentId;
    @Column(name="PAGE_CONTENT_NAME", nullable=false)
    private String pageContentName;
    @Column(name="LOCATION", nullable=false)
    private String location;

    @OneToMany(cascade=CascadeType.REMOVE)
    private Collection<Group> groups = new ArrayList<Group>();

    public Long getPageContentId() {
        return pageContentId;
    }
    public void setPageContentId(Long pageContentId) {
        this.pageContentId = pageContentId;
    }
    public String getPageContentName() {
        return pageContentName;
    }
    public void setPageContentName(String pageContentName) {
        this.pageContentName = pageContentName;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public Collection<Group> getGroups() {
        return groups;
    }
    public void setGroups(Collection<Group> groups) {
        this.groups = groups;
    }

}

Image.java

package com.youthministry.domain;

import javax.persistence.Entity;

@Entity
public class Image extends PageContent {
    private String pathToImage;
    private String altText;
    private String titleText;

    public String getPathToImage() {
        return pathToImage;
    }
    public void setPathToImage(String pathToImage) {
        this.pathToImage = pathToImage;
    }
    public String getAltText() {
        return altText;
    }
    public void setAltText(String altText) {
        this.altText = altText;
    }
    public String getTitleText() {
        return titleText;
    }
    public void setTitleText(String titleText) {
        this.titleText = titleText;
    }

}

TextEntry.java

package com.youthministry.domain;

import javax.persistence.Entity;
import javax.persistence.Lob;

@Entity
public class TextEntry extends PageContent {

    private String contentTitle;
    @Lob
    private String contentBody;

    public String getContentTitle() {
        return contentTitle;
    }
    public void setContentTitle(String contentTitle) {
        this.contentTitle = contentTitle;
    }
    public String getContentBody() {
        return contentBody;
    }
    public void setContentBody(String contentBody) {
        this.contentBody = contentBody;
    }

}

I have tried to resolve this and my hunch is that it has to do with the CascadeType that I'm using and I had thought that changing it to Cascade on delete instead which did remove the duplicative insert for the Group Entity however I still receive the aforementioned exception.

Here is the link to the git repo: http://github.com/dmcquillan314/YouthMinistryHibernate

Any help is appreciated thanks in advance.

Here are the create table scripts for all relevant db tables:

CREATE TABLE `PageContent_GROUP_DETAILS` (
  `PageContent_pageContentId` bigint(20) NOT NULL,
  `groups_groupId` bigint(20) NOT NULL,
  UNIQUE KEY `groups_groupId` (`groups_groupId`),
  KEY `FK43215F8D912C9AF9` (`PageContent_pageContentId`),
  KEY `FK43215F8D11E7050D` (`groups_groupId`),
  CONSTRAINT `FK43215F8D11E7050D` FOREIGN KEY (`groups_groupId`) REFERENCES `GROUP_DETAILS` (`groupId`),
  CONSTRAINT `FK43215F8D912C9AF9` FOREIGN KEY (`PageContent_pageContentId`) REFERENCES `PageContent` (`pageContentId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `Image` (
  `altText` varchar(255) DEFAULT NULL,
  `pathToImage` varchar(255) DEFAULT NULL,
  `titleText` varchar(255) DEFAULT NULL,
  `pageContentId` bigint(20) NOT NULL,
  PRIMARY KEY (`pageContentId`),
  KEY `FK437B93B4AAD3F6E` (`pageContentId`),
  CONSTRAINT `FK437B93B4AAD3F6E` FOREIGN KEY (`pageContentId`) REFERENCES `PageContent` (`pageContentId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `PageContent` (
  `pageContentId` bigint(20) NOT NULL AUTO_INCREMENT,
  `LOCATION` varchar(255) NOT NULL,
  `PAGE_CONTENT_NAME` varchar(255) NOT NULL,
  PRIMARY KEY (`pageContentId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

CREATE TABLE `GROUP_DETAILS` (
  `groupId` bigint(20) NOT NULL AUTO_INCREMENT,
  `groupDesc` varchar(255) DEFAULT NULL,
  `GROUP_NAME` varchar(255) NOT NULL,
  PRIMARY KEY (`groupId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

回答1:


You are making groups_groupId unique.

But the relationship in the PageContent class is OneToMany.

In the relationship table will be like below.

PageContentID GroupID
1             2
1             1
1             3
2             1
2             3

So making any column unique in this relationship table is a no no.

What I meant was for you to remove

UNIQUE KEY `groups_groupId` (`groups_groupId`),

from

CREATE TABLE `PageContent_GROUP_DETAILS` (
 `PageContent_pageContentId` bigint(20) NOT NULL,
 `groups_groupId` bigint(20) NOT NULL,
 UNIQUE KEY `groups_groupId` (`groups_groupId`),  
 KEY `FK43215F8D912C9AF9` (`PageContent_pageContentId`),  
 KEY `FK43215F8D11E7050D` (`groups_groupId`),  
 CONSTRAINT `FK43215F8D11E7050D` FOREIGN KEY (`groups_groupId`) REFERENCES `GROUP_DETAILS` (`groupId`),  
 CONSTRAINT `FK43215F8D912C9AF9` FOREIGN KEY (`PageContent_pageContentId`) 
 REFERENCES `PageContent` (`pageContentId`)) 
 ENGINE=InnoDB DEFAULT CHARSET=latin1


来源:https://stackoverflow.com/questions/15801701/duplicate-entry-exception-hibernate

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