我正在用jpa处理spring boot,我尝试使用onetomany,由于某种原因,当我尝试在postman上发送请求时,它创建了我的父对象,但没有使用子外键,因此我得到了null。
还有一件事:有一次我玩了我的代码,不知怎么的,我让它工作了,但它只成功了一次,现在它不再工作了。我试图在网上学习很多教程,但没有一个帮助我解决这个问题。
@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=?
这是修复代码需要启动公司的优惠券类的方法
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;
}
而且很管用!
在你的公司课堂上
@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;
...
}
每当您创建优惠券时,您都必须设置您的公司,否则它将为空。查看您的公司数据库。如果您尚未手动设置公司,则优惠券表中的公司列为空。因此,您必须将公司ID传递给控制器。
5.3. 多返回值 在Go中,一个函数可以返回多个值。我们已经在之前例子中看到,许多标准库中的函数返回2个值,一个是期望得到的返回值,另一个是函数出错时的错误信息。下面的例子会展示如何编写多返回值的函数。 下面的程序是findlinks的改进版本。修改后的findlinks可以自己发起HTTP请求,这样我们就不必再运行fetch。因为HTTP请求和解析操作可能会失败,因此findlinks声明了2
我用的是parse.com。每个包里面都有很多钻子,每个品类里面都有很多包。 我卡住了。然后(函数(result,result2,result3)在最后一行。promise可以有多个promise,它是可变的。有没有一种方法可以写出如下内容: 谢谢!
Go语言是支持多值返回的。怎么实现的呢?让我们先看一看C语言是如果返回多个值的。在C中如果想返回多个值,通常会在调用函数中分配返回值的空间,并将返回值的指针传给被调函数。 int ret1, ret2; f(a, b, &ret1, &ret2) 被调函数被定义为下面形式,在函数中会修改ret1和ret2。对指针参数所指向的内容的修改会被返回到调用函数,用这种方式实现多值返回。 void f(i
问题内容: 现在在phpMyAdmin中工作: 表结构: 该表当前有4行数据,具有不同的OrderID。 SQL: 结果: 我期待第四行-只是一个数字,但是phpMyAdmin中的每一行都有一个0。 问题答案: 返回最后插入的行的ID,并且未绑定到任何表。因此,如果您创建一个新行: 它将返回最后一个id(新主键具有的任何值)。 有关详细信息,请查看 手册 : LAST_INSERT_ID()(不带
问题内容: 我用Java写了一个函数,我希望这个函数返回多个值。除了使用数组和结构外,还有没有办法返回多个值? 我的代码: 问题答案: 在Java中,当您希望函数返回多个值时,必须 将这些值嵌入到您返回的对象中 或更改传递给函数的对象 在您的情况下,您显然需要定义一个可以包含,和的类: 然后将您的功能更改为
Go语言内置支持多返回值,这个在Go语言中用的很多,比如一个函数同时返回结果和错误信息。 package main import "fmt" // 这个函数的返回值为两个int func vals() (int, int) { return 3, 7 } func main() { // 获取函数的两个返回值 a, b := vals() fmt.Println(a