0

Iv'e been trying to understand Hibernate mappings but I just don't get it. using hibernate in a spring project. mysql workbench for db.

each table has an Id, coupon and company. when a company creates a coupon I want the coupons ID and the companies ID to go to a third table that is called companyCoupon and map it there so:

a company may have MANY coupons. a coupon may be assosicated with ONE company.

company id = 1; coupon id = 1;

companycoupon - compid = 1; coupid = 1;

ive created the third table and forgein keys that map to the other tables.

anyone? any ideas? good articles to read?

EDIT :

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

}

enter image description here

Roman Sterlin
  • 1,485
  • 3
  • 10
  • 25
  • To prevent downvoting please add only the information which is necessary to understand your problem and define a clear question. – Tobias Liefke Sep 05 '19 at 11:31

1 Answers1

0

Okay Then you no need to add third table only add list of coupons with 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;

This is static example you can get idea

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);
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • a company may have MANY coupons. a coupon may be assosicated with ONE company. – Roman Sterlin Sep 03 '19 at 12:14
  • java.sql.SQLSyntaxErrorException: Unknown column 'company_id' in 'field list' – Roman Sterlin Sep 03 '19 at 12:54
  • I dont have the field company_id ANYWHERE in the program, not in code, not in database any idea where it came from? lol, its basicaly making up a new field in the coupon object adding "company_id" into the coupon objeect – Roman Sterlin Sep 03 '19 at 12:55
  • do i need to add the "company_id" column that you wrote inside db coupons table? – Roman Sterlin Sep 03 '19 at 12:57
  • this is what hibernate is trying to do Hibernate: insert into coupons (amount, company_id, endd, image, message, price, startd, title, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?) but the real values in the object are (amount, endd, image, message, price, startd, title, type) – Roman Sterlin Sep 03 '19 at 12:58
  • company_id column is id of company table you need add company id column in coupons tables in sql – mandy rayat Sep 03 '19 at 13:04
  • ive added a company_id colum in the coupon table, still not adding the id of the company, do i make it a forgien key? – Roman Sterlin Sep 03 '19 at 13:05
  • yes. make it a forgien key.please also set user when you saving coupons data.like this..coupons.setUser(user); user is the object which user you saving with repect to coupons – mandy rayat Sep 03 '19 at 13:07
  • hmm..still null on the compnay_id even after i made it a forgien key – Roman Sterlin Sep 03 '19 at 13:09
  • edited & included the code if you could take a look maybe im missing something – Roman Sterlin Sep 03 '19 at 13:13
  • coupon is created but company_id inside coupon table is null – Roman Sterlin Sep 03 '19 at 13:20
  • add company in construtor of coupons and please set user object in your java code while you saving coupons data – mandy rayat Sep 03 '19 at 13:23
  • thanks baby jesus it works, now the question is : a customer may also have a coupon, what if he has many coupons? how would that be saved? in a seperate table? or in the customer table as a list of coupons how would that look in the db – Roman Sterlin Sep 03 '19 at 14:02