-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping by Annotations (newbie)
PostPosted: Tue Mar 28, 2006 9:56 am 
Newbie

Joined: Tue Mar 28, 2006 9:36 am
Posts: 4
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,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.