我在将条目存储到数据库时遇到了一些问题。我正在使用以下的heirarchy:
我有一个父类PageContent和两个子类TextEntry和Image但是当我尝试存储多个PageContent实体时,我得到以下异常:
HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.dao.DataIntegrityViolationException:键'groups_groupId'的重复条目'2'; SQL [不适用];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:键'groups_groupId'的重复条目'2'
以下是相关课程
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;
}
}
我试图解决这个问题,我的预感是它与我正在使用的CascadeType有关,而且我曾想过将它更改为Cascade on delete而不是删除了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
答案 0 :(得分: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