Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Custom sequence
Custom sequence [message #754571] Thu, 03 November 2011 06:40 Go to next message
A.Rothe  is currently offline A.Rothe
Messages: 12
Registered: November 2011
Location: Leipzig, Germany
Junior Member
Hi,

I try to encapsulate the Id of the entities within its own class to have the possibility to switch to another implementation very fast.

@Embeddable
public class PersistId
implements Serializable {

private static final long serialVersionUID = 8328103682151530154L;

@Basic
private Integer id;

public PersistId() {
this.id = null;
}

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

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

public boolean equals(Object o) {
if (!(o instanceof PersistId)) {
return false;
}
PersistId d = (PersistId) o;
return (id == d.id) || (id != null && id.equals(d.id));
}

public int hashCode() {
return (id == null ? 0 : id.hashCode());
}
}

This is my current PK implementation class. The entities use the following abstract class:

@MappedSuperclass
public abstract class AbstractEntity {

private static final long serialVersionUID = -8574667350713432415L;

@EmbeddedId
@GeneratedValue(strategy = GenerationType.AUTO)
private PersistId id;

public static boolean isId(PersistId id) {
return (id != null && id.getId() > 0);
}

public boolean hasId() {
return isId(getId());
}

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

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

Now an example entity:


@Entity
@Table(name = "CL_FUNCTIONS")
public class Function extends AbstractEntity {

private static final long serialVersionUID = -8931494442608402547L;

@Column(unique = true, nullable = false, name = "DESCR")
private String description;

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

}


The problem is, I don't get any Id from the generator. I have somewhere read, that composite keys can not be used with generators. So the idea is now, to create my own generator to handle that. But the documentation is not really helpful on that point. Can anyone explain, how I can write a simple Id generator, which uses the generation type of the database system (identity, sequence, table) and returns a filled instance of PersistId?

Thanks a lot
Andre
Re: Custom sequence [message #754579 is a reply to message #754571] Thu, 03 November 2011 07:10 Go to previous message
A.Rothe  is currently offline A.Rothe
Messages: 12
Registered: November 2011
Location: Leipzig, Germany
Junior Member
There is an example for UUIDs on wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing, but I don't know, what I have to write on getGeneratedValue() to get a PersistId instance filled with the next Id from the table of the entity. I use Derby at the moment and every table has an identity column, which generates the numbers.

Thanks
Andre
Previous Topic:toNumber() expression with custom precision
Next Topic:How to upgrade database schema built with EclipseLink?
Goto Forum:
  


Current Time: Wed Aug 27 17:10:40 EDT 2014

Powered by FUDForum. Page generated in 0.02088 seconds