Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 使用JPA@onetomany和@manytoone注释将数据插入到三个表中_Spring Boot_Spring Data Jpa - Fatal编程技术网

Spring boot 使用JPA@onetomany和@manytoone注释将数据插入到三个表中

Spring boot 使用JPA@onetomany和@manytoone注释将数据插入到三个表中,spring-boot,spring-data-jpa,Spring Boot,Spring Data Jpa,我试图通过使用JPA存储库和spring boot应用程序将数据插入三个不同的表中。为此,我在这三个类中使用了@onetomany和@manytoone注释: 家庭 所有者详细信息 家庭成员 但是当我尝试插入时,我得到了以下错误 Hibernate: select * from household where sl_no = ? 2019-10-16 12:24:46.622 INFO 19380 --- [nio-

我试图通过使用JPA存储库和spring boot应用程序将数据插入三个不同的表中。为此,我在这三个类中使用了@onetomany和@manytoone注释:

  • 家庭
  • 所有者详细信息
  • 家庭成员
  • 但是当我尝试插入时,我得到了以下错误

    Hibernate: 
        select
            * 
        from
            household 
        where
            sl_no = ?
    2019-10-16 12:24:46.622  INFO 19380 --- [nio-8080-exec-1] c.o.a.a.s.FormServiceImpl                : FormServiceImpl saveHouseDetailsWithBase64() is invoked : 234546674
    Hibernate: 
        select
            nextval ('house_id_seq')
    Hibernate: 
        select
            nextval ('owner_id_seq')
    Hibernate: 
        select
            nextval ('mem_id_seq')
    Hibernate: 
        insert 
        into
            household
            (area, audio, district, east, gp_name, grid_no, house_dimension, house_photo, id, id_number, id_photo, Khatha_no, latitute, locality_name, longitute, map_photo, north, phone_num, pin, prop_details, prop_type, rent_amount, road_name, sl_no, south, servey_date, survey_no, surveyor_name, taluk, tenant, toilet_available, total_members, vacant_port, village_name, water_facility, west, hid) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    Hibernate: 
        insert 
        into
            ownerdetails
            (age, education, gender, hid, idname, idnumber, name, oid) 
        values
            (?, ?, ?, ?, ?, ?, ?, ?)
    2019-10-16 12:24:46.832  WARN 19380 --- [nio-8080-exec-1] o.h.e.j.s.SqlExceptionHelper             : SQL Error: 0, SQLState: 23502
    2019-10-16 12:24:46.832 ERROR 19380 --- [nio-8080-exec-1] o.h.e.j.s.SqlExceptionHelper             : ERROR: null value in column "hid" violates not-null constraint
      Detail: Failing row contains (1, 10, education, male, adhaarcard1, 23424242343, name, null).
    2019-10-16 12:24:46.840 ERROR 19380 --- [nio-8080-exec-1] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
    could not execute statement; SQL [n/a]; constraint [hid]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    2019-10-16 12:24:46.848 ERROR 19380 --- [nio-8080-exec-1] c.o.a.a.s.FormServiceImpl                : could not execute statement; SQL [n/a]; constraint [hid]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    2019-10-16 12:24:46.849  INFO 19380 --- [nio-8080-exec-1] c.o.a.a.c.FormDataController             : FormDataController saveHouseHold() request is completed. 
    
    househouse.java

    @Entity
    @Table(name = "household")
    public class HouseHold implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "house_id_seq")
        @SequenceGenerator(name = "house_id_seq", sequenceName = "house_id_seq", allocationSize = 1)
        @Column(name = "hid")
        private Long hid;
        @NotNull
        @Size(max = 100)
        @Column(name = "district")
        private String district;
        @NotNull
        @Size(max = 100)
        @Column(name = "taluk")
        private String taluk;
        @NotNull
        @Size(max = 100)
        @Column(name = "village_name")
        private String villageName;
        @NotNull
        @Column(name = "sl_no")
        private Long slNo;
        @NotNull
        @Size(max = 100)
        @Column(name = "Khatha_no")
        private String khathaNo;
        @NotNull
        @Size(max = 50)
        @Column(name = "locality_name")
        private String localityName;
        @NotNull
        @Size(max = 50)
        @Column(name = "prop_details")
        private String propertyDetails;
        @NotNull
        @Size(max = 50)
        @Column(name = "tenant")
        private String tenant;
        @NotNull
        @Size(max = 200)
        @Column(name = "house_dimension")
        private String houseDimension;
        @NotNull
        @Size(max = 50)
        @Column(name = "east")
        private String east;
        @NotNull
        @Size(max = 50)
        @Column(name = "west")
        private String west;
        @NotNull
        @Size(max = 50)
        @Column(name = "north")
        private String north;
        @NotNull
        @Size(max = 50)
        @Column(name = "south")
        private String south;
        @NotNull
        @Digits(integer = 6, fraction = 2)
        @Column(name = "rent_amount")
        private BigDecimal rentAmount;
        @NotNull
        @Size(max = 100)
        @Column(name = "vacant_port")
        private String vacantPort;
        @NotNull
        @Size(max = 100)
        @Column(name = "gp_name")
        private String gpName;
        @NotNull
        @Size(max = 100)
        @Column(name = "prop_type")
        private String propertyType;
        @NotNull
        @Size(max = 100)
        @Column(name = "road_name")
        private String roadName;
        @NotNull
        @Column(name = "pin")
        private Long pin;
        @NotNull
        @Column(name = "survey_no")
        private Long surveyNo;
        @NotNull
        @Size(max = 250)
        @Column(name = "grid_no")
        private String gridNo;
        @NotNull
        @Size(max = 250)
        @Column(name = "id_number")
        private String idNumber;
        @NotNull
        @Size(max = 100)
        @Column(name = "area")
        private String area;
        @NotNull
        @Size(max = 3)
        @Column(name = "toilet_available")
        private String toiletAvailable;
        @NotNull
        @Size(max = 3)
        @Column(name = "water_facility")
        private String waterFacility;
        @NotNull
        @Column(name = "phone_num")
        private Long phoneNumber;
        @NotNull
        @Column(name = "house_photo")
        private String housephoto;
        @NotNull
        @Column(name = "id_photo")
        private String idphoto;
        @NotNull
        @Column(name = "map_photo")
        private String mapphoto;
        @NotNull
        @Column(name = "audio")
        private String audio;
        @NotNull
        @Digits(integer = 3, fraction = 25)
        @Column(name = "latitute")
        private BigDecimal latitude;
        @NotNull
        @Digits(integer = 3, fraction = 25)
        @Column(name = "longitute")
        private BigDecimal longitude;
        @NotNull
        @Size(max = 100)
        @Column(name = "surveyor_name")
        private String surveyorName;
        @Column(name = "servey_date")
        @Temporal(TemporalType.TIMESTAMP)
        private Date surveyDate;
        @NotNull
        @Size(max = 10)
        @Column(name = "total_members")
        private String totalMembers;
        @NotNull
        @Column(name = "id")
        private Long id;
        @JsonIgnore
        @Transient
        private String serveyStringDate;
    
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "houseHold", fetch = FetchType.EAGER)
        private List<OwnerDetails> ownerdetail = new ArrayList<>();
    //default constructor
    //parameterized constructor
    //getter setter
    
    Dtat.json

    {
        "district" : "district",
        "taluk" : "taluk",
        "villageName" : "village name",
        "slNo" : 234546674,
        "khathaNo" : "35824005",
        "localityName" : "localitiyname",
        "propertyDetails" : "property Details",
        "tenant" : "tenant",
        "houseDimension" : "housedimension",
        "east":"east",
        "west":"west",
        "north":"north",
        "south":"south",
        "rentAmount":2000.45,
        "vacantPort":"2342",
        "gpId":23112,
        "gpName":"gpname",
        "propertyType":"proprty type",
        "roadName":"road name",
        "pin":700003,
        "surveyNo":23122,
        "gridNo":"23122",
        "idNumber":"2321223232232",
        "area":"area",
        "toiletAvailable":"yes",
        "waterFacility":"yes",
        "phoneNumber":9999999999,
    
        "housephoto":"",
    
        "mapphoto":"",
    
        "audio":"",
    
        "latitude":"22.453",
        "longitude":"88.453",
        "surveyorName":"surveyor name",
        "serveyStringDate":"2019-10-13 11:25:36",
        "totalMembers":"2",
        "id":1,
    
        "ownerdetail":
        [ 
            {
                "name":"name",
                "education":"education",
                "age":10,
                "gender":"male",
                "idName":"adhaarcard1",
                "idNumber":"23424242343",
                "membersdetails":
                [
                    {
                        "name":"name",
                        "education":"education",
                        "age":10,
                        "gender":"male",
                        "idName":"adhaarcard2",
                        "idNumber":"23424242344"
                    },
                    {
                        "name":"name1",
                        "education":"education1",
                        "age":11,
                        "gender":"male",
                        "idName":"adhaarcard2",
                        "idNumber":"23424242344"
                    }
                ]
            }
        ]
    }
    

    我还为每个实体类创建了存储库类。有人能帮我解决这个错误吗。提前感谢。

    [JPA Examplem][1对多和多对1关系][]

    您如何为“hid”创造价值?错误显示:错误:“hid”列中的null值违反了not null约束为此,我在OwnerDetails类public Long getHid(){return househouse.getHid();}中创建了一个get方法,它是返回HOusehouse hid值,但不起作用。“hid”列是自动递增值吗?get方法不会为“hid”生成新值。您需要生成“hid”或让数据库处理它。从我的角度看,它将如何生成?实际上,在Househouse类中,我使用了@GeneratedValue(strategy=GenerationType.IDENTITY),该类工作正常,但在OwnerDetails类中,我使用了@ManyToOne注释,其中的Househouse对象没有得到值,因此给出了错误。你知道我该怎么做吗。当我从@JoinColumn注释中删除nullable=false时,数据会插入到所有三个表中,但在ownerdetails和householdmembers表的外键列中设置的值为null。请用文字解释答案。当链接过期时,链接只回答失去意义的问题。
    @Entity
    @Table(name = "household_members")
    public class HouseHoldMembers implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "mem_id_seq")
        @SequenceGenerator(name = "mem_id_seq", sequenceName = "mem_id_seq", allocationSize = 1)
        @Column(name = "mid")
        private Long mid;
        @NotNull
        @Size(max = 100)
        @Column(name = "name")
        private String name;
        @NotNull
        @Size(max = 100)
        @Column(name = "education")
        private String education;
        @NotNull
        @Column(name = "age")
        private int age;
        @NotNull
        @Size(max = 10)
        @Column(name = "gender")
        private String gender;
        @NotNull
        @Size(max = 100)
        @Column(name = "idname")
        private String idName;
        @NotNull
        @Size(max = 100)
        @Column(name = "idnumber")
        private String idNumber;
    
        @JsonIgnore
        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "oid", nullable = false, updatable = false, insertable = true)
        @OnDelete(action = OnDeleteAction.CASCADE)
        private OwnerDetails ownerdetails;
            //default constructor
            //parameterized constructor
            //getter setter
    
    {
        "district" : "district",
        "taluk" : "taluk",
        "villageName" : "village name",
        "slNo" : 234546674,
        "khathaNo" : "35824005",
        "localityName" : "localitiyname",
        "propertyDetails" : "property Details",
        "tenant" : "tenant",
        "houseDimension" : "housedimension",
        "east":"east",
        "west":"west",
        "north":"north",
        "south":"south",
        "rentAmount":2000.45,
        "vacantPort":"2342",
        "gpId":23112,
        "gpName":"gpname",
        "propertyType":"proprty type",
        "roadName":"road name",
        "pin":700003,
        "surveyNo":23122,
        "gridNo":"23122",
        "idNumber":"2321223232232",
        "area":"area",
        "toiletAvailable":"yes",
        "waterFacility":"yes",
        "phoneNumber":9999999999,
    
        "housephoto":"",
    
        "mapphoto":"",
    
        "audio":"",
    
        "latitude":"22.453",
        "longitude":"88.453",
        "surveyorName":"surveyor name",
        "serveyStringDate":"2019-10-13 11:25:36",
        "totalMembers":"2",
        "id":1,
    
        "ownerdetail":
        [ 
            {
                "name":"name",
                "education":"education",
                "age":10,
                "gender":"male",
                "idName":"adhaarcard1",
                "idNumber":"23424242343",
                "membersdetails":
                [
                    {
                        "name":"name",
                        "education":"education",
                        "age":10,
                        "gender":"male",
                        "idName":"adhaarcard2",
                        "idNumber":"23424242344"
                    },
                    {
                        "name":"name1",
                        "education":"education1",
                        "age":11,
                        "gender":"male",
                        "idName":"adhaarcard2",
                        "idNumber":"23424242344"
                    }
                ]
            }
        ]
    }