Java Hibernate正在外键字段中插入空值

Java Hibernate正在外键字段中插入空值,java,spring,hibernate,jpa,persistence,Java,Spring,Hibernate,Jpa,Persistence,我有两个简单的域对象,如下..使用MYSQL DB @Entity @Table(name="Product") public class Product { @Id @Column(name="productId") @GeneratedValue protected int productId; @Column(name="Product_Name") protected String name; @OneToMany(cascade

我有两个简单的域对象,如下..使用MYSQL DB

@Entity
@Table(name="Product")
public class Product {
    @Id
    @Column(name="productId")
    @GeneratedValue
    protected int productId;
    @Column(name="Product_Name")
    protected String name;
    @OneToMany(cascade = javax.persistence.CascadeType.ALL,mappedBy="product")
    protected List<ProductOption> productoption = new ArrayList<ProductOption>();
还有我的主要方法

public class Createschema {

    public static void main(String[] args) {

        Product product = new Product();
        product.setName("Coffee");

        ProductOption po2 = new ProductOption();
        po2.setTopping("barbeque");
        ProductOption po = new ProductOption();
        po.setTopping("whipcream");
        ProductOption po1 = new ProductOption();
        po1.setTopping("honeymustard");


        List<ProductOption> productoptions = new ArrayList<ProductOption>();

        productoptions.add(po1);
        productoptions.add(po2);
        productoptions.add(po);



        System.out.println("schema!");
        Configuration configuration = new Configuration().configure();

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

        SessionFactory factory = configuration.buildSessionFactory(builder.build());

        Session session = factory.openSession();
        session.beginTransaction();
        product.setProductoption(productoptions);
        session.save(product);
        session.getTransaction().commit();
        session.close();
        System.out.println(product.getName());

        for(int i=0;i<product.getProductoption().size();i++){
        System.out.println(product.getProductOptionsAsListOfStrings().get(i));  
        System.out.println(product.getProductoption().get(i).getTopping());
        }

    }

}
公共类Createschema{
公共静态void main(字符串[]args){
产品=新产品();
产品名称(“咖啡”);
ProductOption po2=新的ProductOption();
po2.烤肉;
ProductOption po=新的ProductOption();
采购订单设置顶部(“whipStream”);
ProductOption po1=新的ProductOption();
po1.糖浆(“蜂蜜芥末”);
List productoptions=new ArrayList();
productoptions.add(po1);
productoptions.add(po2);
productoptions.add(采购订单);
System.out.println(“schema!”);
配置=新配置().configure();
StandardServiceRegistryBuilder=新的StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory=configuration.buildSessionFactory(builder.build());
Session Session=factory.openSession();
session.beginTransaction();
product.setProductoption(productoptions);
保存(产品);
session.getTransaction().commit();
session.close();
System.out.println(product.getName());

对于(int i=0;iIn
Product
change

@OneToMany(mappedBy="product")
List<ProductOption> productoption;
@ManyToOne
@JoinColumn(name="productId")
private Product product;
最后在
Createschema

session.beginTransaction();

Product product = new Product();
product.setName("Coffee");
session.save(product);

ProductOption po2 = new ProductOption();
po2.setTopping("barbeque");
po2.setProduct(product)
ProductOption po = new ProductOption();
po.setTopping("whipcream");
po2.setProduct(product)
ProductOption po1 = new ProductOption();
po1.setTopping("honeymustard");
po2.setProduct(product)

session.save(po2);
session.save(po);
session.save(po1);

session.getTransaction().commit();
session.close();

我同意@sindarthramesh。给出CascadeType的想法。所有的都是关于关系操作的插入、删除等可以由Hibernate自己处理

您没有在ProductOption对象中设置product(product)

您不需要在main方法中创建新的List对象

    List<ProductOption> productoptions = new ArrayList<ProductOption>();

    productoptions.add(po1);
    productoptions.add(po2);
    productoptions.add(po);
您在main方法中创建了另一个ArrayList,它不必要地使用了内存分配。当然,如果您没有在Product(Entity)中创建新对象,则需要执行上述操作。但现在,您可以这样保存

product.setProductoption(productoptions);
//getProductionoption() is getter List type getter method in your Product Entity
product.getProductionoption().add(po1);
product.getProductionoption().add(po2);
product.getProductionoption().add(po);
这是我试图在main方法中修复的代码

    Product product = new Product(); //This must be created on top so that product(id) can be added in ProductOption(Object).

    //Product Options
    ProductOption po2 = new ProductOption();
    po2.setTopping("barbeque");
    po2.setProduct(product); //This will set your foreign key id

    ProductOption po = new ProductOption();
    po.setTopping("whipcream");
    po.setProduct(product); //This will set your foreign key id

    ProductOption po1 = new ProductOption();
    po1.setTopping("honeymustard");
    po1.setProduct(product); //This will set your foreign key id

    //product
    product.setName("Coffee");
    //getProductionoption() is getter List type getter method in your Product Entity
    product.getProductoption().add(po1); 
    product.getProductoption().add(po2);
    product.getProductoption().add(po);
    /*
    Or you can create list and use like this.
    But like I said, it is unnecessary.
    product.setProductoption(productoptions);
    */

    Configuration configuration = new Configuration().configure();

    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

    SessionFactory factory = configuration.buildSessionFactory(builder.build());

    Session session = factory.openSession();
    session.beginTransaction();
    //product.setProductoption(productoptions);
    session.save(product); //Hibernate will automatically join the relationship here.
    session.getTransaction().commit();
    session.close();
    System.out.println(product.getName());

    for(int i=0;i<product.getProductoption().size();i++){
    System.out.println(product.getProductOptionsAsListOfStrings().get(i));  
    System.out.println(product.getProductoption().get(i).getTopping());
Product Product=new Product();//必须在顶部创建此项,以便可以将产品(id)添加到ProductOption(对象)中。
//产品选择
ProductOption po2=新的ProductOption();
po2.烤肉;
po2.setProduct(product);//这将设置您的外键id
ProductOption po=新的ProductOption();
采购订单设置顶部(“whipStream”);
po.setProduct(product);//这将设置您的外键id
ProductOption po1=新的ProductOption();
po1.糖浆(“蜂蜜芥末”);
po1.setProduct(product);//这将设置您的外键id
//产品
产品名称(“咖啡”);
//getProductionoption()是产品实体中的getter列表类型getter方法
product.getProductoption().add(po1);
product.getProductoption().add(po2);
product.getProductoption().add(采购订单);
/*
或者,您可以创建列表并像这样使用。
但正如我所说,这是没有必要的。
product.setProductoption(productoptions);
*/
配置=新配置().configure();
StandardServiceRegistryBuilder=新的StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory=configuration.buildSessionFactory(builder.build());
Session Session=factory.openSession();
session.beginTransaction();
//product.setProductoption(productoptions);
session.save(product);//Hibernate将在此处自动加入关系。
session.getTransaction().commit();
session.close();
System.out.println(product.getName());

对于(int i=0;iThanks!。我犯了一个非常愚蠢的错误。我忘了为每个选项设置产品。我以为hibernate会在内部这样做。嗨,Sandhu,为什么我们要分别保存产品、po2、po和po1?为什么我不能用po、po1和po2填充产品,然后只保存产品。然后我应该在内部保存所有po、po1和po2。我正在尝试这一点不起作用。请帮助。我同意@Abdul的观点,给予CascadeType.ALL是为了让插入、删除等操作可以由Hibernate自己处理。不是吗?
//getProductionoption() is getter List type getter method in your Product Entity
product.getProductionoption().add(po1);
product.getProductionoption().add(po2);
product.getProductionoption().add(po);
    Product product = new Product(); //This must be created on top so that product(id) can be added in ProductOption(Object).

    //Product Options
    ProductOption po2 = new ProductOption();
    po2.setTopping("barbeque");
    po2.setProduct(product); //This will set your foreign key id

    ProductOption po = new ProductOption();
    po.setTopping("whipcream");
    po.setProduct(product); //This will set your foreign key id

    ProductOption po1 = new ProductOption();
    po1.setTopping("honeymustard");
    po1.setProduct(product); //This will set your foreign key id

    //product
    product.setName("Coffee");
    //getProductionoption() is getter List type getter method in your Product Entity
    product.getProductoption().add(po1); 
    product.getProductoption().add(po2);
    product.getProductoption().add(po);
    /*
    Or you can create list and use like this.
    But like I said, it is unnecessary.
    product.setProductoption(productoptions);
    */

    Configuration configuration = new Configuration().configure();

    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

    SessionFactory factory = configuration.buildSessionFactory(builder.build());

    Session session = factory.openSession();
    session.beginTransaction();
    //product.setProductoption(productoptions);
    session.save(product); //Hibernate will automatically join the relationship here.
    session.getTransaction().commit();
    session.close();
    System.out.println(product.getName());

    for(int i=0;i<product.getProductoption().size();i++){
    System.out.println(product.getProductOptionsAsListOfStrings().get(i));  
    System.out.println(product.getProductoption().get(i).getTopping());