-->
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.  [ 2 posts ] 
Author Message
 Post subject: Non-persistent base class with abstract get/set for ID
PostPosted: Mon Oct 18, 2010 6:31 pm 
Newbie

Joined: Fri Feb 25, 2005 4:11 pm
Posts: 4
Location: Fayetteville, GA
Hi all,

I'm attempting to put some base functionality in a base class for all persistent entities. To that end I must be able to call get/setId on all entities so I put an abstract definition of such in the base class:
Code:
  @Id     
  public abstract Serializable getId();

  public abstract void setId(Serializable id);

The type is Serializable because some classes have compound keys.

Then in a subclass:
Code:
    @Column(name="id", insertable=false, updatable=false)
    @org.hibernate.annotations.Type(type="java.lang.Long")
    @javax.persistence.SequenceGenerator(name = "generator", allocationSize = 1)
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.SEQUENCE, generator = "generator")
    @Override
    public Serializable getId() {
        return this.id;
    }
   
    @Override
    public void setId(Serializable id) {
        this.id = (Long)id;
    }

what this gets me is an error stating that I have to set the id manually, as if it doesn't recognize the generation strategy. I've previously defined all annotations on the subclass, and gotten errors indicating that hibernate is trying to do an insert with the ID field listed twice (duplicate column).

Has anyone gotten something similar to this to succeed?

Hibernate: 3.5.5 Final in JPA 'mode'
Database: Oracle 10g
Java: 6


Top
 Profile  
 
 Post subject: Re: Non-persistent base class with abstract get/set for ID
PostPosted: Tue Oct 19, 2010 4:43 am 
Beginner
Beginner

Joined: Fri Nov 14, 2008 7:34 pm
Posts: 24
in the code i see missed sequence name and @Id should be moved in subclass


i'd better refactor with generics:

public interface Entity<ID extends Serializable> {
ID getId();
void setId(ID id);
}

public class User implements Entity<Long> {

@Id
public Long id;

}


compound keys should be moved to separate class that is Serializable.

public class UserLogRecord implements Entity<UserLogRecord.Id>, Serializable {

public Id getId() {
return id;
}

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

public class Id implements Serializable {
}
......
}


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