0

I need a simple sequence which would give me incremented integers that I later on use as part of a String.

I made this sequence using postgresql command line:

CREATE SEQUENCE my_seq
INCREMENT BY 1

The sequence exists as I can query it from postgresql command line, But I'm trying to get the values using hibernate:

Query query = session.createSQLQuery("select nextval(:sequence);");
query.setParameter("sequence", "my_seq");
Long nextVal=((BigInteger)query.uniqueResult()).longValue();

And I am getting this exception:

ERROR: relation "my_seq" does not exist

The sequence values do NOT represent the attribute of any entity. I only need them to store the number of logins, but I do not store the logins as entities.

EDIT: I got it working by adding the scheme name to the query:

String query1 = "select nextval(myscheme.my_seq)";
Query query = session.createSQLQuery(query1);

I can't figure out why it needed the scheme though, as myscheme was already default and all other queries worked fine without specifying the scheme. If anyone can shed some light I shall accept its answer.

3
  • does this select nextval(my_seq) work if you run it in db client itself? Commented Oct 3, 2014 at 13:43
  • Yes, it returns the value incremented. Commented Oct 3, 2014 at 13:44
  • this link might help you take a look stackoverflow.com/questions/6386888/… Commented Oct 3, 2014 at 14:42

3 Answers 3

1

You don't need to map a sequence to an entity. Just leave the mapping as follow for the ID column of your entity, and the id of the tntity will be generated from the sequence my_seq. You don't need to call nextval(my_seq) or anything else.

@Entity("myEntity")
public class MyEntity {

   @Id
   @Column(name = "id", unique = true, nullable = false, insertable=false)
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
   @SequenceGenerator(name = "my_seq", sequenceName = "my_seq", allocationSize = 1)
   private long id;
}
Sign up to request clarification or add additional context in comments.

3 Comments

As I said, the sequence is not an attribute of an entity, I do not have any 'myEntity' nor do I need it. I only want the integer from nextVal so I can use it as part of a code. More specifically I will form a string like: "You are " + nextValOfSequence + "nth user" which I will send to the user, however I do not store the user in the db.
In this case you should just call session.createSQLQuery("select nextval(my_seq)") without any mapping at all.
I did, but hibernate cannot find it.
0

Below is the logic which will use a sequence called "my_seq" from the database and map it to the column "column_name" table "table_name"

Soon after you import the classes the sequence generator should be placed before you begin the class

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "table_name")
@SequenceGenerator(name = "my_seq", sequenceName = "my_seq")

public class ClassName implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_seq")
@Column(name = "column_name")
private Long columnName;

}

Let me know if this is useful.

1 Comment

Thanks, but I don't want to map it to any table. I got it working though.
0

In order to avoid hardcoding schema to your query, you can set hibernate.default_schema property in your persistence.xml as follows.

<property name="hibernate.default_schema" value="myscheme"/>

This could be also used in spring configuration file as is stated here.

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.