首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用JPA @onetomany和@manytoone注释将数据插入三个表中

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

Stack Overflow用户
提问于 2019-10-16 07:20:13
回答 1查看 718关注 0票数 0

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

  1. HouseHold
  2. OwnerDetails
  3. HouseHoldMembers

但是,当我试图插入时,我会得到以下错误。

代码语言:javascript
运行
复制
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. 

HouseHold.java

代码语言:javascript
运行
复制
@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

OwnerDetails.java

代码语言:javascript
运行
复制
@Entity
@Table(name = "ownerdetails")
public class OwnerDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "owner_id_seq")
    @SequenceGenerator(name = "owner_id_seq", sequenceName = "owner_id_seq", allocationSize = 1)
    @Column(name = "oid")
    private Long oid;
    @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(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "hid", referencedColumnName = "hid", nullable = false, updatable = false, insertable = true)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private HouseHold houseHold;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ownerdetails", fetch = FetchType.EAGER)
    private List<HouseHoldMembers> membersdetails = new ArrayList<>();
    //default constructor
    //parameterized constructor
    //getter setter

HouseHoldMembers.java

代码语言:javascript
运行
复制
@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

Dtat.json

代码语言:javascript
运行
复制
{
    "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"
                }
            ]
        }
    ]
}

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

EN

回答 1

Stack Overflow用户

发布于 2019-10-18 12:24:24

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58407829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档