-->
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: ID in abstract class, with entity-specific sequence name
PostPosted: Fri Oct 27, 2006 9:53 pm 
Newbie

Joined: Wed Sep 20, 2006 10:15 pm
Posts: 12
Hi folks,

Please excuse me if this question has been asked before. I have searched for a hint/solution but must've searched for the wrong keywords. I'm not entirely sure how to find this.

I have an abstract class which contains a number of things, one of which is the Id property. All the other entities extends this class. I previously used the traditional XML mapping and this worked just fine. However, I would like to get rid of the XML files and move all the mapping into annotations.

Here's what I have:

Code:
@MappedSuperclass
public abstract class AbstractObject implements Serializable {
   
   @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
   private long id = 0;
   
   public long getId(){return id;}
   private void setId(long id){
      this.id = id;
   }
   
   //....some other stuff...
}

@Entity
public class Photo extends AbstractObject {
   
   private static final long serialVersionUID = 0;
   
   public Photo(){}
   
   @ManyToOne
   @JoinColumn(name="dogID")
   private Dog dog;
   public Dog getDog(){return dog;}
   public void setDog(Dog dog){
      this.dog = dog;
   }
   
   @Lob
   @Column(columnDefinition="bytea")
   private byte[] image;
   public byte[] getImage(){return image;}
   public void setImage(byte[] image){
      this.image = image;
   }
}


This works okay for reading data from the DB. However, it fails when I try to persist entities. I'm using PostgreSQL and, since I have not specified the sequence name, it's looking for the default "hibernate_sequence".

Can someone please help me with this? I need to know how to specify the sequence name.

Any hints/suggestions will be greatly appreciated.

Many thanks,
Dany.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 11:06 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 9:28 pm
Posts: 25
Here's how I've done it with oracle 9i as the back end ... (I don't claim that this is the best way!)

On the id field in my abstract @MappedSuperclass:

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "TableSequence")



Then in my concrete sub classes:

@Entity
@SequenceGenerator(name = "TableSequence", sequenceName = "<sequence name goes here>")


We use a different sequence for each table. I'm not sure how to do it it you want to share the same sequence for each table (maybe just move the @SequenceGenerator annotation up to the abstract super class?)

Hope this helps,
-Jay


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 10:35 pm 
Newbie

Joined: Wed Sep 20, 2006 10:15 pm
Posts: 12
Thanks Jay, that did the trick!


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.