【问题标题】:Creating one table from two tables using their Id's使用它们的 Id 从两个表创建一个表
【发布时间】:2019-09-03 11:52:26
【问题描述】:

我一直在尝试理解 Hibernate 映射,但我就是不明白。 在春季项目中使用休眠。用于数据库的 mysql 工作台。

每张桌子都有一个 ID、优惠券和公司。 当公司创建优惠券时,我希望将优惠券 ID 和公司 ID 转到名为 companyCoupon 的第三个表并将其映射到那里:

一家公司可能有很多优惠券。 一张优惠券可能与 ONE 公司相关联。

公司编号 = 1; 优惠券 ID = 1;

公司优惠券 - 比较 = 1; coupid = 1;

我创建了第三个表并伪造了映射到其他表的键。

有人吗?有任何想法吗?好文章值得一看?

编辑:

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
             + "]";
    }
}

2:

package com.example.CouponProjectCore.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;

@Entity
@Table(name = "company")

public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @Column(name = "comp_name")
    private String comp_name;
    @Column(name = "password")
    private String password;
    @Column(name = "email")
    private String email;

    @OneToMany()
    @JoinColumn(name="id")
    private List<Coupon> coupons;

    @Column(name = "client_type")
    @Enumerated(EnumType.STRING)
    private ClientType client_type = ClientType.COMPANY;

    public Company() {

    }

    public Company(String comp_name, String password, String email, List<Coupon> coupons, ClientType client_type) {
        super();
        this.comp_name = comp_name;
        this.password = password;
        this.email = email;
        this.coupons = coupons;
        this.client_type = client_type;
    }

    public void addCoupon(Coupon coupon) {
        if (coupons == null) {
            coupons = new ArrayList<Coupon>();
        }

        coupons.add(coupon);
    }

    public long getId() {
        return id;
    }

    public void setId(long 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;
    }

    public ClientType getClient_type() {
        return client_type;
    }

    public void setClient_type(ClientType client_type) {
        this.client_type = client_type;
    }

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

}

【问题讨论】:

  • 为防止投反对票,请仅添加理解您的问题和定义明确问题所必需的信息。

标签: hibernate spring-boot spring-data mysql-workbench


【解决方案1】:

好的,那么您无需添加第三个表,只需添加带有 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);

【讨论】:

  • 一家公司可能有很多优惠券。优惠券可能与 ONE 公司相关联。
  • java.sql.SQLSyntaxErrorException:“字段列表”中的未知列“company_id”
  • 我在程序中没有字段 company_id ANYWHERE,代码中没有,数据库中没有,知道它来自哪里吗?大声笑,它基本上在优惠券对象中构成一个新字段,将“company_id”添加到优惠券对象中
  • 我需要添加您在 db coupons 表中写入的“company_id”列吗?
  • 这是hibernate试图做的Hibernate:插入优惠券(金额,公司ID,结束,图像,消息,价格,开始,标题,类型)值(?,?,?,?, ?, ?, ?, ?, ?) 但对象中的实际值是 (amount, endd, image, message, price, startd, title, type)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-29
  • 2020-10-29
  • 2020-11-13
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多