我一直在尝试了解Hibernate映射,但我只是不明白。 在春季项目中使用休眠模式。 db的mysql工作台。
每个表都有一个ID,优惠券和公司。 当公司创建优惠券时,我希望优惠券ID和公司ID转到名为companyCoupon的第三张表,并在该表中进行映射,这样:
一家公司可能有许多优惠券。 优惠券可能与一家公司关联。
公司ID = 1; 优惠券ID = 1;
companycoupon- compid = 1; coupid = 1;
ive创建了第三个表,并伪造了映射到其他表的键。
有人吗?有任何想法吗?好文章要阅读?
编辑:
zipCodePattern = /^\d{4} ?[a-z]{2}$/i;
2:
package com.example.CouponProjectCore.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "coupons")
public class Coupon {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="coupid")
private long coupid;
@Column(name="title")
private String title;
@Column(name="startd")
private String startd;
@Column(name="endd")
private String endd;
@Column(name="amount")
private int amount;
@Column(name="type")
private String type;
@Column(name="message")
private String message;
@Column(name="price")
private double price;
@Column(name="image")
private String image;
@ManyToOne()
private Company company;
public Coupon(String title, String startd, String endd, int amount, String type, String message, double price,
String image) {
super();
this.title = title;
this.startd = startd;
this.endd = endd;
this.amount = amount;
this.type = type;
this.message = message;
this.price = price;
this.image = image;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public long getCoupid() {
return coupid;
}
public void setCoupid(long coupid) {
this.coupid = coupid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStartd() {
return startd;
}
public void setStartd(String startd) {
this.startd = startd;
}
public String getEndd() {
return endd;
}
public void setEndd(String endd) {
this.endd = endd;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Coupon [coupid=" + coupid + ", title=" + title + ", startd=" + startd + ", endd=" + endd + ", amount="
+ amount + ", type=" + type + ", message=" + message + ", price=" + price + ", image=" + image
+ "]";
}
}
答案 0 :(得分:0)
好的,那么您无需添加第三张表,只需添加带有companyId的优惠券列表
//add in Company Enitity
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="COMPANY_ID")
private List<CouponsEntity> coupons;
//add in Coupons Enitity
@ManyToOne
private CompanyEntity company;
这是可以理解的静态示例
Company user= new Company();
user.setId(1);
user.setComp_name("qwqwq");
user.setEmail("dde@gmail.com");
user.setPassword("w1w");
Coupon c= new Coupon("wssw", "2019", "2201", 1, "1w1", "1w1w", 2.3, "w1w1w1", user);
Coupon c1= new Coupon("wssw", "2019", "2201", 1, "1w1", "1w1w", 2.3, "w1w1w1", user);
Coupon c2= new Coupon("wssw", "2019", "2201", 1, "1w1", "1w1w", 2.3, "w1w1w1", user);
Coupon c3= new Coupon("wssw", "2019", "2201", 1, "1w1", "1w1w", 2.3, "w1w1w1", user);
List<Coupon> coupons = new ArrayList<Coupon>();
coupons.add(c);
coupons.add(c1);
coupons.add(c2);
coupons.add(c3);
user.setCoupons(coupons);