简体   繁体   中英

hibernate many to one foreign key not working

the follwing is my hibernate example for one to many relationship

cart java class
@Entity
@Table(name="cart")
public class Solocart {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="carts_id")
    int id;
    @Column(name="cust_name")

    String name;
    @OneToMany(mappedBy="cartitem")
    Set<Soloitems>soloitem;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Set<Soloitems> getSoloitem() {
        return soloitem;
    }
    public void setSoloitem(Set<Soloitems> soloitem) {
        this.soloitem = soloitem;
    }




}

next items java file

@Entity
@Table(name="cartitem")
public class Soloitems {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="cart_id")
    private int id;
    @Column(name="no_item")
    private int number;
    @ManyToOne
    @JoinColumn(name="carts_id")
    private Solocart cartitem;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public Solocart getCartitem() {
        return cartitem;
    }
    public void setCartitem(Solocart cartitem) {
        this.cartitem = cartitem;
    }

impl code

Session sn=Util.getSessionFactory().openSession();
    sn.beginTransaction();
    Solocart crt=new Solocart();
    crt.setName("solomon");
    Soloitems itm1=new Soloitems();
    Soloitems itm2=new Soloitems();
    itm1.setNumber(5);
    itm2.setNumber(8);
    Set<Soloitems>values= new HashSet<Soloitems>();
    values.add(itm1);
    values.add(itm2);
    crt.setSoloitem(values);
    sn.save(crt);
    sn.save(itm2);
    sn.save(itm1);
    sn.getTransaction().commit();
    sn.close();
    System.out.println("sucessfully created");

here one cart should have many items while running both the tanles were updated but

# cart_id, no_item, carts_id
     '1', '   8',     NULL
      '2', '  5',     NULL

second table

# carts_id, cust_name
    '1', '   solomon'

as you see both the tables has been updated but the foreignkey herein this case carts_id didnt get updated in the owner class i have used joincolumn

You are not setting Solocart anywhere to your Soloitem s. Try adding this to your code

itm1.setCartitem(crt);
itm2.setCartitem(crt);

You have a bi-directional relationship between entities Solocart and Soloitems so in your code you need to maintain the relationship from both sides of entities.

So based on this, in your code you are just setting the Soloitems to Solocart but you missed to set the Solocart to Soloitems, so as mentioned by Predrag add below lines of code to maintain the relationship:

itm1.setCartitem(crt);
itm2.setCartitem(crt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM