Hello,
In my model I have a series of classes that extend an abstract class named Resource and I want those classes to have mutually exclusive id's (the id being their primary key) because I want to be able to reference any Resource by unique id. I created a sequence in the database (PostgreSQL 8) named "SEQUENCE_GLOBAL_RESOURCE_ID". I translated this to code like this:
@EmbeddableSuperclass(access = AccessType.FIELD)
public abstract class Resource {
@Id(generate = GeneratorType.SEQUENCE, generator = "GENERATOR_SEQUENCE_GLOBAL_RESOURCE_ID")
@Column(name = "ID")
private int id;
public int getId() {return id;}
public void setId(int resourceId) {this.id = resourceId;}
}
Example of entity which extends Resource:
@Entity(access = AccessType.FIELD)
@Table(name = "ORGANIZATIONS")
@SequenceGenerator(name = "GENERATOR_SEQUENCE_GLOBAL_RESOURCE_ID", sequenceName = "SEQUENCE_GLOBAL_RESOURCE_ID")
public class Organization extends Resource implements Serializable {
@Basic
@Column(name = "NAME", unique = true, nullable = false)
private String name;
// and other properties
}
Now if I retrieve an Organization, the instance I get has all the fields correct (as in the database) except for id which is 0! Does anyone have any idea why this is happening?
Thanks.