简体   繁体   English

休眠许多到一个外键不起作用

[英]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 下一个项目Java文件

@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 在运行两个tanles时,一个购物车应该有很多物品,但是

# 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 如您所见,两个表均已更新,但本例中的外键在我使用过joincolumn的所有者类中未更新

You are not setting Solocart anywhere to your Soloitem s. 您没有设置Solocart任何地方你Soloitem秒。 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. 您在实体SolocartSoloitems之间建立了双向关系,因此在代码中,您需要从实体双方维护这种关系。

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: 因此,基于此,在您的代码中,您只是将Soloitems设置为Solocart,但却错过了将Solocart设置为Soloitems,因此如Predrag所述,添加以下代码行来保持这种关系:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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