当前位置: 首页 > 知识库问答 >
问题:

请求处理失败;嵌套异常org.springframework.dao.DataIntegrityViolationException:

田兴怀
2023-03-14

异常严重:Servlet。路径为[z2]的上下文中servlet[dispatcher]的service()引发异常[请求处理失败;嵌套异常为org.springframework.dao.DataIntegrityViolationException:not null属性引用null或瞬时值:com.spring.entity.Product.cd;嵌套异常为org.hibernate.PropertyValueException:not null属性引用null或瞬时值:com.spring.entity.Product.cd],根本原因为org.hibernate。PropertyValueException:not null属性引用null或瞬态值:com.spring.entity.Product。cd位于org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:106)位于org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:132)位于org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:86)位于org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)位于org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)位于org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)位于org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)位于org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)位于org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)位于org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84)位于org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)位于org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)org.hibernate.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)位于org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)位于org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)位于org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(SessionImpl.java:789)(AbstractEntityManagerImpl.java:1181)位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl。invoke0(本机方法)位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)位于java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.base/java.lang.reflect.Method.invoke(Method.java:566)位于org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvoke(SharedEntityManagerCreator.java:257)位于com.sun.proxy.Proxy39.persist(未知源)位于org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:431)位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl。java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.base/java.lang.reflect.Method.invoke(Method.java:566)

实体类

    @Entity
    @Table(name = "tbl_Customer")
    public class CustomerDetails {
    
        @Id
        @GeneratedValue
        @Column(name="Customer_Id")
        private Long custid;
    
        @Column(name="Customer_Name")
        private String customerName;
        
        @Column(name="Email")
        private String email;
        
        @Column(name="Address")
        private String address;
        
        @Column(name="Phone")
        private String phoneno;
        
        public CustomerDetails() {
        }
    
        @Override
        public String toString() {
            return "CustomerDetails [custid=" + custid + ", customername=" + customerName + ", email=" + email
                    + ", address=" + address + ", phoneno=" + phoneno + "]";
        }
    
        public CustomerDetails(String customername, String email, String address, String phoneno) {
            super();
            this.customerName = customername;
            this.email = email;
            this.address = address;
            this.phoneno = phoneno;
        }
    
        @OneToMany(mappedBy = "cd", fetch = FetchType.LAZY,cascade = CascadeType.ALL)
        private Set<Product> product;
        
    //getters and setters
    }
--------------------------------------------------------------------------------------------------

@Entity
@Table(name="tbl_Product")
public class Product {
 
    
    @Id
    @GeneratedValue
    @Column(name="Product_Id")
    private Long productId;
    
    @Column(name="Product_Name")
    private String productName;
    
    @Column(name="Product_Brand")
    private String productBrand;
    
    @Column(name="Product_Price")
    private double productPrice;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private CustomerDetails cd;
   
    
    
    public Product(Long productId, String productName, String productBrand, double productPrice) {
        super();
        this.productId = productId;
        this.productName = productName;
        this.productBrand = productBrand;
        this.productPrice = productPrice;
    }
    
    
    
    public Product(String productName, String productBrand, double productPrice) {
        super();
        this.productName = productName;
        this.productBrand = productBrand;
        this.productPrice = productPrice;
    }

//getters and setters
}

共有1个答案

楚浩然
2023-03-14

我认为这是因为在映射设置中有option optional=false。此选项旨在防止运行时出现空值

@ManyToOne(fetch = FetchType.LAZY, optional = false)
private CustomerDetails cd;

optional=false的语义如下:

Whether the association is optional. If set to false then a non-null relationship must always exist.

因此,您应该检查对应实体的值是否为null

 类似资料: