1

我在将条目存储到我的数据库中时遇到了一些问题。我正在使用以下层次结构:

我有一个父类 PageContent 和两个子类 TextEntry 和 Image 但是当我尝试存储多个 PageContent 实体时,我得到以下异常:

HTTP 状态 500 - 请求处理失败;嵌套异常是 org.springframework.dao.DataIntegrityViolationException: Duplicate entry '2' for key 'groups_groupId'; SQL [不适用];约束[空];嵌套异常是 org.hibernate.exception.ConstraintViolationException: Duplicate entry '2' for key 'groups_groupId'

这是相关的类

组.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;
    }

}

页面内容.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;
    }

}

图像.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;
    }

}

文本输入.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;
    }

}

我试图解决这个问题,我的预感是它与我正在使用的 CascadeType 有关,我曾认为在删除时将其更改为 Cascade,而不是删除了 Group Entity 的重复插入,但是我仍然收到上述例外。

这是 git repo 的链接:http: //github.com/dmcquillan314/YouthMinistryHibernate

提前感谢任何帮助。

以下是所有相关数据库表的创建表脚本:

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
4

1 回答 1

2

你正在做groups_groupId独一无二的。

但是PageContent类中的关系是OneToMany

在关系表中将如下所示。

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

因此,在此关系表中使任何列唯一都是不行的。

我的意思是让你删除

UNIQUE KEY `groups_groupId` (`groups_groupId`),

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
于 2013-04-04T04:40:05.553 回答