复制条目异常休眠 [英] Duplicate entry exception hibernate
问题描述
我有一些问题将条目存储到我的数据库中。我使用以下heirarchy:
我有一个父类PageContent和两个子类TextEntry和Image但是当我尝试存储多于PageContent的实体时,我得到以下异常:
HTTP状态500 - 请求处理失败;嵌套的异常是org.springframework.dao.DataIntegrityViolationException:重复键入'groups_groupId'的条目'2'; SQL [n / a];约束[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;
私人字符串groupDesc;
$ b / * 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)
私人字符串位置;
@OneToMany(cascade = CascadeType.REMOVE)
私人收藏< 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(){
返回组;
}
public void setGroups(Collection< Group> groups){
this.groups = groups;
}
}
Image.java
package com.youthministry.domain;
import javax.persistence.Entity;
@Entity
public class Image扩展PageContent {
private String pathToImage;
私人字符串altText;
私人字符串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;
$ b b
$ b TextEntry.java package com.youthministry.domain;
import javax.persistence.Entity;
import javax.persistence.Lob;
@Entity
公共类TextEntry扩展PageContent {
私有字符串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
任何帮助表示感谢,提前。
以下是所有相关db表的创建表脚本:
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`(
`a (NULL)$ 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,$ b $`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
所以在这个关系表中创建任何一个列是唯一的。
我的意思是让你删除
< pre $ UNIQUE KEY`groups_groupId`(`groups_groupId`),
from
pre $
CREATE TABLE`PageContent_GROUP_DETAILS`(
`PageContent_pageContentId` bigint(20)NOT NULL,
`group_groupId` bigint(20)NOT NULL,
UNIQUE KEY`groups_groupId`(`groups_groupId`),
KEY`FK43215F8D912C9AF9`(`PageContent_pageContentId`),
KEY`FK43215F8D1 1E7050D`(`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
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
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
这篇关于复制条目异常休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!