Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for sequence (nextval) on insert of new record #41

Closed
manfreddev opened this issue Nov 8, 2011 · 5 comments
Closed

add support for sequence (nextval) on insert of new record #41

manfreddev opened this issue Nov 8, 2011 · 5 comments

Comments

@manfreddev
Copy link

@manfreddev manfreddev commented Nov 8, 2011

Now you can create a sequence by using a TemplateExpression...
Expression nextCustomerId = TemplateExpressionImpl.create(String.class, "nextval('customer_id_seq')");

Could this be structurally resolved within the framework?

https://groups.google.com/forum/#!topic/querydsl/k5uKUq4DngM

@timowest
Copy link
Member

@timowest timowest commented Nov 9, 2011

I added a convenience file with common Expressions :

public final class SQLExpressions {

  public static final Expression<Object[]> all = Wildcard.all;

  public static final Expression<Long> countAll = Wildcard.count;

  public static final SimpleExpression<Long> nextval(String sequence) {
    return nextval(Long.class, sequence);
  }    

  public static final <T extends Number> SimpleExpression<T> nextval(Class<T> type, String sequence) {
    return SimpleOperation.create(type, SQLTemplates.NEXTVAL, ConstantImpl.create("seq"));
  }

  private SQLExpressions() {}

}

Is this sufficient?

@timowest
Copy link
Member

@timowest timowest commented Nov 19, 2011

Released in 2.2.5

@timowest timowest closed this Nov 19, 2011
@inspell
Copy link

@inspell inspell commented Oct 20, 2015

I think that this is not enough though. When I try to use the NEXTVAL with Derby it throws error.
For example this:
Expression s = SimpleOperation.create(BigInteger.class, SQLOps.NEXTVAL, ConstantImpl
.create("test"));
s.toString(); ---> says unknown operation with operator com.mysema.query.sql.SQLOps#NEXTVAL and args [test]

At some point when I try to execute an Update query and set a field to a value of a sequence (JPAQueryFactory.set(qDoc.documentGenerationTrackingId, s))
I get a NPE because it seems that the precedence for the NEXTVAL op cannot be found (it is null).
I am using com.mysema.querydsl:querydsl-core:3.6.0

@timowest
Copy link
Member

@timowest timowest commented Oct 20, 2015

nextval is not supported in Querydsl JPA, that's why you get an exception. Could you provide the stacktrace for the NPE?

@inspell
Copy link

@inspell inspell commented Oct 21, 2015

This is the stack trace:

java.lang.NullPointerException
at com.mysema.query.types.Templates.getPrecedence(Templates.java:310)
at com.mysema.query.support.SerializerBase.visitOperation(SerializerBase.java:277)
at com.mysema.query.jpa.JPQLSerializer.visitOperation(JPQLSerializer.java:432)
at com.mysema.query.support.SerializerBase.visit(SerializerBase.java:249)
at com.mysema.query.support.SerializerBase.visit(SerializerBase.java:42)
at com.mysema.query.types.OperationImpl.accept(OperationImpl.java:91)
at com.mysema.query.support.SerializerBase.handle(SerializerBase.java:105)
at com.mysema.query.support.SerializerBase.handle(SerializerBase.java:137)
at com.mysema.query.jpa.JPQLSerializer.serializeForUpdate(JPQLSerializer.java:254)
at com.mysema.query.jpa.impl.JPAUpdateClause.execute(JPAUpdateClause.java:61)

The is how the sequence is created:
Expression s = SimpleOperation.create(BigInteger.class, SQLOps.NEXTVAL, ConstantImpl
.create("sequence-name"));
...
return new JPAQueryFactory(entityManager)
.update(qclass)
.where(predicate)
.set....
...
.set(qclass.property, s)
.execute();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants