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

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

Comments

Projects
None yet
3 participants
@manfreddev

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

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Nov 9, 2011

Member

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?

Member

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

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Nov 19, 2011

Member

Released in 2.2.5

Member

timowest commented Nov 19, 2011

Released in 2.2.5

@timowest timowest closed this Nov 19, 2011

@inspell

This comment has been minimized.

Show comment
Hide comment
@inspell

inspell 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

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

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 20, 2015

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@inspell

inspell 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();

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