Custom sequence [message #754571] |
Thu, 03 November 2011 10:40 |
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.01984 seconds