【问题标题】:Spring Boot JPA OneToMany return NullSpring Boot JPA OneToMany 返回 Null
【发布时间】:2020-02-25 05:54:27
【问题描述】:

我正在使用 jpa 开发 spring boot,我尝试使用 onetomany,并且由于某种原因,当我尝试在邮递员上发布请求时,它会创建我的父对象但不使用子外键,为此我' m 为空。

还有一件事:有一次我使用了我的代码并且不知何故让它工作了,但它只成功了一次,现在它不再工作了。 我尝试在网上学习很多教程,但没有一个能帮助我解决这个问题。

@Entity
@Table(name = "company")
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Basic(optional = false)
    @Column(nullable = false)
    private String comp_Name;

    @Basic(optional = false)
    @Column(nullable = false)
    private String password;

    @Basic(optional = false)
    @Column(nullable = false)
    private String email;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Coupon> coupons;

    public Company() {

    }

    public Company(int id, String comp_name, String password, String email) {

        setId(id);
        setComp_Name(comp_name);
        setPassword(password);
        setEmail(email);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getComp_Name() {
        return comp_Name;
    }

    public void setComp_Name(String comp_name) {
        this.comp_Name = comp_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Coupon> getCoupons() {
        return coupons;
    }

    public void setCoupons(List<Coupon> coupons) {
        this.coupons = coupons;
    }

    @Override
    public String toString() {
        return "Company [id=" + id + ",comp_name=" + comp_Name + ", password=" + password + ", email=" + email
                + "] \n\"";
    }

}
@Entity
@Table(name = "coupon")
public class Coupon {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(nullable = false)
    @Basic(optional = false)
    private String title;

    @Column(nullable = false)
    @Basic(optional = false)
    private int amount;

    @Column(nullable = false)
    @Basic(optional = false)
    private Date startDate;

    @Column(nullable = false)
    @Basic(optional = false)
    private Date endDate;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private CouponType type;

    @Column(nullable = false)
    @Basic(optional = false)
    private String msg;

    @Column(nullable = false)
    @Basic(optional = false)
    private double price;

    @Column(nullable = false)
    @Basic(optional = false)
    private String picture;

     public Coupon() {

    }

    public Coupon(int id, String title, int amount, Date startDate, Date endDate, CouponType type, String msg,
            double price, String picture) {

        this.id = id;
        this.title = title;
        this.amount = amount;
        this.startDate = startDate;
        this.endDate = endDate;
        this.type = type;
        this.msg = msg;
        this.price = price;
        this.picture = picture;

    }

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public CouponType getType() {
        return type;
    }

    public void setType(CouponType type) {
        this.type = type;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    @Override
    public String toString() {
        return "Coupon [id=" + id + ", title=" + title + ", amount=" + amount + ", startDate=" + startDate
                + ", endDate=" + endDate + ", type=" + type + ", msg=" + msg + ", price=" + price + ", picture="
                + picture + "]";
    }

}
@PostMapping("/createCoupon")
    public ResponseEntity<String> createCoupon(@RequestBody Coupon coupon) {
            try {
                companyService.createCoupon(coupon);
                return new ResponseEntity<>("coupon created" + coupon, HttpStatus.OK);
            } catch (Exception e) {
                return new ResponseEntity<>(e.getMessage() + e.getStackTrace(), HttpStatus.UNAUTHORIZED);
            }


    }

public boolean checkIfTitleAlreadyExists(String title) {
        if (couponRepo.findByTitle(title) != null) {
            return true;
        }
        return false;
    }

    public Coupon createCoupon(Coupon coupon) throws Exception {
        if (checkIfTitleAlreadyExists(coupon.getTitle()) == false) {
            couponRepo.save(coupon);
            Company comp = companyRepo.findById(this.company.getId());
            comp.getCoupons().add(coupon);
            companyRepo.save(comp);


        } else {
            throw new Exception("The title " + coupon.getTitle() + " already exist, please try another title");
        }
        return coupon;
    }

@Repository
public interface CouponRepo extends JpaRepository<Coupon, Integer> {

    @Query("Select c from Coupon c where c.id = :id")
    Coupon findById(int id);

    @Query("Select c from Coupon c where c.type = :type")
    List<Coupon> findByType(String type);

    @Transactional
    @Modifying
    @Query("DELETE FROM Coupon c WHERE c.id = :id")
    void removeCoupon(@Param("id") int id);

    public List<Coupon> findByEndDate(Date endDate);

    @Query("Select c from Coupon c where c.title = :title")
    Coupon findByTitle(String title);

    Customer save(int id);
}
@Repository
public interface CompanyRepo extends JpaRepository<Company, Integer> {

    @Query("Select c from Company c where c.id = :id")
    Company findById(int id);

    @Query("Select c from Company c where c.comp_Name = :compName")
    Company findCompanyByCompName(String compName);

    @Query("select c from Coupon c where c.endDate = :endDate")
    List<Coupon> findCouponsByDate(Date endDate);

    @Query("select c from Company c where c.comp_Name = :name And c.password = :password")
    Company findByCompanyNameAndPassword(String name, String password);

    @Query("select c from Coupon c where c.price = :price")
    List<Coupon> findCouponsByPrice(double price);

    @Query("select c from Coupon c where c.type = :type")
    List<Coupon> findCouponsByType(CouponType type);
}
im getting this msg on postman:

null[Ljava.lang.StackTraceElement;@75cf8323

and on eclipse this:

WebConfig; /company/createCoupon
Request Method: POST
Hibernate: select coupon0_.id as id1_2_, coupon0_.amount as amount2_2_, coupon0_.end_date as end_date3_2_, coupon0_.msg as msg4_2_, coupon0_.picture as picture5_2_, coupon0_.price as price6_2_, coupon0_.start_date as start_da7_2_, coupon0_.title as title8_2_, coupon0_.type as type9_2_ from coupon coupon0_ where coupon0_.title=?
Hibernate: select coupon0_.id as id1_2_0_, coupon0_.amount as amount2_2_0_, coupon0_.end_date as end_date3_2_0_, coupon0_.msg as msg4_2_0_, coupon0_.picture as picture5_2_0_, coupon0_.price as price6_2_0_, coupon0_.start_date as start_da7_2_0_, coupon0_.title as title8_2_0_, coupon0_.type as type9_2_0_ from coupon coupon0_ where coupon0_.id=?
Hibernate: update coupon set amount=?, end_date=?, msg=?, picture=?, price=?, start_date=?, title=?, type=? where id=?

【问题讨论】:

  • 我认为 NullPointerException 来自 Company comp = companyRepo.findById(this.company.getId()); comp.getCoupons().add(coupon);(可能是 comp=null)
  • 为了澄清问题,请查看完整的错误日志。
  • 仅供参考,您可以参考:github.com/dineshbhagat/spring-boot-web-jpa

标签: spring jpa hibernate-onetomany


【解决方案1】:

在您的公司课程中

@Entity
@Table(name = "company")
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @OneToMany(cascade = CascadeType.ALL,
        mappedBy = "company", orphanRemoval=true)
    @JsonManagedReference
    private List<Coupon> coupons= new ArrayList<>( 0 );

    .....
}

在您的优惠券类中

@Entity
@Table(name = "coupon")
public class Coupon {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    @JoinColumn(name = "company_id")
    @JsonBackReference
    private Company company;

    ...
}

每当您创建优惠券时,您都必须设置您的公司,否则它将为空。查看您的公司数据库。如果您没有手动设置公司,则 Coupon 表中的公司列为空。所以你必须将公司 ID 传递给你的控制器..

【讨论】:

  • 查看这个答案和问题。 stackoverflow.com/a/52435853/9427395
  • dasunse 我尝试了您发布的内容:我在邮递员 JSON 解析错误中收到此错误:无法构造 com.Spring.CouponSystem.Beans.Company 的实例(尽管至少存在一个创建者):没有 int/Int-argument 构造函数/从数值 (1) 反序列化的工厂方法;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造 com.Spring.CouponSystem.Beans.Company 的实例(尽管至少存在一个 Creator):没有 int/Int-argument 构造函数/工厂方法可以从数值 (1) 反序列化
  • @boxser 你能告诉我你试图发送的 json 对象吗..
  • { "id": 1, "title": "wqdqwrthrergd", "amount": 1, "startDate": 1569888000000, "endDate": 1571097600000, "type": "CLOTHING", " msg": "aaa", "price": 22, "picture": "analyticsindiamag.com/wp-content/uploads/2019/07/…", "company":1 }
  • 顺便说一句,我在 2 天前设法在没有 manytoone 和 joincol 的情况下在公司课程上发布了一次,但我改变了一些东西,现在它不再工作了,我忘了保存它
【解决方案2】:

这是如何修复需要在 Coupon 类中启动公司的代码

Coupon.class
@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
@ManyToOne
    @JoinColumn(name = "company_id")
    @JsonBackReference
    private Company company;

public Coupon(int id, String title, int amount, Date startDate, Date endDate, CouponType type, String msg,
            double price, String picture, Company company) {

        this.id = id;
        this.title = title;
        this.amount = amount;
        this.startDate = startDate;
        this.endDate = endDate;
        this.type = type;
        this.msg = msg;
        this.price = price;
        this.picture = picture;
        this.company = company;

    }
getters+setters

Company.class 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "company", orphanRemoval = true)
    @JsonManagedReference
    private List<Coupon> coupons = new ArrayList<>(0);
getters+setters

CompanyController.class

在邮递员上发布什么:

//  http://localhost:8080/company/coupon/{companyId}
//  {
//      "title": "adaddd333dad",
//      "amount": 1,
//      "startDate": 1569888000000,
//      "endDate": 1571097600000,
//      "type": "CLOTHING",
//      "msg": "aaa",
//      "price": 22,
//      "picture": "https://analyticsindiamag.com/wp-content/uploads/2019/07/image_rec_lib_banner.jpg"
//  }

    @PostMapping(value = "/coupon/{companyId}")
    @ResponseBody
    public ResponseEntity<String> createCoupon(@RequestBody Coupon coupon, @PathVariable("companyId") int id) {
        try {
            companyService.createCoupon(coupon, id);
            return new ResponseEntity<>("coupon created", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage() + e.getStackTrace(), HttpStatus.UNAUTHORIZED);
        }

    }

CompanyService.class

    public boolean checkIfTitleAlreadyExists(String title) {
        if (couponRepo.findByTitle(title) != null) {
            return true;
        }
        return false;
    }

    public boolean checkIfCompanyExists(int id) {
        if (companyRepo.findById(id) != null) {
            return true;
        }
        return false;
    }

    public Coupon createCoupon(Coupon coupon, int companyId) throws Exception {
        if (checkIfTitleAlreadyExists(coupon.getTitle()) == false) {
            coupon.setCompany(companyRepo.findById(companyId));
            if (checkIfCompanyExists(companyId)) {
                couponRepo.save(coupon);
            } else {
                throw new Exception("This Company is not exists");
            }


        } else {
            throw new Exception("The title " + coupon.getTitle() + " already exist, please try another title");
        }
        return coupon;
    }

它有效!

【讨论】:

    猜你喜欢
    • 2017-04-23
    • 2016-09-18
    • 2017-05-29
    • 2017-11-21
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多