Я работаю над весенней загрузкой с jpa, я пытаюсь использовать onetomany и по какой-то причине, когда я пытаюсь опубликовать запрос на почтальоне, он создает мой родительский объект, но не использует дочерний внешний ключ, и для этого я 'Я получаю нулевое значение.
Еще одна вещь: однажды я поиграл со своим кодом и каким-то образом заставил его работать, но это удалось только один раз, и теперь он больше не работает. Я пытался следовать многим учебникам в Интернете, но ни один из них не помог мне решить эту проблему.
@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=?