-->
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.  [ 8 posts ] 
Author Message
 Post subject: Id in superclass
PostPosted: Mon Oct 09, 2006 6:53 am 
Regular
Regular

Joined: Tue Oct 07, 2003 1:13 pm
Posts: 70
Location: Paris, France
Hibernate version:3.2

The case is pretty trivial but i cant solve it with annotations.

I have a base class that handles the id :

Code:
public abstract BaseEntity
{
   private Long id;

   public Long getId() { return id;}
...
}


A subclass ClassA defined like this :
Code:
public ClassA extends BaseEntity
{
   private String name1;

   public String getName1() { return name1;}
...
}


and a subclass ClassB :

Code:
public ClassB extends BaseEntity
{
   private String name2;

   public String getName2() { return name2;}
...
}



Each subclass has a different strategy to generate id : "sequence" for ClassA and "assigned" for classB

I tried many examples, but it failed, how could I add annotations to model that ?
Any help appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 5:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you can't

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Solved
PostPosted: Tue Oct 10, 2006 11:12 am 
Regular
Regular

Joined: Tue Oct 07, 2003 1:13 pm
Posts: 70
Location: Paris, France
Emmanuel, I've found a working solution.
Here it is :


Code:
@MappedSuperclass
@AccessType("field")
public abstract class BaseEntity
{
   @Id
   @GeneratedValue(generator="ID_STORE")
   private Long id;

       public Long getId() { return this.id;}
...
}


@Entity
@javax.persistence.SequenceGenerator(
    name="ID_STORE",
    sequenceName="classA_id_seq",
    allocationSize=1
)
@AccessType("field")
public ClassA extends BaseEntity
{
   private String name1;

   public String getName1() { return name1;}
...
}

@Entity
@GenericGenerator(name = "ID_STORE", strategy = "assigned")   
@AccessType("field")
public ClassB extends BaseEntity
{
   private String name2;

   public String getName2() { return name2;}
...
}


Hope it can help.
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 11:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Funny, I didn't write the code to purposely allow that. Nice to see this working

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Nice
PostPosted: Tue Oct 10, 2006 12:08 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 1:13 pm
Posts: 70
Location: Paris, France
Actually the result is very nice, because it is basically flexible.
Glad to hear the author didn't expect that ! ;-)
Richard


Top
 Profile  
 
 Post subject: Moving SequenceGenerator to the MappedSuperclass
PostPosted: Fri Oct 13, 2006 2:07 pm 
Newbie

Joined: Tue Jun 14, 2005 3:21 pm
Posts: 9
Are there plans to allow for the sequence generator to be moved to the mapped superclass? Currently, that fails -- the sequence generator has to be in the entity, even if you are declaring the id field in the MappedSuperclass, like above.
I am aware that you can declare the sequence once in an xml file, but we are trying to avoid xml declarations wherever possible for the sake of a consistent approach.
Allowing for declaring a SequenceGenerator, or @TypeDef, or other declaration, in a MappedSuperclass, would avoid duplicate declarations.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 17, 2006 4:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
open a JIRA issue, even better with a test case.
Thanks

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Different Sequences
PostPosted: Fri Jan 04, 2008 5:32 pm 
Newbie

Joined: Fri Jan 04, 2008 5:22 pm
Posts: 1
Location: FLORIDA
I have a problem similar to the mentioned above. I have a mapped superclass that has an ID attribute. I have two child classes to the parent.

In my subclass I want to map it to an ID column and want to use a sequence (SEQUENCE1) to generate the ID values.

In another class I want to use another sequence (SEQUENCE2) to generate the ID values.

I tried to do this as explained above but it is using only Sequence and the one mentioned in the BaseClass.

My problem is I want to use two different sequences for different entities.

Is it possible to have two different sequences???


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