gpt4 book ai didi

java - 重复输入异常hibernate

转载 作者:行者123 更新时间:2023-11-29 06:50:25 24 4
gpt4 key购买 nike

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

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

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'

这里是相关类

组.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 on delete 而不是删除组实体的重复插入,但是我仍然收到上述异常。

这是 git 仓库的链接: 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

最佳答案

您正在使 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

关于java - 重复输入异常hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com