我有2个简单的域对象,如下所示。

@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>();


和第二个对象

@Entity
@Table(name="ProductOption")
public class ProductOption{


    /**
     *
     */

    @Id
    @Column(name="product_option_id")
    @GeneratedValue
    protected int productOptionId;


    //@JoinColumn(name="productId")
    @ManyToOne()
    protected Product product;

    @Column(name="ProductTopping")
    private String topping;


和我的主要方法。

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());
        }

    }

}


出于某种原因,hibernate在productoptions的外键字段中插入了空值。
我尝试更改级联样式,生成策略,但没有任何效果。使nullable = false引发error。插入了数据,但在productoptions表中,foreign键字段为null。
请帮忙

最佳答案

Product更改

@OneToMany(mappedBy="product")
List<ProductOption> 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();

10-08 16:13