-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping by Annotations (newbie)
PostPosted: Tue Mar 28, 2006 10:03 am 
Newbie

Joined: Tue Mar 28, 2006 9:36 am
Posts: 4
Hi,

I only noticed after posting in Hibernate users that this forum is the most appropriate for my question. I apologise for the cross posting.

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  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 6:58 am 
Newbie

Joined: Fri Mar 24, 2006 9:31 am
Posts: 10
Location: Berlin, Germany
Did you generate your DDL from you EJB annotations ? If yes, how you did ?

I am looking for a tool that uses EJB3 annotations for generating hibernate mpping files and DDL.


Last edited by bibodo on Wed Mar 29, 2006 8:53 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 7:24 am 
Newbie

Joined: Tue Mar 28, 2006 9:36 am
Posts: 4
bibodo wrote:
Did you generate your DDL fro you EJB annotations ? If yes, how you did ?

I am looking for a tool that uses EJB3 annotations for generating hibernate mpping files and DDL.


I don't know of any such tool. I created them by hand.
I have solved the problem. I added a CascadeType.ALL attribute to oneToMany mapping in the Category class. Everything is now working as wished.

E


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 4:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
bibodo wrote:
Did you generate your DDL from you EJB annotations ? If yes, how you did ?

I am looking for a tool that uses EJB3 annotations for generating hibernate mpping files and DDL.


Hibernate Tools allows you to generate DDL from the annotated classes, even hbm files.

What is the reason for generating hbm files?

_________________
Emmanuel


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

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.