-->
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: Inheritence, polymorphism and Id problem
PostPosted: Wed Mar 07, 2012 2:09 pm 
Newbie

Joined: Wed Mar 11, 2009 7:15 am
Posts: 13
I have abstract Place,
City extends Place
Region extends Place.

Place
^^
| |
| City
|
Region

Place:
Code:
@MappedSuperclass
public abstract class Place {
   
   private String name;
   
   @Transient
   public abstract void setId(Long id);
   @Transient
   public abstract Long getId();
   
   @Column
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

City:
Code:
@Entity
public class City extends Place{

   private Long id;
   
   @Override
   @Column
   @Id
   public Long getId() {
      return id;
   }
   @Override
   public void setId(Long id) {
      this.id = id;
   }
}


Region:
Code:
@Entity
public class Region extends Place{

   private Long id;
   
   @Override
        @Column
        @Id
   public Long getId() {
      return id;
   }
   @Override
   public void setId(Long id) {
      this.id = id;
   }   
   
}


In Hotel is relation to Place. It can be relation to City or Region. So far, I used hibernate @Any and it works.
Code:
@Entity
public class Hotel {

   private Long id;
   private Place place;

   @Any(metaColumn = @Column(name = "place_type"), fetch = FetchType.LAZY)
   @AnyMetaDef(idType = "long", metaType = "string", metaValues = {
         @MetaValue(value = "C", targetEntity = City.class),
         @MetaValue(value = "R", targetEntity = Region.class) })
   @Cascade({ org.hibernate.annotations.CascadeType.ALL })
   @JoinColumn(name = "place_id")
   public Place getPlace() {
      return place;
   }

   public void setPlace(Place place) {
      this.place = place;
   }
        //get set id
}


But now, I can't use @Any or any other special hibernate feature. I have to use only JPA.
Is it possible to obtain the same result using only JPA, without @Any?
Inheritance types:
TABLE_PER_CLASS - I found that this strategy doesn't offer polymorphism, so it is not for my case.
JOINED - Trying to use this strategy, I have problem with id. Field with @Id must be declared in superclass. So I can't put id in subclasses? But, I use PostgreSQL and I have to indicate sequence generator name in subclasses.
Code:
@Id
@SequenceGenerator(name="city_seq", sequenceName="city_seq") // for City
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="city_seq")
public Long getId() {
   return id;
}

and region_seq for Region.
SINGLE_TABLE - the same problem as with joined strategy.
MAPPEDSUPERCLASS - Class with this annotatnion is not entity, so it can not be place as relation @ManyToOne
Code:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinColumn(name = "place_id")
   public Place getPlace() {
      return place;
   }

Help please...


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.