-->
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.  [ 3 posts ] 
Author Message
 Post subject: How can make it with annotations
PostPosted: Wed Feb 01, 2006 11:52 pm 
Newbie

Joined: Mon Sep 12, 2005 9:47 pm
Posts: 4
Hi:

First thing, my english is very bad.
The problem:
Code:
Class
|-------------|
|             |
|             |
|             |<----|
|             |     |
|-------------|     |
        |           |
        |-----------|


No very good luking, but no have other resource. I have a field of my class that is a List that object the same type of my class. How can make it with annotations.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 7:28 am 
Beginner
Beginner

Joined: Wed Aug 24, 2005 5:32 am
Posts: 23
Hi,
You can do it using the @OneToMany and @ManyToOne, just don't forget to set the appropriate cascade type for your case

Code:
package par.common;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;

@Entity
public class SampleA {

   @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
   private int id;
   
   private String name;
   
   @OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
   private Collection<SampleA> collection = new LinkedList<SampleA>();

   @ManyToOne
   private SampleA parent;
   
   
   public Collection<SampleA> getCollection() {
      return collection;
   }

   public void setCollection(Collection<SampleA> collection) {
      this.collection = collection;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
   public void addChild(SampleA c) {
      c.parent = this;
      collection.add(c);
   }
      
}



Eyal Lupu


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 11:00 pm 
Newbie

Joined: Mon Sep 12, 2005 9:47 pm
Posts: 4
Thanks Eyal, my problem was solve. Thanks again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.