Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate is inserting null values in foreign key field

I have 2 simple domain objects as follows..with 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>();

and second object

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

and my main method.

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

    }

}

For some reason hibernate is inserting null values in foreign key field of productoptions. I tried changing cascading styles, generation strategies, but nothing worked. Making nullable=false throws error . Data gets inserted but in the productoptions table foriegn key field is null. Please help

like image 343
Nikhil Avatar asked Sep 02 '25 04:09

Nikhil


1 Answers

In Product change

@OneToMany(mappedBy="product")
List<ProductOption> productoption;

In ProductOption change

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

And finally in 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();
like image 84
Sandhu Santhakumar Avatar answered Sep 04 '25 23:09

Sandhu Santhakumar