Bug 68 : make @SequenceGenerator annotation work
Priority 
High
Reported Version 
 
Logged By 
imario
Status 
Fixed
Fixed Version 
1.0.3
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
15/02/2009
Updated 
15/02/2009
Type 
Bug
 
Attachments 
No attachments

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)){

 
imario 16 Feb 07:01
fixed bug with non-null id not working with the patch above

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 Mon Feb 16 07:58:30 CET 2009
@@ -19,9 +19,6 @@
*/
package com.avaje.ebean.server.persist.dml;

-import java.sql.SQLException;
-import java.util.Set;
-
import com.avaje.ebean.server.core.PersistRequest;
import com.avaje.ebean.server.deploy.BeanDescriptor;
import com.avaje.ebean.server.deploy.InheritInfo;
@@ -30,6 +27,9 @@
import com.avaje.ebean.server.persist.dmlbind.BindableId;
import com.avaje.ebean.server.plugin.PluginDbConfig;

+import java.sql.SQLException;
+import java.util.Set;
+
/**
* Meta data for insert handler. The meta data is for a particular bean type. It
* is considered immutable and is thread safe.
@@ -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 = (!nullId || sequenceNextVal==null)?0:1;
+ for (int i = bindStart; i < bindCount; i++) {
if (i > 0) {
request.append(", ");
}

Rob 17 Feb 11:09
Convert from ENH to BUG

I'll convert this over from an ENH to a BUG.

Rob 18 Feb 10:34
Fixed in v1.0.3

Fixed in v1.0.3.

Thanks Mario - excellent work.

woResponse

Upload a file