Hi,
I have the following mapping classes:
Code:
Product.java
========
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
@javax.persistence.SequenceGenerator(
       name="TYDX_SEQ_STORE",
       sequenceName="tydx_seq"
   )
public class Product implements Serializable{
    
   private static final long serialVersionUID = 501928014799075508L;
   
   private long id;
   private String name;
    private Category category;
    
    public Product() {
      
   }
  
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TYDX_SEQ_STORE")
   public long getId() {
      return id;
   }
    
   public void setId(long id) {
      this.id = id;
   }
   
   @ManyToOne(cascade = {CascadeType.ALL})
   @JoinColumn(name="cat_id")
   public Category getCategory() {
      return category;
   }
   
   public void setCategory(Category myCategory) {
      this.category = myCategory;
   }
   
   @Column(name = "prod_name", nullable = false, length=100)
   public String getName() {
      return name;
   }
   
   public void setName(String name) {
      this.name = name;
   }
   
}
Category.java
==========
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
@javax.persistence.SequenceGenerator(
       name="TYDX_SEQ_STORE",
       sequenceName="tydx_seq"
   )
public class Category implements Serializable{
   private static final long serialVersionUID = 2627757500106285177L;
   
   private long id;
    private String name;
    private List<Product>  myProduct;
  
    public Category() {
      
   }
   @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TYDX_SEQ_STORE")
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }
   @OneToMany(mappedBy="category")
   public List<Product> getMyProduct() {
      return myProduct;
   }
   public void setMyProduct(List<Product> myProduct) {
      this.myProduct = myProduct;
   }
   @Column(name = "cat_name", nullable = false, unique=true, length=100)
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}
I am trying to insert data into the database with the following code:
Code:
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HibernateTest {
   /**
    * @param args
    */
   public static void main(String[] args) {
      
      
      
      Product prod = new Product();
      prod.setName("Digital camera");
            
      Category cat = new Category();
      cat.setName("Electronics");
      
            
      prod.setCategory(cat);
      List<Product> list = new ArrayList<Product>();
      list.add(prod);
      prod = new Product();
      prod.setName("TV");
      prod.setCategory(cat);
      list.add(prod);
      cat.setMyProduct(list);
      
      Session session = HibernateUtil.currentSession();
      Transaction tx= session.beginTransaction();
      
      session.save(prod);
      
      tx.commit();
      HibernateUtil.closeSession();
      
      
   }
}
With this code I'm expecting 1 row in the Category table and 2 rows in the Product table but I get only 1 row in each table. In other words instead of a row for "digital camera" and "TV" I'm seeing only "TV".
What am I doing wrong?
Regards,