1

I'm trying to configure a mapped class to use a sequence I defined in a postgres db. The ids are always zero when I try to persist any entities, if I use select nextval('item_seq'), I'll get 1 (or the next val). I used intellij to regenerate the classes. The version of hibernate in this project is 3.6.0, if that might be part of my problem?

@Entity
@Table(name = "item")
public class Item {
    private int itemid;
    ...

    @Basic
    @Id
    @SequenceGenerator(name = "item_seq", sequenceName = "item_seq", allocationSize = 1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "item_seq")
    @Column(name = "itemid", unique = true, nullable = false, insertable = true, updatable = true)

    public int getItemid() {
        return itemid;
    }
    ...
}

Usage

Item item = new Item();
item.setCreated(new Date());
item.setVendorId(vendorId);
save(item); // essentially is getHibernateTemplate().save(Item.class.getName(), item);

-- EDIT --

I've been trying the suggestions below and everything seems to keep generating 0 as an id or throw the exception 'ids for this class must be manually assigned before calling save()'. This is where I am now, as a last ditch effort I tried moving the annotations to the variable declaration instead of the getter. Didn't help.

@Entity
@Table(name = "item")
public class Item {
    @Id
    //@SequenceGenerator(name = "item_seq", sequenceName = "item_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.IDENTITY) //, generator = "item_seq")
    @Column(name = "itemid", unique = true, nullable = false) //, insertable = true, updatable = true)
    private Long itemid;
...

    public Long getItemid() {
        return itemid;
    }
}
Crushing
  • 487
  • 1
  • 9
  • 24
  • I worked with a project with postgres + hibernate and worked very well. Try to remove allocationSize = 1. Usually hibernate does a select nextval('item_seq') before the insert. – Rodrigo Menezes Mar 16 '15 at 13:22
  • I tried removing allocationSize option from the sequence generator annotation but did not change. Also tried this in combination with changing type to 'Integer'. – Crushing Mar 16 '15 at 14:55
  • use IDENTITY generator strategy with postgres, and dont define a sequence. it works – Neil McGuigan Mar 16 '15 at 20:41

4 Answers4

1

This always works for me (Hibernate 4.x though):

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Id makes the column a primary key (unique, no nulls), so you don't need the @Column(...) annotation. Is something setting your itemId somewhere? You can remove its setter if you have one (Hibernate doesn't require).

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • I changed around my types for itemid and my persistence classes, I get the 'ids for this class must be manually assigned before calling save()'. I'll update my post. – Crushing Mar 17 '15 at 18:08
  • 1
    I upgraded to hibernate 4 and this is how my working setup is configured. Should have accepted this earlier... – Crushing Apr 22 '15 at 14:40
0

what database u are using? can be possibly a database that does not support sequence??

try to use strategy=auto and see how does it work

since 'select nextval(...)' works, your database (postgresql) is supporting sequence. so thats not it

maybe for some reason hibernate is threating yout int == 0 as an id and it is trying to update it instead of inserting a new record ( deafult value for int =0 ) just change your id type to Integer and see if it solve the problem

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
alizelzele
  • 892
  • 2
  • 19
  • 34
  • When I try setting the type to "Integer", hibernate was throwing the exception "ids for this class must be manually assigned before calling save()". I even tried changing the generation strategy to IDENTITY like I've seen in a lot of posts but same exceptions thrown. – Crushing Mar 16 '15 at 14:53
  • well `idis must be manually set` is defenetly the sign than it is ignoring sequence generator. just for sake of it, can u run it without `@Basic` and see how it will go? – alizelzele Mar 17 '15 at 06:35
  • Tried removing @Basic, no difference. – Crushing Mar 17 '15 at 18:03
0

Try to provide database schema name for given table and sequence in you entity class. Make sure your application user has privileges (grants) to the sequence.

-- update --

I think solution is here

Community
  • 1
  • 1
jesse
  • 13
  • 1
  • 6
0

I was having the same problem and here's what I did to solve the issue

I was using Hibernate tools to auto generate POJOs and all the annotations were being placed at the method level, however, Spring recommends (requires?) them at the field level. You can't just move the Id annotations to the field level either, because it's either one or the other (I got some other exceptions when I tried this). So I followed this answer to customize Hibernate tools to generate POJOs with the annotations all at the field level.

Now my code looks like this and it's using the database sequence sequence just fine.

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="my_seq")
@SequenceGenerator(name="my_seq",sequenceName="SPT_PROJECT_SEQ", allocationSize=1)
@Column(name = "PROJECT_ID", unique = true, nullable = false, precision = 10, scale = 0)
private long projectId;

Hope this helps

Community
  • 1
  • 1
otgw
  • 390
  • 1
  • 5
  • 16