【问题标题】:Insert data into three table by using JPA @onetomany and @manytoone annotation使用 JPA @onetomany 和 @manytoone 注解将数据插入三个表
【发布时间】:2019-10-16 07:20:13
【问题描述】:

我正在尝试通过将 JPA Repository 与 Spring Boot 应用程序一起使用,将数据插入三个不同的表中。为此,我在这三个类中使用了@onetomany 和@manytoone 注释:

  1. 家庭控股
  2. 所有者详细信息
  3. HouseHoldMembers

但是当我尝试插入时出现以下错误。

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

@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

@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

@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

{
    "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"
                }
            ]
        }
    ]
}

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

【问题讨论】:

  • 您如何为“隐藏”创造价值?错误说:错误:“隐藏”列中的空值违反非空约束
  • 为此我在 OwnerDetails 类中创建了一个 get 方法 public Long getHid() { return houseHold.getHid(); } 这是返回 HouseHold 隐藏值但它不起作用。
  • “隐藏”列是自动增量值吗? get 方法不会为“hid”生成新值。您需要生成“隐藏”或让数据库处理它。
  • 从我这边它将如何生成?实际上,在 HouseHold 类中,我使用了 @GeneratedValue(strategy = GenerationType.IDENTITY) ,它工作正常,但在 OwnerDetails 类中,我在这里使用了带有 HouseHold 对象的 @ManyToOne 注释,它没有获得价值,所以它给出了错误。你知道我该怎么做吗?
  • 当我从 @JoinColumn 注释中删除 nullable = false 时,数据将插入到所有三个表中,但在 ownerdetails 和 homemembers 表中的外键列中设置的值为 null。

标签: spring-boot spring-data-jpa


【解决方案1】:

[JPA Examplem][一对多和多对一关系][https://github.com/kuldeepjha/JpaProject/tree/master/demo]

【讨论】:

  • 请用文字解释答案。当链接过期时,仅链接的答案失去意义。
猜你喜欢
  • 2016-11-14
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多