you can annotate your entity with @SequenceGenerator at class or property level to tell the system the database sequence name to use.
e.g.
@Entity
@SequenceGenerator(name="AD_SEQ_NAME", sequenceName="AD_SEQ")
public class AuditLog
{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AD_SEQ_NAME")
private Long id;
means the id should be set using the database sequence with name AD_SEQ.
I've created a patch which
1) fixes a bug when inserting the sql for the db-sequence get, the resulting sql looked like something like:
insert into (id, desc) values (, the-seq-sql?, ?) you see the "," and "?" in the the-seq-sql part.
2) honors the @SequenceGenerator configuration. With this the namingconvention for creating the sequence name in avaje is no longer used.
Patch:
Index: ../ebean/src/com/avaje/ebean/server/persist/dml/InsertMeta.java
===================================================================
--- ../ebean/src/com/avaje/ebean/server/persist/dml/InsertMeta.java (revision 127)
+++ ../ebean/src/com/avaje/ebean/server/persist/dml/InsertMeta.java Sun Feb 15 10:14:48 CET 2009
@@ -207,6 +207,9 @@
request.append(") values (");
+ // ensure prefixes are reset for the value list
+ request.setInsertSetMode();
+
// the number of scalar properties being bound
int bindCount = request.getBindCount();
@@ -214,7 +217,8 @@
addNullUidValue(request);
}
- for (int i = 0; i < bindCount; i++) {
+ int bindStart = sequenceNextVal==null?0:1;
+ for (int i = bindStart; i < bindCount; i++) {
if (i > 0) {
request.append(", ");
}
Index: ../ebean/src/com/avaje/ebean/server/naming/SequenceNaming.java
===================================================================
--- ../ebean/src/com/avaje/ebean/server/naming/SequenceNaming.java (revision 127)
+++ ../ebean/src/com/avaje/ebean/server/naming/SequenceNaming.java Sun Feb 15 10:06:47 CET 2009
@@ -88,6 +88,11 @@
public String getName(DeployBeanDescriptor desc) {
String baseTable = desc.getBaseTable();
+ if (desc.getIdGeneratorName() != null)
+ {
+ // user provided generator name is correct for the db already, no need to do further processing
+ return desc.getIdGeneratorName();
+ }
String uidColumn = null;
boolean includeColumn = properties.getPropertyBoolean("namingconvention.sequence.includecolumn", false);
Index: ../ebean/src/com/avaje/ebean/server/deploy/parse/AnnotationParser.java
===================================================================
--- ../ebean/src/com/avaje/ebean/server/deploy/parse/AnnotationParser.java (revision 127)
+++ ../ebean/src/com/avaje/ebean/server/deploy/parse/AnnotationParser.java Sun Feb 15 10:03:42 CET 2009
@@ -81,4 +81,22 @@
}
return a;
}
+
+ /**
+ * Return the annotation for the property.
+ *
+ * Looks
+ * first at the field and
+ * then at the getter method.
+ * then at class level
+ *
+ */
+ @SuppressWarnings("unchecked")
+ protected Annotation find(DeployBeanProperty prop, Class annClass) {
+ Annotation a = get(prop, annClass);
+ if (a == null) {
+ a = prop.getOwningType().getAnnotation(annClass);
-}
+ }
+ return a;
+ }
+}
Index: ../ebean/src/com/avaje/ebean/server/deploy/parse/AnnotationFields.java
===================================================================
--- ../ebean/src/com/avaje/ebean/server/deploy/parse/AnnotationFields.java (revision 127)
+++ ../ebean/src/com/avaje/ebean/server/deploy/parse/AnnotationFields.java Sun Feb 15 10:03:42 CET 2009
@@ -37,6 +37,7 @@
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
+import javax.persistence.SequenceGenerator;
import com.avaje.ebean.annotation.Formula;
import com.avaje.ebean.server.deploy.IdentityGeneration;
@@ -168,6 +169,13 @@
private void readGenValue(GeneratedValue gen, DeployBeanProperty prop) {
String genName = gen.generator();
+ SequenceGenerator sequenceGenerator = (SequenceGenerator) find(prop, SequenceGenerator.class);
+ if (sequenceGenerator != null) {
+ if (sequenceGenerator.name().equals(genName)) {
+ genName = sequenceGenerator.sequenceName();
+ }
+ }
+
GenerationType strategy = gen.strategy();
if (strategy == GenerationType.IDENTITY) {
@@ -175,6 +183,9 @@
} else if (strategy == GenerationType.SEQUENCE) {
descriptor.setIdentityGeneration(IdentityGeneration.DB_SEQUENCE);
+ if (genName != null && genName.length() > 0) {
+ descriptor.setIdGeneratorName(genName);
+ }
} else if (strategy == GenerationType.AUTO) {
if (prop.getPropertyType().equals(UUID.class)){