gpt4 book ai didi

spring - hibernate 中带有 onetomany 注释的 org.hibernate.exception.ConstraintViolationException

转载 作者:行者123 更新时间:2023-12-02 06:39:47 28 4
gpt4 key购买 nike

我是探索 Spring Boot 和 Hibernate 的新手,面临着一个我认为并不新鲜的问题。然而,尽管提出了所有建议,我仍然找不到解决我当前面临的问题的方法。

你们中的任何人都可以指出我错在哪里吗?

以下是场景 -

我有一个 Category 类,该类别类的每个实例都可以有许多子类别的实例。

我已经使用 @OneToMany 注释设置了关系。但是,当尝试将记录保存到数据库时,我面临着org.hibernate.exception.ConstraintViolationException 报告异常,指出外键值不能为 NULL。

请在下面找到类声明

类别.class

import java.util.Date;
import java.util.Set;


import javax.persistence.JoinColumn;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.springframework.context.annotation.Scope;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Table(name="Category")
@Scope("session")
@EntityListeners(AuditingEntityListener.class)
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Category_Id")
private Long Id;

private String CategoryName;

private String CategoryValue;

@Column(name = "IsActive", columnDefinition = "BOOLEAN")
private Boolean IsActive;

// @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "category")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "category")
// @JoinTable(name = "Category_SubCategory", joinColumns = { @JoinColumn(name = "Category_Id") }, inverseJoinColumns = { @JoinColumn(name = "Sub_Category_Id") })
private Set<SubCategory> SubCategories;

@CreatedBy
@Column(nullable = false, updatable = false)
private String CreatedBy;

@CreatedDate
@Column(nullable = false, updatable = false)
private Date CreatedDate;

@LastModifiedBy
@Column(nullable = false)
private String ModifiedBy;

@LastModifiedDate
@Column(nullable = false)
private Date ModifiedDate;

public Long getId() {
return Id;
}

public void setId(Long id) {
Id = id;
}

public String getCategoryName() {
return CategoryName;
}

public void setCategoryName(String categoryName) {
CategoryName = categoryName;
}

public String getCategoryValue() {
return CategoryValue;
}

public void setCategoryValue(String categoryValue) {
CategoryValue = categoryValue;
}

public Boolean getIsActive() {
return IsActive;
}

public void setIsActive(Boolean isActive) {
IsActive = isActive;
}

public Set<SubCategory> getSubCategories() {
return SubCategories;
}

public void setSubCategories(Set<SubCategory> subCategories) {
SubCategories = subCategories;
}

public String getCreatedBy() {
return CreatedBy;
}

public void setCreatedBy(String createdBy) {
CreatedBy = createdBy;
}

public Date getCreatedDate() {
return CreatedDate;
}

public void setCreatedDate(Date createdDate) {
CreatedDate = createdDate;
}

public String getModifiedBy() {
return ModifiedBy;
}

public void setModifiedBy(String modifiedBy) {
ModifiedBy = modifiedBy;
}

public Date getModifiedDate() {
return ModifiedDate;
}

public void setModifiedDate(Date modifiedDate) {
ModifiedDate = modifiedDate;
}

}

子类别.class

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.context.annotation.Scope;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Table(name="SubCategory")
@Scope("session")
@EntityListeners(AuditingEntityListener.class)
public class SubCategory {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Sub_Category_Id")
private Long Id;

private String SubCategoryName;

private String SubCategoryValue;

@Column(name = "IsActive", columnDefinition = "BOOLEAN")
private Boolean IsActive;

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "Category_Id", nullable = false)
private Category category;

@CreatedBy
@Column(nullable = false, updatable = false)
private String CreatedBy;

@CreatedDate
@Column(nullable = false, updatable = false)
private Date CreatedDate;

@LastModifiedBy
@Column(nullable = false)
private String ModifiedBy;

@LastModifiedDate
@Column(nullable = false)
private Date ModifiedDate;

public Long getId() {
return Id;
}

public void setId(Long id) {
Id = id;
}

public String getSubCategoryName() {
return SubCategoryName;
}

public void setSubCategoryName(String subCategoryName) {
SubCategoryName = subCategoryName;
}

public String getSubCategoryValue() {
return SubCategoryValue;
}

public void setSubCategoryValue(String subCategoryValue) {
SubCategoryValue = subCategoryValue;
}

public Boolean getIsActive() {
return IsActive;
}

public void setIsActive(Boolean isActive) {
IsActive = isActive;
}

public String getCreatedBy() {
return CreatedBy;
}

public void setCreatedBy(String createdBy) {
CreatedBy = createdBy;
}

public Date getCreatedDate() {
return CreatedDate;
}

public void setCreatedDate(Date createdDate) {
CreatedDate = createdDate;
}

public String getModifiedBy() {
return ModifiedBy;
}

public void setModifiedBy(String modifiedBy) {
ModifiedBy = modifiedBy;
}

public Date getModifiedDate() {
return ModifiedDate;
}

public void setModifiedDate(Date modifiedDate) {
ModifiedDate = modifiedDate;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}



}

ServiceImpl -

@Override
public void save(Category category) {

Set<SubCategory> subCategoryRec = category.getSubCategories();
if(subCategoryRec != null && subCategoryRec.size() > 0) {
for(SubCategory rec: subCategoryRec) {
try {
subcategoryRepository.save(rec);
}catch (Exception ex) {
ex.printStackTrace();
}

}
}

try {
categoryRepository.save(category);
} catch (Exception ex) {
ex.printStackTrace();
}


}

不知道我错在哪里。

报告的异常具有以下堆栈跟踪 -

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2855)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3426)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:619)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:273)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:254)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:299)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
at com.sun.proxy.$Proxy113.persist(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy113.persist(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:508)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:513)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:475)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 104 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'category_id' cannot be null

最佳答案

原因是您尝试在保存父对象之前保存子对象。因此,将实现更改为首先保存父对象,然后保存子对象,如下所示。

    categoryRepository.save(category);
Set<SubCategory> subCategoryRec = category.getSubCategories();
if(subCategoryRec != null && subCategoryRec.size() > 0) {
for(SubCategory rec: subCategoryRec) {
subcategoryRepository.save(rec);
}
}

关于spring - hibernate 中带有 onetomany 注释的 org.hibernate.exception.ConstraintViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646029/

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