Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Custom sequence
Custom sequence [message #754570] Thu, 03 November 2011 10:40 Go to next message
A.Rothe  is currently offline A.Rothe Friend
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 #754578 is a reply to message #754570] Thu, 03 November 2011 11:10 Go to previous messageGo to next message
A.Rothe  is currently offline A.Rothe Friend
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
Re: Custom sequence [message #754619 is a reply to message #754578] Thu, 03 November 2011 14:43 Go to previous messageGo to next message
A.Rothe  is currently offline A.Rothe Friend
Messages: 12
Registered: November 2011
Location: Leipzig, Germany
Junior Member
I have implemented a simple test case, but it seems, that EclipseLink doesn't call the generator for embedded Ids. So I have no chance to create a PersistId. EclipseLink generates also an insert statement with a null value for the Id. So it is not possible to use identity columns on Derby. The only thing I can do, is to create my own Id value on @PrePersist. Sad
Re: Custom sequence [message #754620 is a reply to message #754578] Thu, 03 November 2011 14:43 Go to previous messageGo to next message
A.Rothe  is currently offline A.Rothe Friend
Messages: 12
Registered: November 2011
Location: Leipzig, Germany
Junior Member
I have implemented a simple test case, but it seems, that EclipseLink doesn't call the generator for embedded Ids. So I have no chance to create a PersistId. EclipseLink generates also an insert statement with a null value for the Id. So it is not possible to use identity columns on Derby. The only thing I can do, is to create my own Id value on @PrePersist. :(
Re: Custom sequence [message #755228 is a reply to message #754619] Mon, 07 November 2011 18:21 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You need to put the @GeneratedValue on the id in the embeddable, not on the @EmbeddedId.


James : Wiki : Book : Blog : Twitter
Previous Topic:mysterious logging
Next Topic:Partition - id on new entities?
Goto Forum:
  


Current Time: Tue Apr 16 18:42:56 GMT 2024

Powered by FUDForum. Page generated in 0.78685 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top