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;
}
}