10

I've seen the similar question, but in my situation it doesn't work. I need to get the next value of the sequence.

Model class:

@Entity
@Table(name = "item")
public class Item {
    @Id
    @SequenceGenerator(name = "id_seq", sequenceName = "item_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
    @Column(name = "id", updatable = false)
    protected Long id;
}

Jpa Repository:

public interface ItemRepository extends JpaRepository<Item, Long> {
    List<Item> findFirst10ByOrderByPublicationDateDesc();

    @Query(value = "SELECT item_id_seq.nextval FROM item", nativeQuery = 
    true)
    Long getNextSeriesId();
}

I would appreciate any help.

2 Answers 2

19

I've found solution:

@Query(value = "SELECT nextval('item_id_seq')", nativeQuery =
            true)
    Long getNextSeriesId();

My mistake was that I used oracle syntax instead of postgreSQL, which I am using.

Sign up to request clarification or add additional context in comments.

Comments

0

The same issue I also faced. The DB has sequecne under schema and after adding schema it working fine. My code is like -

@Entity
@Table(name = "Contact", schema = "tshema")
public class ContactUs implements AppEntity {

    @Id
    @SequenceGenerator(name = "OID_SEQ", sequenceName = "CONTACTUS_OID_SEQ", allocationSize = 1, schema = "tshema")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "OID_SEQ")
    @Column(name = "oid", unique = true, nullable = false, updatable = false)
    private Long oid;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.