jpa 请求处理失败;嵌套异常是org. springframework. dao.数据完整性违规异常:

rwqw0loc  于 2023-02-09  发布在  Spring
关注(0)|答案(1)|浏览(245)

Exception SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/z2] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.spring.entity.Product.cd; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.spring.entity.Product.cd] with root cause org.hibernate.PropertyValueException: not-null property references a null or transient value : com.spring.entity.Product.cd at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:106) at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:132) at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:86) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490) at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195) at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179) at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214) at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 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) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:257) at com.sun.proxy.$Proxy39.persist(Unknown Source) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:431) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 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 classes

@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
}
ilmyapht

ilmyapht1#

我认为这是因为您在Map设置中有选项optional = false。此选项旨在防止在runtime中出现空值

@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.

因此,您应该检查相应实体的值是否为空

相关问题